diff --git a/platypush/backend/sensor/mcp3008.py b/platypush/backend/sensor/mcp3008.py new file mode 100644 index 000000000..ab009f43f --- /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 e69de29bb..9ec6a2c64 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: + +