From a4dd5a25cfe2c788a58f4e5922350f5c7459e4f8 Mon Sep 17 00:00:00 2001 From: DJ2LS Date: Thu, 11 Jan 2024 14:39:53 +0100 Subject: [PATCH] improved explorer publising using sched --- modem/explorer.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/modem/explorer.py b/modem/explorer.py index 9af681f4..13e302c5 100644 --- a/modem/explorer.py +++ b/modem/explorer.py @@ -11,6 +11,8 @@ import requests import threading import ujson as json import structlog +import sched +import time log = structlog.get_logger("explorer") @@ -21,18 +23,16 @@ class explorer(): self.states = states self.explorer_url = "https://api.freedata.app/explorer.php" self.publish_interval = 120 - self.enable_explorer = config["STATION"]["enable_explorer"] - if self.enable_explorer: - self.interval_thread = threading.Thread(target=self.interval, name="interval", daemon=True) - self.interval_thread.start() + self.scheduler = sched.scheduler(time.time, time.sleep) + self.schedule_thread = threading.Thread(target=self.run_scheduler) + self.schedule_thread.start() - def interval(self): - # Wait for just a little bit incase modem is contionously restarting due to a bug or user configuration issue - threading.Event().wait(30) - while True: - self.push() - threading.Event().wait(self.publish_interval) + def run_scheduler(self): + # Schedule the first execution of push + self.scheduler.enter(self.publish_interval, 1, self.push) + # Run the scheduler in a loop + self.scheduler.run() def push(self): @@ -67,8 +67,14 @@ class explorer(): station_data = json.dumps(station_data) try: response = requests.post(self.explorer_url, json=station_data, headers=headers) - # print(response.status_code) - # print(response.content) except Exception as e: log.warning("[EXPLORER] connection lost") + + # Reschedule the push method + self.scheduler.enter(self.publish_interval, 1, self.push) + + def shutdown(self): + # If there are other cleanup tasks, include them here + if self.schedule_thread: + self.schedule_thread.join()