Background updater for lxmd sensor

This commit is contained in:
Mark Qvist 2025-01-26 12:15:26 +01:00
parent cc87e8c109
commit b03d91d206

View File

@ -2733,11 +2733,15 @@ class RNSTransport(Sensor):
class LXMFPropagation(Sensor): class LXMFPropagation(Sensor):
SID = Sensor.SID_LXMF_PROPAGATION SID = Sensor.SID_LXMF_PROPAGATION
STALE_TIME = 120 STALE_TIME = 45
def __init__(self): def __init__(self):
self.identity = None self.identity = None
self.lxmd = None self.lxmd = None
self._last_update = 0
self._update_interval = 60
self._update_lock = threading.Lock()
self._running = False
super().__init__(type(self).SID, type(self).STALE_TIME) super().__init__(type(self).SID, type(self).STALE_TIME)
def set_identity(self, identity): def set_identity(self, identity):
@ -2751,34 +2755,61 @@ class LXMFPropagation(Sensor):
except Exception as e: except Exception as e:
RNS.log("Could not load LXMF propagation sensor identity from \"{file_path}\"", RNS.LOG_ERROR) RNS.log("Could not load LXMF propagation sensor identity from \"{file_path}\"", RNS.LOG_ERROR)
def _update_job(self):
while self._running:
self._update_data()
time.sleep(self._update_interval)
def _start_update_job(self):
if not self._running:
self._running = True
update_thread = threading.Thread(target=self._update_job, daemon=True)
update_thread.start()
def setup_sensor(self): def setup_sensor(self):
self.update_data() self.update_data()
def teardown_sensor(self): def teardown_sensor(self):
self._running = False
self.identity = None self.identity = None
self.data = None self.data = None
def update_data(self): def update_data(self):
if self.identity != None: # This sensor runs the actual data updates
if self.lxmd == None: # in the background. An update_data request
import LXMF.LXMPeer as LXMPeer # will simply start the update job if it is
import LXMF.Utilities.lxmd as lxmd # not already running.
self.ERROR_NO_IDENTITY = LXMPeer.LXMPeer.ERROR_NO_IDENTITY if not self._running:
self.ERROR_NO_ACCESS = LXMPeer.LXMPeer.ERROR_NO_ACCESS RNS.log(self)
self.ERROR_TIMEOUT = LXMPeer.LXMPeer.ERROR_TIMEOUT self._start_update_job()
self.lxmd = lxmd
status_response = self.lxmd.query_status(identity=self.identity) def _update_data(self):
if status_response == None: if not self.synthesized:
RNS.log("Status response from lxmd was received, but contained no data", RNS.LOG_ERROR) with self._update_lock:
elif status_response == self.ERROR_NO_IDENTITY: if time.time() - self._last_update < self.STALE_TIME:
RNS.log("Updating telemetry from lxmd failed due to missing identification", RNS.LOG_ERROR) return
elif status_response == self.ERROR_NO_ACCESS:
RNS.log("Access was denied while attempting to update lxmd telemetry", RNS.LOG_ERROR) if self.identity != None:
elif status_response == self.ERROR_TIMEOUT: if self.lxmd == None:
RNS.log("Updating telemetry from lxmd failed due to timeout", RNS.LOG_ERROR) import LXMF.LXMPeer as LXMPeer
else: import LXMF.Utilities.lxmd as lxmd
self.data = status_response self.ERROR_NO_IDENTITY = LXMPeer.LXMPeer.ERROR_NO_IDENTITY
self.ERROR_NO_ACCESS = LXMPeer.LXMPeer.ERROR_NO_ACCESS
self.ERROR_TIMEOUT = LXMPeer.LXMPeer.ERROR_TIMEOUT
self.lxmd = lxmd
self._last_update = time.time()
status_response = self.lxmd.query_status(identity=self.identity)
if status_response == None:
RNS.log("Status response from lxmd was received, but contained no data", RNS.LOG_ERROR)
elif status_response == self.ERROR_NO_IDENTITY:
RNS.log("Updating telemetry from lxmd failed due to missing identification", RNS.LOG_ERROR)
elif status_response == self.ERROR_NO_ACCESS:
RNS.log("Access was denied while attempting to update lxmd telemetry", RNS.LOG_ERROR)
elif status_response == self.ERROR_TIMEOUT:
RNS.log("Updating telemetry from lxmd failed due to timeout", RNS.LOG_ERROR)
else:
self.data = status_response
def pack(self): def pack(self):
d = self.data d = self.data