From 8f0f7908e1ce9cef7dc5dafa9c75a07fb21d5d1d Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Sat, 10 Dec 2022 22:42:27 +0100 Subject: [PATCH] added semaphore for using the callback; keep the monitoring thread running, only enable/disable the callback (removes the dead time for connecting during start) --- slic/core/sensor/bssensor.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/slic/core/sensor/bssensor.py b/slic/core/sensor/bssensor.py index d4d67515..04e68a1d 100644 --- a/slic/core/sensor/bssensor.py +++ b/slic/core/sensor/bssensor.py @@ -9,12 +9,16 @@ MSG_MISSING_TYPE = "'type' channel field not found. Parse as 64-bit floating-poi class BSSensor(Sensor): - def start(self): - self.thread = thread = BSMonitorThread(self.name, self._collect) + def __init__(self, name, **kwargs): + super().__init__(name, **kwargs) + self.thread = thread = BSMonitorThread(name, self._collect) thread.start() + def start(self): + self.thread.enable_callback() + def stop(self): - self.thread.stop() + self.thread.disable_callback() @@ -24,21 +28,30 @@ class BSMonitorThread(Thread): super().__init__() self.name = name self.callback = callback + self.use_callback = Event() self.running = Event() def run(self): + use_callback = self.use_callback running = self.running running.set() with BSChannel(self.name) as chan: while running.is_set(): value = chan.get() - self.callback(value) + if use_callback.is_set(): + self.callback(value) running.clear() def stop(self): self.running.clear() self.join() + def enable_callback(self): + self.use_callback.set() + + def disable_callback(self): + self.use_callback.clear() + class BSChannel(source):