diff --git a/platypush/plugins/arduino.py b/platypush/plugins/arduino.py index 978edf65..844aabf0 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 6ab5c432..3bf82ee0 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 00000000..eccc8ef4 --- /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 5b667092..9e3a34e3 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