From 0905981c588369c7f102a0bd81276fb19ba21d0e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 11 Jun 2018 21:07:54 +0200 Subject: [PATCH] Refactoring sensor backend and added SensorSerialBackend --- platypush/backend/sensor/__init__.py | 60 ++++++++++++++++++++++ platypush/backend/sensor/serial.py | 21 ++++++++ platypush/message/event/sensor/__init__.py | 17 +++++- 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 platypush/backend/sensor/serial.py diff --git a/platypush/backend/sensor/__init__.py b/platypush/backend/sensor/__init__.py index e69de29bb..215c07128 100644 --- a/platypush/backend/sensor/__init__.py +++ b/platypush/backend/sensor/__init__.py @@ -0,0 +1,60 @@ +from platypush.backend import Backend +from platypush.message.event.sensor import SensorDataChangeEvent, \ + SensorDataAboveThresholdEvent, SensorDataBelowThresholdEvent + + +class SensorBackend(Backend): + def __init__(self, thresholds=None, *args, **kwargs): + """ + Thresholds is 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. + + 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} + """ + + super().__init__(**kwargs) + self.measurement = None + self.thresholds = thresholds + + def get_measurement(self): + raise NotImplementedError('To be implemented in a derived class') + + def run(self): + super().run() + + while not self.should_stop(): + new_data = self.get_measurement() + if self.data is None or self.data != new_data: + self.bus.post(SensorDataChangeEvent(data=new_data)) + + if self.thresholds: + if isinstance(self.thresholds, dict) and isinstance(new_data, dict): + for (measure, threshold) in self.thresholds.items(): + if measure not in new_data: + continue + + if new_data[measure] > threshold and (self.data is None or ( + measure in self.data and self.data[measure] <= threshold)): + self.bus.post(SensorDataAboveThresholdEvent(data=new_data)) + elif new_data[measure] < threshold and (self.data is None or ( + measure in self.data and self.data[measure] >= threshold)): + self.bus.post(SensorDataBelowThresholdEvent(data=new_data)) + else: + if new_data > threshold and (self.data is None or self.data <= threshold): + self.bus.post(SensorDataAboveThresholdEvent(data=new_data)) + elif new_data < threshold and (self.data is None or self.data >= threshold): + self.bus.post(SensorDataBelowThresholdEvent(data=new_data)) + + self.data = new_data + + +# vim:sw=4:ts=4:et: + diff --git a/platypush/backend/sensor/serial.py b/platypush/backend/sensor/serial.py new file mode 100644 index 000000000..4c3fbd325 --- /dev/null +++ b/platypush/backend/sensor/serial.py @@ -0,0 +1,21 @@ +import serial + +from platypush.backend.sensor import SensorBackend +from platypush.context import get_plugin + + +class SensorSerialBackend(SensorBackend): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def get_measurement(self): + plugin = get_plugin('serial') + return plugin.get_data().output + + def run(self): + self.logger.info('Initialized serial backend') + super().run() + + +# vim:sw=4:ts=4:et: + diff --git a/platypush/message/event/sensor/__init__.py b/platypush/message/event/sensor/__init__.py index 9ec6a2c64..d39fe542e 100644 --- a/platypush/message/event/sensor/__init__.py +++ b/platypush/message/event/sensor/__init__.py @@ -2,8 +2,21 @@ from platypush.message.event import Event class SensorDataChangeEvent(Event): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) + def __init__(self, data, *args, **kwargs): + super().__init__(data=data, *args, **kwargs) + self.data = data + + +class SensorDataAboveThresholdEvent(Event): + def __init__(self, data, *args, **kwargs): + super().__init__(data=data, *args, **kwargs) + self.data = data + + +class SensorDataBelowThresholdEvent(Event): + def __init__(self, data, *args, **kwargs): + super().__init__(data=data, *args, **kwargs) + self.data = data # vim:sw=4:ts=4:et: