Improved MySensors-Worker.
This commit is contained in:
+28
-11
@@ -9,9 +9,10 @@ _LOGGER = logger.get(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class MsgwWorker(BaseWorker):
|
class MsgwWorker(BaseWorker):
|
||||||
|
LOGNAME = "mysensors-nrf24"
|
||||||
|
|
||||||
def _setup(self):
|
def _setup(self):
|
||||||
_LOGGER.debug("Setting up mysensors-mqtt")
|
_LOGGER.debug("Setting up " + LOGNAME)
|
||||||
import serial
|
import serial
|
||||||
self.ser = serial.Serial(self.port, self.baudrate, timeout=10)
|
self.ser = serial.Serial(self.port, self.baudrate, timeout=10)
|
||||||
if not hasattr(self, 'encoding'):
|
if not hasattr(self, 'encoding'):
|
||||||
@@ -21,35 +22,51 @@ class MsgwWorker(BaseWorker):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
def run(self, mqtt):
|
def run(self, mqtt):
|
||||||
_LOGGER.debug("Starting mysensors-mqtt at: %s" % self.ser.name)
|
_LOGGER.debug("Starting " + LOGNAME + " at: %s" % self.ser.name)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
line = self.ser.readline()
|
line = self.ser.readline()
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
splited_line = self.format_topic(line.decode(self.encoding).strip()).split(';')
|
_LOGGER.debug(LOGNAME + ": Serial.read: %s" % line)
|
||||||
_LOGGER.debug("MSGW.serial.read: %s" % line)
|
splited_line = line.decode(self.encoding).strip().split(';', 5)
|
||||||
topic = '/'.join(splited_line[0:-1])
|
if len(splited_line) < 5:
|
||||||
payload = ''.join(splited_line[-1])
|
raise Exception("protocol error: too few values")
|
||||||
|
# node-id ; child-sensor-id ; command ; ack ; type ; payload
|
||||||
|
#nodeid = int(splited_line[0])
|
||||||
|
#childid = int(splited_line[1])
|
||||||
|
command = int(splited_line[2])
|
||||||
|
#ack = int(splited_line[3])
|
||||||
|
ctype = int(splited_line[4])
|
||||||
|
payload = splited_line[5] if len(splited_line) > 5 else ''
|
||||||
|
topic = self.format_topic(*splited_line[0:5])
|
||||||
|
# Internal
|
||||||
|
if command == 3:
|
||||||
|
# I_TIME
|
||||||
|
if ctype == 1:
|
||||||
|
timevalue = str(int(time.mktime(time.localtime())))
|
||||||
|
resp = str.encode(splited_line[0]+";"+splited_line[1]+";3;0;1;"+timevalue+"\n")
|
||||||
|
self.ser.write(resp)
|
||||||
|
continue
|
||||||
mqtt.publish([MqttMessage(topic=topic, payload=payload)])
|
mqtt.publish([MqttMessage(topic=topic, payload=payload)])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.error("Caught exception in MsgwWorker.run(): {}".format(e))
|
_LOGGER.error(LOGNAME + ": Caught exception in MsgwWorker.run(): {}".format(e))
|
||||||
traceback.print_exc();
|
traceback.print_exc();
|
||||||
except serial.serialutil.SerialException as e:
|
except serial.serialutil.SerialException as e:
|
||||||
_LOGGER.error("Caught exception in MsgwWorker.run(): {}".format(e))
|
_LOGGER.error(LOGNAME + ": Caught exception in MsgwWorker.run(): {}".format(e))
|
||||||
traceback.print_exc();
|
traceback.print_exc();
|
||||||
self.ser = serial.Serial(self.port, self.baudrate, timeout=10)
|
self.ser = serial.Serial(self.port, self.baudrate, timeout=10)
|
||||||
|
|
||||||
|
|
||||||
def on_command(self, topic, value):
|
def on_command(self, topic, value):
|
||||||
_LOGGER.debug("MsgwWorker.on_command({}, {})".format(topic, value))
|
_LOGGER.debug(LOGNAME + ": on_command({}, {})".format(topic, value))
|
||||||
try:
|
try:
|
||||||
splited_topic = topic.split('/')
|
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'
|
line = str.encode(splited_topic[-5]+";"+splited_topic[-4]+";"+splited_topic[-3]+";"+splited_topic[-2]+";"+splited_topic[-1]+";")+value+b'\n'
|
||||||
_LOGGER.debug("MSGW.serial.write: %s" % line)
|
_LOGGER.debug(LOGNAME + ": Serial.write: %s" % line)
|
||||||
self.ser.write(line)
|
self.ser.write(line)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.error("Caught exception in MsgwWorker.on_command(): {}".format(e))
|
_LOGGER.error(LOGNAME + ": Caught exception in MsgwWorker.on_command(): {}".format(e))
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|||||||
+3
-45
@@ -1,6 +1,6 @@
|
|||||||
from mqtt import MqttMessage
|
from mqtt import MqttMessage
|
||||||
|
|
||||||
from workers.base import BaseWorker
|
from workers.msgw import MsgwWorker
|
||||||
import logger
|
import logger
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
@@ -8,48 +8,6 @@ REQUIREMENTS = ['pyserial']
|
|||||||
_LOGGER = logger.get(__name__)
|
_LOGGER = logger.get(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Msgw2Worker(BaseWorker):
|
class Msgw2Worker(MsgwWorker):
|
||||||
|
LOGNAME = "mysensors-rfm69
|
||||||
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