get_data/get_measurement/close should be implemented in a base SensorPlugin

class, not specifically in GpioSensorPlugin
This commit is contained in:
Fabio Manganiello 2020-01-08 00:20:00 +01:00
parent aa6bf5379a
commit 6738ff832a
4 changed files with 65 additions and 45 deletions

View file

@ -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 <https://www.arduino.cc/en/reference/firmata>`_ protocol.

View file

@ -1,43 +1,9 @@
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):
class GpioSensorPlugin(ABC, SensorPlugin):
pass

View file

@ -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:

View file

@ -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