Added serial device backend

This commit is contained in:
Fabio Manganiello 2018-04-07 03:08:52 +02:00
parent 2be2677963
commit 8ae22726ed
2 changed files with 51 additions and 0 deletions

View file

@ -0,0 +1,41 @@
import logging
import serial
import time
from platypush.backend import Backend
from platypush.message.event.serial import SerialDataEvent
class SerialBackend(Backend):
state = None
def __init__(self, device, baud_rate=9600, **kwargs):
super().__init__(**kwargs)
self.device = device
self.baud_rate = baud_rate
self.serial = None
def send_message(self, msg):
pass
def get_state(self):
return self.state
def run(self):
super().run()
self.serial = serial.Serial(self.device, self.baud_rate)
prev_value = None
logging.info('Initialized serial backend on device {}'.format(self.device))
while not self.should_stop():
value = self.serial.readline().decode('utf-8').strip()
if prev_value is None or value != prev_value:
self.bus.post(SerialDataEvent(data=value, device=self.device))
prev_value = value
# vim:sw=4:ts=4:et:

View file

@ -0,0 +1,10 @@
from platypush.message.event import Event
class SerialDataEvent(Event):
def __init__(self, data, device=None, *args, **kwargs):
super().__init__(data=data, device=device, *args, **kwargs)
# vim:sw=4:ts=4:et: