Simplified MCP3008 sensor backend by letting it extend SensorBackend
This commit is contained in:
parent
c410bd8926
commit
77d954f14e
2 changed files with 26 additions and 48 deletions
|
@ -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:
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue