forked from platypush/platypush
Added MCP3008 backend
This commit is contained in:
parent
2f6565ec87
commit
bd8d4649c5
2 changed files with 57 additions and 0 deletions
46
platypush/backend/sensor/mcp3008.py
Normal file
46
platypush/backend/sensor/mcp3008.py
Normal file
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
||||
|
Loading…
Reference in a new issue