platypush/platypush/plugins/serial/__init__.py

31 lines
592 B
Python
Raw Normal View History

import json
2018-04-29 16:18:27 +02:00
import serial
from platypush.message.response import Response
from .. import Plugin
class SerialPlugin(Plugin):
def __init__(self, device, baud_rate=9600, *args, **kwargs):
super().__init__(*args, **kwargs)
self.device = device
self.baud_rate = baud_rate
def get_data(self):
2018-04-29 16:19:26 +02:00
ser = serial.Serial(self.device, self.baud_rate)
2018-04-29 16:19:26 +02:00
try: data = ser.readline().decode('utf-8').strip()
finally: ser.close()
try: data = json.loads(data)
except: pass
2018-04-29 16:38:05 +02:00
return Response(output=data)
# vim:sw=4:ts=4:et: