This commit is contained in:
2020-12-28 01:53:21 +01:00
parent 53a29b138e
commit 53b8d957cd
2 changed files with 18 additions and 10 deletions
+17 -9
View File
@@ -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 []
+1 -1
View File
@@ -9,5 +9,5 @@ _LOGGER = logger.get(__name__)
class Msgw2Worker(MsgwWorker):
LOGNAME = "mysensors-rfm69
LOGNAME = "mysensors-rfm69"