Improved MySensors-Worker.

This commit is contained in:
2020-12-28 00:01:34 +01:00
parent 20abf8e8c0
commit 53a29b138e
2 changed files with 32 additions and 57 deletions
+3 -45
View File
@@ -1,6 +1,6 @@
from mqtt import MqttMessage
from workers.base import BaseWorker
from workers.msgw import MsgwWorker
import logger
import traceback
@@ -8,48 +8,6 @@ REQUIREMENTS = ['pyserial']
_LOGGER = logger.get(__name__)
class Msgw2Worker(BaseWorker):
def _setup(self):
_LOGGER.debug("Setting up mysensors-2-mqtt")
import serial
self.ser = serial.Serial(self.port, self.baudrate, timeout=10)
if not hasattr(self, 'encoding'):
self.encoding = 'utf-8'
def config(self):
return []
def run(self, mqtt):
_LOGGER.debug("Starting mysensors-2-mqtt at: %s" % self.ser.name)
while True:
try:
line = self.ser.readline()
if not line:
continue
splited_line = self.format_topic(line.decode(self.encoding).strip()).split(';')
_LOGGER.debug("MSGW2.serial.read: %s" % line)
topic = '/'.join(splited_line[0:-1])
payload = ''.join(splited_line[-1])
mqtt.publish([MqttMessage(topic=topic, payload=payload)])
except Exception as e:
_LOGGER.error("Caught exception in Msgw2Worker.run(): {}".format(e))
traceback.print_exc();
except serial.serialutil.SerialException as e:
_LOGGER.error("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("MsgwWorker2.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.info("MSGW2.serial.write: %s" % line)
self.ser.write(line)
except Exception as e:
_LOGGER.error("Caught exception in Msgw2Worker.on_command(): {}".format(e))
traceback.print_exc()
return []
class Msgw2Worker(MsgwWorker):
LOGNAME = "mysensors-rfm69