def main(): """Schedule and run strategy.""" url, port, plantID, devID, ts_url, ts_port = functions.read_file(FILE) thread1 = SchedulingThread(1, "thread1") thread1.start() while True: url, port, plantID, devID, ts_url, ts_port = functions.read_file(FILE) string = ("http://" + url + ":" + port + "/info/" + plantID) data = json.loads(requests.get(string).text) hours = data["hours"] env = data["environment"] global TIME_LIST for h in hours: t = h["time"] delayed_hour = functions.delay_h(t, -300) entry = { "hour": t, "schedule_time": delayed_hour, # "schedule_time": "15:43", "env": env } TIME_LIST.append(entry) # Fill timetable. print("Rain check at: %s - %s" % (delayed_hour, plantID)) time.sleep(86400) # One day TIME_LIST = [] # Reset timetable
def irr(self, plantID, hour, devID, url, port): """Compute irrigation delay and duration. It checks the irrigation parameters stored in dynamic catalog and then computer total irrigation delay and duration. Then it calls other functions to publish immediately or after a delay the MQTT message which actually starts irrigation. """ # Get dynamic catalog. string = "http://" + url + ":" + port + "/dynamic" data = json.loads(requests.get(string).text) # Get device topic. string_dev = "http://" + url + ":" + port + "/info/" + devID data_dev = json.loads(requests.get(string_dev).text) topic = data_dev["topic"] # Find hour modifications. for g in data["gardens"]: for p in g["plants"]: if p["plantID"] == plantID: for h in p["hours"]: if h["time"] == hour: duration = h["duration"] delay = h["delay"] break string = "http://" + url + ":" + port + "/info/" + plantID data = json.loads(requests.get(string).text) water = data["environment"]["water"] duration = duration + water # Reset data about duration and delay on catalog. Next day it will be # generated again. functions.reset_mod(plantID, h, url, port) # If there is no delay: publish via MQTT irrigation and duration. if delay == 0: if topic is not None: self.publish(duration, topic) # If the irrigation has to be anticipated do the same as before but # write on catalog that the next day it has to be anticipated. elif delay < 0: if topic is not None: self.publish(duration, topic) new_h = functions.delay_h(h, delay) functions.post_delay(plantID, h, new_h, url, port) # If there is a delay: start thread with a countdown and then publish. elif delay > 0: delayed_irrigation = DelayedIrrigation(devID.replace('d_', ''), devID + "_sch", delay, self.pub, duration, topic) delayed_irrigation.start()