forked from platypush/platypush
Refactored serial, the entry point for querying serial devices should be the serial plugin, the backend should only be a client. Also, if the serial output is a JSON, parse it
This commit is contained in:
parent
514eb3d4e4
commit
a907c9ad63
2 changed files with 26 additions and 29 deletions
|
@ -1,30 +1,21 @@
|
||||||
import logging
|
import logging
|
||||||
import serial
|
import serial
|
||||||
import tempfile
|
|
||||||
import time
|
|
||||||
|
|
||||||
from platypush.backend import Backend
|
from platypush.backend import Backend
|
||||||
|
from platypush.context import get_plugin
|
||||||
from platypush.message.event.serial import SerialDataEvent
|
from platypush.message.event.serial import SerialDataEvent
|
||||||
|
|
||||||
class SerialBackend(Backend):
|
class SerialBackend(Backend):
|
||||||
def __init__(self, device, baud_rate=9600, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
self.device = device
|
|
||||||
self.baud_rate = baud_rate
|
|
||||||
self.serial = None
|
|
||||||
self.data = None
|
self.data = None
|
||||||
self.tmp_file = tempfile.TemporaryFile()
|
|
||||||
|
|
||||||
def send_message(self, msg):
|
def send_message(self, msg):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_data(self):
|
def get_data(self):
|
||||||
self.tmp_file.seek(0)
|
plugin = get_plugin('serial')
|
||||||
data = self.tmp_file.read()
|
return plugin.get_data()
|
||||||
self.tmp_file.seek(0)
|
|
||||||
|
|
||||||
return data.decode('utf-8')
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
super().run()
|
super().run()
|
||||||
|
@ -32,19 +23,12 @@ class SerialBackend(Backend):
|
||||||
self.serial = serial.Serial(self.device, self.baud_rate)
|
self.serial = serial.Serial(self.device, self.baud_rate)
|
||||||
logging.info('Initialized serial backend on device {}'.format(self.device))
|
logging.info('Initialized serial backend on device {}'.format(self.device))
|
||||||
|
|
||||||
try:
|
while not self.should_stop():
|
||||||
while not self.should_stop():
|
new_data = self.get_data()
|
||||||
new_data = self.serial.readline().decode('utf-8').strip()
|
if self.data is None or self.data != new_data:
|
||||||
if self.data is None or self.data != new_data:
|
self.bus.post(SerialDataEvent(data=new_data, device=self.device))
|
||||||
self.bus.post(SerialDataEvent(data=new_data, device=self.device))
|
|
||||||
|
|
||||||
self.data = new_data
|
self.data = new_data
|
||||||
|
|
||||||
self.tmp_file.seek(0)
|
|
||||||
self.tmp_file.write(self.data.encode('utf-8'))
|
|
||||||
self.tmp_file.seek(0)
|
|
||||||
finally:
|
|
||||||
self.tmp_file.close()
|
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
|
@ -1,15 +1,28 @@
|
||||||
from platypush.context import get_backend
|
import json
|
||||||
|
|
||||||
from platypush.message.response import Response
|
from platypush.message.response import Response
|
||||||
|
|
||||||
from .. import Plugin
|
from .. import Plugin
|
||||||
|
|
||||||
|
|
||||||
class SerialPlugin(Plugin):
|
class SerialPlugin(Plugin):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, device, baud_rate=9600, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
self.device = device
|
||||||
|
self.baud_rate = baud_rate
|
||||||
|
|
||||||
|
|
||||||
def get_data(self):
|
def get_data(self):
|
||||||
backend = get_backend('serial')
|
serial = serial.Serial(self.device, self.baud_rate)
|
||||||
return Response(output=backend.get_data())
|
|
||||||
|
try: data = serial.readline().decode('utf-8').strip()
|
||||||
|
finally: serial.close()
|
||||||
|
|
||||||
|
try: data = json.loads(data)
|
||||||
|
except: pass
|
||||||
|
|
||||||
|
return Response(data)
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
Loading…
Reference in a new issue