FreeDATA/modem/beacon.py

41 lines
1.3 KiB
Python
Raw Normal View History

2023-11-26 14:45:41 +00:00
import command_beacon
2024-01-11 13:56:45 +00:00
import sched
import time
import threading
2023-11-26 14:23:20 +00:00
2023-11-26 20:51:19 +00:00
class Beacon:
def __init__(self, config, states, event_manager, logger, modem):
2024-01-11 13:56:45 +00:00
self.config = config
self.states = states
self.event_manager = event_manager
2023-11-26 20:51:19 +00:00
self.log = logger
2023-12-21 16:47:48 +00:00
self.modem = modem
2023-11-26 14:23:20 +00:00
2024-01-11 13:56:45 +00:00
self.scheduler = sched.scheduler(time.time, time.sleep)
self.beacon_interval = self.config['MODEM']['beacon_interval']
self.beacon_enabled = False
2023-11-26 20:51:19 +00:00
self.event = threading.Event()
2023-11-26 14:23:20 +00:00
2023-11-26 20:51:19 +00:00
def start(self):
2024-01-11 13:56:45 +00:00
self.beacon_enabled = True
self.schedule_beacon()
2023-11-26 14:23:20 +00:00
def stop(self):
2024-01-11 13:56:45 +00:00
self.beacon_enabled = False
2023-11-26 20:51:19 +00:00
2024-01-11 13:56:45 +00:00
def schedule_beacon(self):
if self.beacon_enabled:
self.scheduler.enter(self.beacon_interval, 1, self.run_beacon)
threading.Thread(target=self.scheduler.run, daemon=True).start()
2023-11-26 14:23:20 +00:00
2024-01-11 13:56:45 +00:00
def run_beacon(self):
if self.beacon_enabled:
# Your beacon logic here
cmd = command_beacon.BeaconCommand(self.config, self.states, self.event_manager)
cmd.run(self.event_manager, self.modem)
self.schedule_beacon() # Reschedule the next beacon
2023-11-26 14:23:20 +00:00
2024-01-11 13:56:45 +00:00
def refresh(self):
# Interrupt and reschedule the beacon
self.scheduler = sched.scheduler(time.time, time.sleep)
self.schedule_beacon()