mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
improved explorer publising using sched
This commit is contained in:
parent
a1c92a0a03
commit
a4dd5a25cf
1 changed files with 18 additions and 12 deletions
|
@ -11,6 +11,8 @@ import requests
|
||||||
import threading
|
import threading
|
||||||
import ujson as json
|
import ujson as json
|
||||||
import structlog
|
import structlog
|
||||||
|
import sched
|
||||||
|
import time
|
||||||
|
|
||||||
log = structlog.get_logger("explorer")
|
log = structlog.get_logger("explorer")
|
||||||
|
|
||||||
|
@ -21,18 +23,16 @@ class explorer():
|
||||||
self.states = states
|
self.states = states
|
||||||
self.explorer_url = "https://api.freedata.app/explorer.php"
|
self.explorer_url = "https://api.freedata.app/explorer.php"
|
||||||
self.publish_interval = 120
|
self.publish_interval = 120
|
||||||
self.enable_explorer = config["STATION"]["enable_explorer"]
|
|
||||||
|
|
||||||
if self.enable_explorer:
|
self.scheduler = sched.scheduler(time.time, time.sleep)
|
||||||
self.interval_thread = threading.Thread(target=self.interval, name="interval", daemon=True)
|
self.schedule_thread = threading.Thread(target=self.run_scheduler)
|
||||||
self.interval_thread.start()
|
self.schedule_thread.start()
|
||||||
|
|
||||||
def interval(self):
|
def run_scheduler(self):
|
||||||
# Wait for just a little bit incase modem is contionously restarting due to a bug or user configuration issue
|
# Schedule the first execution of push
|
||||||
threading.Event().wait(30)
|
self.scheduler.enter(self.publish_interval, 1, self.push)
|
||||||
while True:
|
# Run the scheduler in a loop
|
||||||
self.push()
|
self.scheduler.run()
|
||||||
threading.Event().wait(self.publish_interval)
|
|
||||||
|
|
||||||
def push(self):
|
def push(self):
|
||||||
|
|
||||||
|
@ -67,8 +67,14 @@ class explorer():
|
||||||
station_data = json.dumps(station_data)
|
station_data = json.dumps(station_data)
|
||||||
try:
|
try:
|
||||||
response = requests.post(self.explorer_url, json=station_data, headers=headers)
|
response = requests.post(self.explorer_url, json=station_data, headers=headers)
|
||||||
# print(response.status_code)
|
|
||||||
# print(response.content)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning("[EXPLORER] connection lost")
|
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()
|
||||||
|
|
Loading…
Reference in a new issue