From 6738ff832ab3470bcf23b2363c7b7aae3959da77 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 8 Jan 2020 00:20:00 +0100 Subject: [PATCH] get_data/get_measurement/close should be implemented in a base SensorPlugin class, not specifically in GpioSensorPlugin --- platypush/plugins/arduino.py | 4 +- platypush/plugins/gpio/sensor/__init__.py | 44 +++--------------- platypush/plugins/sensor/__init__.py | 54 +++++++++++++++++++++++ platypush/plugins/serial/__init__.py | 8 ++-- 4 files changed, 65 insertions(+), 45 deletions(-) create mode 100644 platypush/plugins/sensor/__init__.py diff --git a/platypush/plugins/arduino.py b/platypush/plugins/arduino.py index 978edf65e..844aabf07 100644 --- a/platypush/plugins/arduino.py +++ b/platypush/plugins/arduino.py @@ -6,7 +6,7 @@ from typing import Optional, Dict, Union, Callable, Tuple, Type from pyfirmata2 import Arduino, ArduinoMega, ArduinoDue, ArduinoNano, Pin, util, ANALOG, INPUT, PWM from platypush.plugins import action -from platypush.plugins.gpio.sensor import GpioSensorPlugin +from platypush.plugins.sensor import SensorPlugin class PinType(enum.IntEnum): @@ -21,7 +21,7 @@ class BoardType(enum.Enum): # noinspection PyBroadException -class ArduinoPlugin(GpioSensorPlugin): +class ArduinoPlugin(SensorPlugin): """ Interact with an Arduino connected to the host machine over USB using the `Firmata `_ protocol. diff --git a/platypush/plugins/gpio/sensor/__init__.py b/platypush/plugins/gpio/sensor/__init__.py index 6ab5c4327..3bf82ee0a 100644 --- a/platypush/plugins/gpio/sensor/__init__.py +++ b/platypush/plugins/gpio/sensor/__init__.py @@ -1,44 +1,10 @@ -from platypush.plugins import Plugin, action +from abc import ABC + +from platypush.plugins.sensor import SensorPlugin -class GpioSensorPlugin(Plugin): - """ - GPIO sensor abstract plugin. Any plugin that interacts with sensor via GPIO - should implement this class (and the get_measurement() method) - """ - - def __init__(self, **kwargs): - super().__init__(**kwargs) - - @action - def get_measurement(self, *args, **kwargs): - """ - Implemented by the subclasses. - - :returns: Either a raw scalar: - - ``output = 273.16`` - - or a name-value dictionary with the values that have been read:: - - output = { - "temperature": 21.5, - "humidity": 41.0 - } - """ - raise NotImplementedError('get_measurement should be implemented in a derived class') - - @action - def get_data(self, *args, **kwargs): - """ - Alias for ``get_measurement`` - """ - - return self.get_measurement(*args, **kwargs).output - - @action - def close(self): - pass +class GpioSensorPlugin(ABC, SensorPlugin): + pass # vim:sw=4:ts=4:et: diff --git a/platypush/plugins/sensor/__init__.py b/platypush/plugins/sensor/__init__.py new file mode 100644 index 000000000..eccc8ef42 --- /dev/null +++ b/platypush/plugins/sensor/__init__.py @@ -0,0 +1,54 @@ +from platypush.plugins import Plugin, action + + +class SensorPlugin(Plugin): + """ + Sensor abstract plugin. Any plugin that interacts with sensors + should implement this class (and the get_measurement() method) + """ + + def __init__(self, **kwargs): + super().__init__(**kwargs) + + @action + def get_measurement(self, *args, **kwargs): + """ + Implemented by the subclasses. + + :returns: Either a raw scalar: + + ``output = 273.16`` + + or a name-value dictionary with the values that have been read:: + + output = { + "temperature": 21.5, + "humidity": 41.0 + } + + or a list of values:: + + [ + 0.01, + 0.34, + 0.53, + ... + ] + + """ + raise NotImplementedError('get_measurement should be implemented in a derived class') + + @action + def get_data(self, *args, **kwargs): + """ + Alias for ``get_measurement`` + """ + + return self.get_measurement(*args, **kwargs).output + + @action + def close(self): + pass + + +# vim:sw=4:ts=4:et: diff --git a/platypush/plugins/serial/__init__.py b/platypush/plugins/serial/__init__.py index 5b6670925..9e3a34e37 100644 --- a/platypush/plugins/serial/__init__.py +++ b/platypush/plugins/serial/__init__.py @@ -5,11 +5,11 @@ import threading import time from platypush.plugins import action -from platypush.plugins.gpio.sensor import GpioSensorPlugin +from platypush.plugins.sensor import SensorPlugin # noinspection PyBroadException -class SerialPlugin(GpioSensorPlugin): +class SerialPlugin(SensorPlugin): """ The serial plugin can read data from a serial device, as long as the serial device returns a JSON. You can use this plugin to interact for example with @@ -19,7 +19,7 @@ class SerialPlugin(GpioSensorPlugin): https://github.com/bblanchon/ArduinoJson. """ - def __init__(self, device=None, baud_rate=9600, *args, **kwargs): + def __init__(self, device=None, baud_rate=9600, **kwargs): """ :param device: Device path (e.g. ``/dev/ttyUSB0`` or ``/dev/ttyACM0``) :type device: str @@ -28,7 +28,7 @@ class SerialPlugin(GpioSensorPlugin): :type baud_rate: int """ - super().__init__(*args, **kwargs) + super().__init__(**kwargs) self.device = device self.baud_rate = baud_rate