From d0c54a086b9ea67c067193b51dd8f16e8e75f454 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Thu, 16 Dec 2021 21:59:53 +0100 Subject: Fix some more python3 issues, add stats command --- adsl.py | 28 +++++++++++++++++++++------- requirements.txt | 2 +- serialrx.py | 40 ++++++++++++++++++++++------------------ 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/adsl.py b/adsl.py index 414c4b2..cd37fae 100644 --- a/adsl.py +++ b/adsl.py @@ -151,6 +151,7 @@ class ADSL(threading.Thread): def __init__(self, ser): threading.Thread.__init__(self) self.monitor = Monitor(ser) + self._ser = ser def run(self): @@ -168,7 +169,7 @@ class ADSL(threading.Thread): offset = None - bot.send_message(config.TELEGRAM_GROUP, '\xe2\x84\xb9 Hello ! I have been started, so everything has been reset on my side.').wait() + bot.send_message(config.TELEGRAM_GROUP, b'\xe2\x84\xb9 Hello ! I have been started, so everything has been reset on my side.'.decode()).wait() while True: @@ -177,9 +178,7 @@ class ADSL(threading.Thread): offset = updates[0].update_id + 1 try: - print(updates[0].message.chat.id) if int(updates[0].message.chat.id) == int(config.TELEGRAM_GROUP): - if updates[0].message.text.startswith('/status'): response = "Here is the current status:\n\n" @@ -203,9 +202,24 @@ class ADSL(threading.Thread): bot.send_message(config.TELEGRAM_GROUP, response).wait() - if updates[0].message.text.startswith('/reboot'): + elif updates[0].message.text.startswith('/stats'): + t_now = time.time() + values = ser.get_parsed_values() + + stats_lines = ["Stats:"] + + for k in values: + value, ts = values[k] + since = t_now - ts + stats_lines.append(f"{k}: {value} since {since}s") + bot.send_message(config.TELEGRAM_GROUP, "\n".join(stats_lines)).wait() + + + elif updates[0].message.text.startswith('/reboot'): os.system(config.TELEGRAM_REBOOT_COMMAND) - bot.send_message(config.TELEGRAM_GROUP, '\xe2\x84\xb9 I issued a reboot command. I hope everything is ok.').wait() + bot.send_message(config.TELEGRAM_GROUP, b'\xe2\x84\xb9 I issued a reboot command. I hope everything is ok.'.decode()).wait() + else: + print(f"Ignore chat ID {updates[0].message.chat.id}") except: pass @@ -213,11 +227,11 @@ class ADSL(threading.Thread): for alarm in new_alarms: if alarm not in alarms: - bot.send_message(config.TELEGRAM_GROUP, '\xe2\x9a\xa0 Problem \xe2\x9a\xa0\nSorry to bother you, but I think there is a problem with the glutt-o-matique: \n\n{}'.format(alarm)).wait() + bot.send_message(config.TELEGRAM_GROUP, b'\xe2\x9a\xa0 Problem \xe2\x9a\xa0\nSorry to bother you, but I think there is a problem with the glutt-o-matique: \n\n{}'.decode().format(alarm)).wait() for old_alarm in alarms: if old_alarm not in new_alarms: - bot.send_message(config.TELEGRAM_GROUP, '\xe2\x9c\x85 Problem fixed \xe2\x9c\x85\nThe following problem is not anymore a problem with the glutt-o-matique:\n\n{}'.format(old_alarm)).wait() + bot.send_message(config.TELEGRAM_GROUP, b'\xe2\x9c\x85 Problem fixed \xe2\x9c\x85\nThe following problem is not anymore a problem with the glutt-o-matique:\n\n{}'.decode().format(old_alarm)).wait() alarms = new_alarms diff --git a/requirements.txt b/requirements.txt index 0f9dede..aff7d58 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -click==6.6 +gevent-websocket==0.10.1 Flask==1.1.2 gevent==20.6.2 greenlet==0.4.16 diff --git a/serialrx.py b/serialrx.py index 4e7ccf6..263ed1b 100644 --- a/serialrx.py +++ b/serialrx.py @@ -135,25 +135,29 @@ class SerialRX(threading.Thread): self.line_accumulator.append(databyte) if databyte == b"\n": - line = b"".join(self.line_accumulator).decode() - self._parser.parse_message(line) - self.data_lock.acquire() try: - for queue in self.clients: - queue.append(line) - - if len(queue) > config.LINES_TO_KEEP: - queue.popleft() - - self.last_lines.append(line) - - if len(self.last_lines) > config.LAST_LINE_TO_KEEP: - self.last_lines.pop(0) - except: - raise - finally: - self.data_lock.release() - self.line_accumulator = [] + line = b"".join(self.line_accumulator).decode('ascii') + self._parser.parse_message(line) + self.data_lock.acquire() + try: + for queue in self.clients: + queue.append(line) + + if len(queue) > config.LINES_TO_KEEP: + queue.popleft() + + self.last_lines.append(line) + + if len(self.last_lines) > config.LAST_LINE_TO_KEEP: + self.last_lines.pop(0) + except: + raise + finally: + self.data_lock.release() + self.line_accumulator = [] + except UnicodeDecodeError: + print(f"Ignoring line with invalid ASCII bytes {self.line_accumulator}") + self.line_accumulator = [] def stop(self): self.event_stop.set() -- cgit v1.2.3