.
This commit is contained in:
+17
-9
@@ -3,6 +3,7 @@ from mqtt import MqttMessage
|
|||||||
from workers.base import BaseWorker
|
from workers.base import BaseWorker
|
||||||
import logger
|
import logger
|
||||||
import traceback
|
import traceback
|
||||||
|
import time
|
||||||
|
|
||||||
REQUIREMENTS = ['pyserial']
|
REQUIREMENTS = ['pyserial']
|
||||||
_LOGGER = logger.get(__name__)
|
_LOGGER = logger.get(__name__)
|
||||||
@@ -12,7 +13,7 @@ class MsgwWorker(BaseWorker):
|
|||||||
LOGNAME = "mysensors-nrf24"
|
LOGNAME = "mysensors-nrf24"
|
||||||
|
|
||||||
def _setup(self):
|
def _setup(self):
|
||||||
_LOGGER.debug("Setting up " + LOGNAME)
|
_LOGGER.debug("Setting up " + self.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'):
|
||||||
@@ -22,13 +23,13 @@ class MsgwWorker(BaseWorker):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
def run(self, mqtt):
|
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:
|
while True:
|
||||||
try:
|
try:
|
||||||
line = self.ser.readline()
|
line = self.ser.readline()
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
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)
|
splited_line = line.decode(self.encoding).strip().split(';', 5)
|
||||||
if len(splited_line) < 5:
|
if len(splited_line) < 5:
|
||||||
raise Exception("protocol error: too few values")
|
raise Exception("protocol error: too few values")
|
||||||
@@ -44,29 +45,36 @@ class MsgwWorker(BaseWorker):
|
|||||||
if command == 3:
|
if command == 3:
|
||||||
# I_TIME
|
# I_TIME
|
||||||
if ctype == 1:
|
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")
|
resp = str.encode(splited_line[0]+";"+splited_line[1]+";3;0;1;"+timevalue+"\n")
|
||||||
self.ser.write(resp)
|
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
|
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(LOGNAME + ": Caught exception in MsgwWorker.run(): {}".format(e))
|
_LOGGER.error(self.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(LOGNAME + ": Caught exception in MsgwWorker.run(): {}".format(e))
|
_LOGGER.error(self.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(LOGNAME + ": on_command({}, {})".format(topic, value))
|
_LOGGER.debug(self.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(LOGNAME + ": Serial.write: %s" % line)
|
_LOGGER.debug(self.LOGNAME + ": Serial.write: %s" % line)
|
||||||
self.ser.write(line)
|
self.ser.write(line)
|
||||||
except Exception as e:
|
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()
|
traceback.print_exc()
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -9,5 +9,5 @@ _LOGGER = logger.get(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class Msgw2Worker(MsgwWorker):
|
class Msgw2Worker(MsgwWorker):
|
||||||
LOGNAME = "mysensors-rfm69
|
LOGNAME = "mysensors-rfm69"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user