1
0
Fork 0
Browse Source

update how URL is formed (RESTAPI call).

This adds a check to determine which relay the system is (hostname/device name). When name is determined,the URL the  program will use for the RESTAPI call is then set. At the time of writing, the system is expecting two relays. Hence the if-else statement. If more devices are to be added, this approach will need to be reconsidered.
unstable
Craig Oates 3 years ago
parent
commit
59cab50a32
  1. 14
      relay.py

14
relay.py

@ -5,16 +5,26 @@ import time
import requests
relay_pin = 11
#GPIO.setmode(GPIO.BCM) # GPIO umbers instead of board numbers.
#GPIO.setmode(GPIO.BCM) # GPIO numbers instead of board numbers.
GPIO.setmode(GPIO.BOARD)
GPIO.setup(relay_pin, GPIO.OUT)
def get_api_url():
# Yes, I know I could do this better. Stop moaning.
if (device_id == "factory1"):
return "http://ritherdon.abbether.net/api/readings/add/1"
elif (device_id == "factory2"):
return "http://ritherdon.abbether.net/api/readings/add/2"
# Make sure this is valid.
# api_url = "http://ritherdon.abbether.net/api/readings/add/1"
api_url = get_api_url() # Saves having to do the if-check every call.
def main():
try:
while True:
# Make sure the U.R.L. is valid.
r = requests.get('http://ritherdon.abbether.net/api/readings/latest/1')
r = requests.get(api_url)
# print(r.status_code) # For testing.
data = r.json()
# print(data) # For testing.