forked from platypush/platypush
Exposing serial data functionally through a plugin
This commit is contained in:
parent
8ae22726ed
commit
2bf4ff136b
2 changed files with 37 additions and 11 deletions
|
@ -1,40 +1,50 @@
|
||||||
import logging
|
import logging
|
||||||
import serial
|
import serial
|
||||||
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from platypush.backend import Backend
|
from platypush.backend import Backend
|
||||||
from platypush.message.event.serial import SerialDataEvent
|
from platypush.message.event.serial import SerialDataEvent
|
||||||
|
|
||||||
|
|
||||||
class SerialBackend(Backend):
|
class SerialBackend(Backend):
|
||||||
state = None
|
|
||||||
|
|
||||||
def __init__(self, device, baud_rate=9600, **kwargs):
|
def __init__(self, device, baud_rate=9600, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
self.device = device
|
self.device = device
|
||||||
self.baud_rate = baud_rate
|
self.baud_rate = baud_rate
|
||||||
self.serial = None
|
self.serial = None
|
||||||
|
self.data = None
|
||||||
|
self.tmp_file = tempfile.TemporaryFile()
|
||||||
|
|
||||||
def send_message(self, msg):
|
def send_message(self, msg):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_state(self):
|
def get_data(self):
|
||||||
return self.state
|
self.tmp_file.seek(0)
|
||||||
|
data = self.tmp_file.read()
|
||||||
|
self.tmp_file.seek(0)
|
||||||
|
|
||||||
|
return data.decode('utf-8')
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
super().run()
|
super().run()
|
||||||
|
|
||||||
self.serial = serial.Serial(self.device, self.baud_rate)
|
self.serial = serial.Serial(self.device, self.baud_rate)
|
||||||
prev_value = None
|
|
||||||
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():
|
||||||
value = self.serial.readline().decode('utf-8').strip()
|
new_data = self.serial.readline().decode('utf-8').strip()
|
||||||
if prev_value is None or value != prev_value:
|
if self.data is None or self.data != new_data:
|
||||||
self.bus.post(SerialDataEvent(data=value, device=self.device))
|
self.bus.post(SerialDataEvent(data=new_data, device=self.device))
|
||||||
|
|
||||||
prev_value = value
|
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:
|
||||||
|
|
16
platypush/plugins/serial/__init__.py
Normal file
16
platypush/plugins/serial/__init__.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
from platypush.context import get_backend
|
||||||
|
from platypush.message.response import Response
|
||||||
|
|
||||||
|
from .. import Plugin
|
||||||
|
|
||||||
|
class SerialPlugin(Plugin):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def get_data(self):
|
||||||
|
backend = get_backend('serial')
|
||||||
|
return Response(output=backend.get_data())
|
||||||
|
|
||||||
|
|
||||||
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
Loading…
Reference in a new issue