2018-04-29 16:14:43 +02:00
|
|
|
import json
|
2018-04-29 16:18:27 +02:00
|
|
|
import serial
|
2018-07-17 23:51:30 +02:00
|
|
|
import time
|
2018-04-29 16:14:43 +02:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import Plugin, action
|
2018-07-17 00:59:02 +02:00
|
|
|
from platypush.plugins.gpio.sensor import GpioSensorPlugin
|
2018-04-08 18:16:24 +02:00
|
|
|
|
2018-04-29 16:14:43 +02:00
|
|
|
|
2018-07-17 00:59:02 +02:00
|
|
|
class SerialPlugin(GpioSensorPlugin):
|
2018-06-19 20:05:24 +02:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
some sensors connected through an Arduino. Just make sure that the code on
|
|
|
|
your serial device returns JSON values. If you're using an Arduino or any
|
|
|
|
ATMega compatible device, take a look at
|
|
|
|
https://github.com/bblanchon/ArduinoJson.
|
|
|
|
"""
|
|
|
|
|
2018-04-29 16:14:43 +02:00
|
|
|
def __init__(self, device, baud_rate=9600, *args, **kwargs):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
:param device: Device path (e.g. ``/dev/ttyUSB0`` or ``/dev/ttyACM0``)
|
|
|
|
:type device: str
|
|
|
|
|
|
|
|
:param baud_rate: Serial baud rate (default: 9600)
|
|
|
|
:type baud_rate: int
|
|
|
|
"""
|
|
|
|
|
2018-04-08 18:16:24 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2018-04-29 16:14:43 +02:00
|
|
|
self.device = device
|
|
|
|
self.baud_rate = baud_rate
|
2018-07-17 23:47:13 +02:00
|
|
|
self.serial = None
|
2018-04-29 16:14:43 +02:00
|
|
|
|
|
|
|
|
2018-06-19 20:32:19 +02:00
|
|
|
def _read_json(self, serial_port):
|
2018-06-19 20:05:24 +02:00
|
|
|
n_brackets = 0
|
|
|
|
is_escaped_ch = False
|
|
|
|
parse_start = False
|
2018-06-19 20:32:19 +02:00
|
|
|
output = bytes()
|
2018-06-19 20:05:24 +02:00
|
|
|
|
|
|
|
while True:
|
2018-06-19 20:32:19 +02:00
|
|
|
ch = serial_port.read()
|
2018-06-19 20:05:24 +02:00
|
|
|
if not ch:
|
|
|
|
break
|
|
|
|
|
|
|
|
if ch.decode() == '{' and not is_escaped_ch:
|
|
|
|
parse_start = True
|
|
|
|
n_brackets += 1
|
|
|
|
|
|
|
|
if not parse_start:
|
|
|
|
continue
|
|
|
|
|
|
|
|
output += ch
|
|
|
|
|
|
|
|
if ch.decode() == '}' and not is_escaped_ch:
|
|
|
|
n_brackets -= 1
|
|
|
|
if n_brackets == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
is_escaped_ch = ch.decode() == '\\'
|
|
|
|
|
|
|
|
return output.decode().strip()
|
|
|
|
|
2018-07-17 23:47:13 +02:00
|
|
|
def _get_serial(self, reset=False):
|
|
|
|
if self.serial:
|
|
|
|
if not reset:
|
|
|
|
return self.serial
|
|
|
|
|
|
|
|
self._close_serial()
|
|
|
|
|
|
|
|
self.serial = serial.Serial(self.device, self.baud_rate)
|
|
|
|
return self.serial
|
|
|
|
|
|
|
|
def _close_serial(self):
|
|
|
|
if self.serial:
|
|
|
|
try:
|
|
|
|
self.serial.close()
|
|
|
|
self.serial = None
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.warning('Error while closing serial communication: {}')
|
|
|
|
self.logger.exception(e)
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-07-17 00:59:02 +02:00
|
|
|
def get_measurement(self):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
Reads JSON data from the serial device and returns it as a message
|
|
|
|
"""
|
|
|
|
|
2018-06-19 20:05:24 +02:00
|
|
|
try:
|
2018-07-17 23:47:13 +02:00
|
|
|
ser = self._get_serial()
|
|
|
|
except:
|
2018-07-17 23:51:30 +02:00
|
|
|
time.sleep(1)
|
2018-07-17 23:47:13 +02:00
|
|
|
ser = self._get_serial(reset=True)
|
|
|
|
|
|
|
|
data = self._read_json(ser)
|
2018-04-29 16:14:43 +02:00
|
|
|
|
2018-06-19 20:05:24 +02:00
|
|
|
try:
|
|
|
|
data = json.loads(data)
|
|
|
|
except:
|
|
|
|
self.logger.warning('Invalid JSON message from {}: {}'.
|
|
|
|
format(self.device, data))
|
2018-04-29 16:14:43 +02:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
return data
|
2018-04-08 18:16:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|