From 53b8d957cd4896883dbbadc47420edea651ce053 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 28 Dec 2020 01:53:21 +0100 Subject: [PATCH] . --- workers/msgw.py | 26 +++++++++++++++++--------- workers/msgw2.py | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/workers/msgw.py b/workers/msgw.py index 9cb1ee8..5dc5262 100644 --- a/workers/msgw.py +++ b/workers/msgw.py @@ -3,6 +3,7 @@ from mqtt import MqttMessage from workers.base import BaseWorker import logger import traceback +import time REQUIREMENTS = ['pyserial'] _LOGGER = logger.get(__name__) @@ -12,7 +13,7 @@ class MsgwWorker(BaseWorker): LOGNAME = "mysensors-nrf24" def _setup(self): - _LOGGER.debug("Setting up " + LOGNAME) + _LOGGER.debug("Setting up " + self.LOGNAME) import serial self.ser = serial.Serial(self.port, self.baudrate, timeout=10) if not hasattr(self, 'encoding'): @@ -22,13 +23,13 @@ class MsgwWorker(BaseWorker): return [] def run(self, mqtt): - _LOGGER.debug("Starting " + LOGNAME + " at: %s" % self.ser.name) + _LOGGER.debug("Starting " + self.LOGNAME + " at: %s" % self.ser.name) while True: try: line = self.ser.readline() if not line: continue - _LOGGER.debug(LOGNAME + ": Serial.read: %s" % line) + _LOGGER.debug(self.LOGNAME + ": Serial.read: %s" % line) splited_line = line.decode(self.encoding).strip().split(';', 5) if len(splited_line) < 5: raise Exception("protocol error: too few values") @@ -44,29 +45,36 @@ class MsgwWorker(BaseWorker): if command == 3: # I_TIME if ctype == 1: - timevalue = str(int(time.mktime(time.localtime()))) + timevalue = str(60*60+int(time.mktime(time.localtime()))) resp = str.encode(splited_line[0]+";"+splited_line[1]+";3;0;1;"+timevalue+"\n") self.ser.write(resp) + _LOGGER.error(self.LOGNAME + ": time request received, sending back %s" % resp) + continue + # I_CONFIG + elif ctype == 6: + resp = str.encode(splited_line[0]+";"+splited_line[1]+";3;0;6;M\n") + self.ser.write(resp) + _LOGGER.error(self.LOGNAME + ": config request received, sending back %s" % resp) continue mqtt.publish([MqttMessage(topic=topic, payload=payload)]) except Exception as e: - _LOGGER.error(LOGNAME + ": Caught exception in MsgwWorker.run(): {}".format(e)) + _LOGGER.error(self.LOGNAME + ": Caught exception in MsgwWorker.run(): {}".format(e)) traceback.print_exc(); except serial.serialutil.SerialException as e: - _LOGGER.error(LOGNAME + ": Caught exception in MsgwWorker.run(): {}".format(e)) + _LOGGER.error(self.LOGNAME + ": Caught exception in MsgwWorker.run(): {}".format(e)) traceback.print_exc(); self.ser = serial.Serial(self.port, self.baudrate, timeout=10) def on_command(self, topic, value): - _LOGGER.debug(LOGNAME + ": on_command({}, {})".format(topic, value)) + _LOGGER.debug(self.LOGNAME + ": on_command({}, {})".format(topic, value)) try: splited_topic = topic.split('/') line = str.encode(splited_topic[-5]+";"+splited_topic[-4]+";"+splited_topic[-3]+";"+splited_topic[-2]+";"+splited_topic[-1]+";")+value+b'\n' - _LOGGER.debug(LOGNAME + ": Serial.write: %s" % line) + _LOGGER.debug(self.LOGNAME + ": Serial.write: %s" % line) self.ser.write(line) except Exception as e: - _LOGGER.error(LOGNAME + ": Caught exception in MsgwWorker.on_command(): {}".format(e)) + _LOGGER.error(self.LOGNAME + ": Caught exception in MsgwWorker.on_command(): {}".format(e)) traceback.print_exc() return [] diff --git a/workers/msgw2.py b/workers/msgw2.py index 30f67f6..af8d52f 100644 --- a/workers/msgw2.py +++ b/workers/msgw2.py @@ -9,5 +9,5 @@ _LOGGER = logger.get(__name__) class Msgw2Worker(MsgwWorker): - LOGNAME = "mysensors-rfm69 + LOGNAME = "mysensors-rfm69"