From bd8d4649c5ee3f88bee3379e33033a396a7e6043 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 29 Apr 2018 23:51:53 +0200 Subject: [PATCH] Added MCP3008 backend --- platypush/backend/sensor/mcp3008.py | 46 ++++++++++++++++++++++ platypush/message/event/sensor/__init__.py | 11 ++++++ 2 files changed, 57 insertions(+) create mode 100644 platypush/backend/sensor/mcp3008.py diff --git a/platypush/backend/sensor/mcp3008.py b/platypush/backend/sensor/mcp3008.py new file mode 100644 index 0000000000..ab009f43f9 --- /dev/null +++ b/platypush/backend/sensor/mcp3008.py @@ -0,0 +1,46 @@ +import logging +import time + +from platypush.backend import Backend +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=0.25, *args, **kwargs): + super().__init__(*args, **kwargs) + self.poll_seconds = poll_seconds + logging.info('Initialized MCP3008 analog sensors backend') + + + def send_message(self, msg): + pass + + + def run(self): + super().run() + 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(sensors=new_measurement)) + + time.sleep(self.poll_seconds) + + +# vim:sw=4:ts=4:et: + diff --git a/platypush/message/event/sensor/__init__.py b/platypush/message/event/sensor/__init__.py index e69de29bb2..9ec6a2c64d 100644 --- a/platypush/message/event/sensor/__init__.py +++ b/platypush/message/event/sensor/__init__.py @@ -0,0 +1,11 @@ +from platypush.message.event import Event + + +class SensorDataChangeEvent(Event): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + +# vim:sw=4:ts=4:et: + +