diff --git a/platypush/backend/serial/__init__.py b/platypush/backend/serial/__init__.py new file mode 100644 index 0000000000..225310844d --- /dev/null +++ b/platypush/backend/serial/__init__.py @@ -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: + diff --git a/platypush/message/event/serial/__init__.py b/platypush/message/event/serial/__init__.py new file mode 100644 index 0000000000..354f19900f --- /dev/null +++ b/platypush/message/event/serial/__init__.py @@ -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: +