.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
from mqtt import MqttMessage
|
||||
|
||||
from workers.base import BaseWorker
|
||||
import logger
|
||||
import traceback
|
||||
|
||||
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 []
|
||||
|
||||
Reference in New Issue
Block a user