From 59cab50a3265deb044ba860405974832c676a0a2 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Fri, 8 Jan 2021 19:45:57 +0000 Subject: [PATCH] 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. --- relay.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/relay.py b/relay.py index 42af4b2..30434a7 100644 --- a/relay.py +++ b/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.