2018-04-29 16:14:43 +02:00
|
|
|
import json
|
|
|
|
|
2018-04-08 18:16:24 +02:00
|
|
|
from platypush.message.response import Response
|
|
|
|
|
|
|
|
from .. import Plugin
|
|
|
|
|
2018-04-29 16:14:43 +02:00
|
|
|
|
2018-04-08 18:16:24 +02:00
|
|
|
class SerialPlugin(Plugin):
|
2018-04-29 16:14:43 +02:00
|
|
|
def __init__(self, device, baud_rate=9600, *args, **kwargs):
|
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-04-08 18:16:24 +02:00
|
|
|
def get_data(self):
|
2018-04-29 16:14:43 +02:00
|
|
|
serial = serial.Serial(self.device, self.baud_rate)
|
|
|
|
|
|
|
|
try: data = serial.readline().decode('utf-8').strip()
|
|
|
|
finally: serial.close()
|
|
|
|
|
|
|
|
try: data = json.loads(data)
|
|
|
|
except: pass
|
|
|
|
|
|
|
|
return Response(data)
|
2018-04-08 18:16:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|