From 8ae22726ed6a76aa8d21c92a3ada0f47b0de00da Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 7 Apr 2018 03:08:52 +0200 Subject: [PATCH] Added serial device backend --- platypush/backend/serial/__init__.py | 41 ++++++++++++++++++++++ platypush/message/event/serial/__init__.py | 10 ++++++ 2 files changed, 51 insertions(+) create mode 100644 platypush/backend/serial/__init__.py create mode 100644 platypush/message/event/serial/__init__.py diff --git a/platypush/backend/serial/__init__.py b/platypush/backend/serial/__init__.py new file mode 100644 index 00000000..22531084 --- /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 00000000..354f1990 --- /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: +