From 77d954f14e45fe14fb684202e30a854202de4faa Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 12 Jun 2018 00:36:43 +0200 Subject: [PATCH] Simplified MCP3008 sensor backend by letting it extend SensorBackend --- platypush/backend/sensor/__init__.py | 32 ++++++++++++++------- platypush/backend/sensor/mcp3008.py | 42 +++------------------------- 2 files changed, 26 insertions(+), 48 deletions(-) diff --git a/platypush/backend/sensor/__init__.py b/platypush/backend/sensor/__init__.py index 8b31ba1ad..860eb769c 100644 --- a/platypush/backend/sensor/__init__.py +++ b/platypush/backend/sensor/__init__.py @@ -1,28 +1,37 @@ +import time + from platypush.backend import Backend from platypush.message.event.sensor import SensorDataChangeEvent, \ SensorDataAboveThresholdEvent, SensorDataBelowThresholdEvent class SensorBackend(Backend): - def __init__(self, thresholds=None, *args, **kwargs): + def __init__(self, thresholds=None, poll_seconds=None, *args, **kwargs): """ - Thresholds is either a scalr value or a dictionary. + Params: + -- thresholds: Thresholds can be either a scalr value or a dictionary. - If set, SensorDataAboveThresholdEvent and SensorDataBelowThresholdEvent - events will be triggered whenever the measurement goes above or below - that value. + If set, SensorDataAboveThresholdEvent and SensorDataBelowThresholdEvent + events will be triggered whenever the measurement goes above or + below that value. - Set it as a scalar if your get_measurement() code returns a scalar, - as a dictionary if it returns a dictionary of values. + Set it as a scalar if your get_measurement() code returns a scalar, + as a dictionary if it returns a dictionary of values. - For instance, if your sensor code returns both humidity and temperature - in a format like {'humidity':60.0, 'temperature': 25.0}, you'll want to - set up a threshold on temperature with a syntax like {'temperature':20.0} + For instance, if your sensor code returns both humidity and + temperature in a format like {'humidity':60.0, 'temperature': 25.0}, + you'll want to set up a threshold on temperature with a syntax like + {'temperature':20.0} + + -- poll_seconds: If set, the thread will wait for the specificed + number of seconds between a read and the next one. """ super().__init__(**kwargs) + self.data = None self.thresholds = thresholds + self.poll_seconds = poll_seconds def get_measurement(self): raise NotImplementedError('To be implemented in a derived class') @@ -56,6 +65,9 @@ class SensorBackend(Backend): self.data = new_data + if self.poll_seconds: + time.sleep(self.poll_seconds) + # vim:sw=4:ts=4:et: diff --git a/platypush/backend/sensor/mcp3008.py b/platypush/backend/sensor/mcp3008.py index d49952355..0a06bc152 100644 --- a/platypush/backend/sensor/mcp3008.py +++ b/platypush/backend/sensor/mcp3008.py @@ -1,45 +1,11 @@ -import time - -from platypush.backend import Backend +from platypush.backend.sensor import SensorBackend from platypush.context import get_plugin -from platypush.message.event.sensor import SensorDataChangeEvent -from platypush.plugins.gpio.sensor.mcp3008 import GpioSensorMcp3008Plugin -class SensorMcp3008Backend(Backend): - last_measurement = {} - - def __init__(self, poll_seconds, *args, **kwargs): - super().__init__(*args, **kwargs) - self.poll_seconds = poll_seconds - self.logger.info('Initialized MCP3008 analog sensors backend') - - - def send_message(self, msg): - pass - - - def run(self): - super().run() +class SensorMcp3008Backend(SensorBackend): + def get_measurement(self): plugin = get_plugin('gpio.sensor.mcp3008') - - while not self.should_stop(): - try: - measurement = plugin.get_measurement().output - except: - plugin = get_plugin('gpio.sensor.mcp3008', reload=True) - - new_measurement = {} - for key in measurement.keys(): - if key not in self.last_measurement \ - or measurement[key] != self.last_measurement[key]: - new_measurement[key] = measurement[key] - - if new_measurement: - self.bus.post(SensorDataChangeEvent(data=new_measurement)) - - self.last_measurement = measurement - time.sleep(self.poll_seconds) + return plugin.get_data().output # vim:sw=4:ts=4:et: