|
@@ -51,7 +51,9 @@ mqtt_port = config['mqtt'].getint('port')
|
|
|
mqtt_user = config['mqtt'].get('user')
|
|
|
mqtt_password = config['mqtt'].get('password')
|
|
|
|
|
|
-TX_interface_prefer = config['main'].get('TX_interface_prefer')
|
|
|
+TX_interface_prefer = config['cul'].get('TX_interface_prefer')
|
|
|
+if TX_interface_prefer is None:
|
|
|
+ TX_interface_prefer = config['main'].get('TX_interface_prefer')
|
|
|
|
|
|
repeat_received_commands = False
|
|
|
|
|
@@ -189,7 +191,7 @@ try:
|
|
|
if debug:
|
|
|
log_write(str(key) + "->" + str(value))
|
|
|
if value.startswith("i"):
|
|
|
-
|
|
|
+
|
|
|
if value in RXcodesToDevFunction_IT.keys():
|
|
|
log_write("")
|
|
|
log_write("")
|
|
@@ -355,25 +357,30 @@ def on_message(client, userdata, msg):
|
|
|
mqttc.publish(res[topic], res[payload], qos=0, retain=False)
|
|
|
|
|
|
|
|
|
-def publish_device_statusupdate(device, cmd):
|
|
|
+def publish_device_statusupdate(device, cmd, value):
|
|
|
if device in devdata.keys():
|
|
|
if 'statTopic' in devdata[device].keys():
|
|
|
statTopic = devdata[device].get('statTopic')
|
|
|
- if verbose: log_write("MQTT publish: '" + cmd + "' -> '" + statTopic + "'")
|
|
|
- mqttc.publish(statTopic, cmd, qos=0, retain=False)
|
|
|
+
|
|
|
+ if value is not None and cmd == "%VALUE%":
|
|
|
+ if verbose: log_write(" MQTT publish: '" + value + "' -> '" + statTopic + "'")
|
|
|
+ mqttc.publish(statTopic, value, qos=0, retain=False)
|
|
|
+ else:
|
|
|
+ if verbose: log_write(" MQTT publish: '" + cmd + "' -> '" + statTopic + "'")
|
|
|
+ mqttc.publish(statTopic, cmd, qos=0, retain=False)
|
|
|
|
|
|
if 'add_statTopics_on' in devdata[device].keys():
|
|
|
- if verbose: log_write(" MQTT publish add_statTopics_on:")
|
|
|
+ if verbose: log_write(" MQTT publish add_statTopics_on:")
|
|
|
for res in devdata[device].get('add_statTopics_on'):
|
|
|
if 'on_payload' in res and 'topic' in res and 'payload' in res:
|
|
|
if cmd == res['on_payload']:
|
|
|
- if verbose: log_write(" on '" + res['on_payload'] + "' -> publish '" + res['payload'] + "' on topic '" + res['topic'] + "'")
|
|
|
+ if verbose: log_write(" on '" + res['on_payload'] + "' -> publish '" + res['payload'] + "' on topic '" + res['topic'] + "'")
|
|
|
mqttc.publish(res['topic'], res['payload'], qos=0, retain=False)
|
|
|
|
|
|
if 'add_statTopics' in devdata[device].keys():
|
|
|
- if verbose: log_write(" MQTT publish on add_statTopics:")
|
|
|
+ if verbose: log_write(" MQTT publish on add_statTopics:")
|
|
|
for res in devdata[device]['add_statTopics']:
|
|
|
- if verbose: log_write(" '" + cmd + "' -> '" + res + "'")
|
|
|
+ if verbose: log_write(" '" + cmd + "' -> '" + res + "'")
|
|
|
mqttc.publish(res, cmd, qos=0, retain=False)
|
|
|
|
|
|
|
|
@@ -381,58 +388,72 @@ def publish_device_statusupdate(device, cmd):
|
|
|
def parseRXCode(rx_code, source_cul):
|
|
|
receivedForDevice = None
|
|
|
receivedCmnd = None
|
|
|
+ receivedValue = None
|
|
|
|
|
|
if rx_code.startswith("i"):
|
|
|
-
|
|
|
- if debug: log_write("INTERTECHNO PROTOCOL")
|
|
|
+
|
|
|
+ if debug: log_write(" PROTOCOL: Intertechno")
|
|
|
sucessfullyParsedITCode = False
|
|
|
|
|
|
|
|
|
if rx_code in RXcodesToDevFunction_IT:
|
|
|
+ if debug: log_write(" code found in device config")
|
|
|
receivedForDevice = RXcodesToDevFunction_IT[rx_code][0]
|
|
|
receivedCmnd = RXcodesToDevFunction_IT[rx_code][1]
|
|
|
sucessfullyParsedITCode = True
|
|
|
- if debug: log_write("DEV: " + receivedForDevice + ", CMD: " + receivedCmnd + ", RX: " + rx_code)
|
|
|
+ if debug: log_write(" DEV: " + receivedForDevice + ", CMD: " + receivedCmnd + ", RX: " + rx_code)
|
|
|
if verbose:
|
|
|
- log_write("")
|
|
|
- log_write("CUL '" + source_cul + "' received '" + rx_code + "' => DEV: " + receivedForDevice + ", CMD: " + receivedCmnd)
|
|
|
+
|
|
|
+ log_write(" CUL '" + source_cul + "' received '" + rx_code + "' => DEV: " + receivedForDevice + ", CMD: " + receivedCmnd)
|
|
|
|
|
|
-
|
|
|
- rx_code = rx_code[0:-2]
|
|
|
- if not sucessfullyParsedITCode and rx_code in RXcodesToDevFunction_IT:
|
|
|
- receivedForDevice = RXcodesToDevFunction_IT[rx_code][0]
|
|
|
- receivedCmnd = RXcodesToDevFunction_IT[rx_code][1]
|
|
|
+
|
|
|
+
|
|
|
+ rx_code_stripped = rx_code[0:-2]
|
|
|
+ if not sucessfullyParsedITCode and rx_code_stripped in RXcodesToDevFunction_IT:
|
|
|
+ if debug: log_write(" code found in device config with last 16 bit stripped")
|
|
|
+ receivedForDevice = RXcodesToDevFunction_IT[rx_code_stripped][0]
|
|
|
+ receivedCmnd = RXcodesToDevFunction_IT[rx_code_stripped][1]
|
|
|
+
|
|
|
+ receivedValue = rx_code[-2:]
|
|
|
+
|
|
|
+
|
|
|
+ if "convertValueToDecimal" in devdata[receivedForDevice]:
|
|
|
+ if devdata[receivedForDevice]["convertValueToDecimal"] == True:
|
|
|
+ if debug: log_write(" converting value to decimal")
|
|
|
+ receivedValue = str(int(receivedValue, base=16))
|
|
|
+
|
|
|
sucessfullyParsedITCode = True
|
|
|
- if debug: log_write("DEV: " + receivedForDevice + ", CMD: " + receivedCmnd + ", RX: " + rx_code)
|
|
|
+ if debug: log_write(" DEV: " + receivedForDevice + ", CMD: " + receivedCmnd + ", VALUE: " + receivedValue + ", RX: " + rx_code_stripped)
|
|
|
if verbose:
|
|
|
- log_write("")
|
|
|
- log_write("CUL '" + source_cul + "' received '" + rx_code + "' => DEV: " + receivedForDevice + ", CMD: " + receivedCmnd)
|
|
|
+
|
|
|
+ log_write(" CUL '" + source_cul + "' received '" + rx_code_stripped + "' => DEV: " + receivedForDevice + ", CMD: " + receivedCmnd + ", VALUE: " + receivedValue)
|
|
|
|
|
|
-
|
|
|
+
|
|
|
if not sucessfullyParsedITCode:
|
|
|
- receivedForDevice, receivedCmnd = decodeInterTechnoRX(rx_code)
|
|
|
+ if debug: log_write(" code parsed as 'default/cheap' Intertechno code")
|
|
|
+ receivedForDevice, receivedCmnd = decodeInterTechnoRX(rx_code_stripped)
|
|
|
if debug:
|
|
|
log_write(str(receivedForDevice) + ", " + str(receivedCmnd))
|
|
|
|
|
|
else:
|
|
|
|
|
|
- if debug: log_write("OTHER/RAW PROTOCOL")
|
|
|
+ if debug: log_write(" PROTOCOL: OTHER/RAW")
|
|
|
if rx_code in RXcodesToDevFunction_RAW:
|
|
|
receivedForDevice = RXcodesToDevFunction_RAW[rx_code][0]
|
|
|
receivedCmnd = RXcodesToDevFunction_RAW[rx_code][1]
|
|
|
- if debug: log_write("DEV: " + receivedForDevice + ", CMD: " + receivedCmnd + ", RX: " + rx_code)
|
|
|
+ if debug: log_write(" DEV: " + receivedForDevice + ", CMD: " + receivedCmnd + ", RX: " + rx_code)
|
|
|
if verbose:
|
|
|
- log_write("")
|
|
|
- log_write("CUL '" + source_cul + "' received '" + rx_code + "' => DEV: " + receivedForDevice + ", CMD: " + receivedCmnd)
|
|
|
+
|
|
|
+ log_write(" CUL '" + source_cul + "' received '" + rx_code + "' => DEV: " + receivedForDevice + ", CMD: " + receivedCmnd)
|
|
|
|
|
|
- if debug:
|
|
|
- log_write("DEV: " + str(receivedForDevice) + ", CMD: " + str(receivedCmnd) + ", RX: " + rx_code)
|
|
|
+
|
|
|
+
|
|
|
|
|
|
if receivedForDevice != None and receivedCmnd != None and receivedForDevice != False and receivedCmnd != False:
|
|
|
- publish_device_statusupdate(receivedForDevice, receivedCmnd)
|
|
|
+ publish_device_statusupdate(receivedForDevice, receivedCmnd, receivedValue)
|
|
|
|
|
|
def decodeInterTechnoRX(rx_code):
|
|
|
-
|
|
|
+
|
|
|
_housecode = None
|
|
|
_devaddr = None
|
|
|
_command = None
|
|
@@ -549,11 +570,11 @@ def encodeInterTechnoRX(itname, cmd):
|
|
|
return _rxcode
|
|
|
|
|
|
else:
|
|
|
- if debug: log_write("unknown or invalid IT code '" + rx_code + "'")
|
|
|
+ if debug: log_write(" unknown or invalid IT code '" + rx_code + "'")
|
|
|
return False
|
|
|
|
|
|
else:
|
|
|
- if debug: log_write("unknown or invalid IT code '" + rx_code + "'")
|
|
|
+ if debug: log_write(" unknown or invalid IT code '" + rx_code + "'")
|
|
|
|
|
|
def cul_received(payload, source_cul):
|
|
|
global lastReceivedTime, lastReceivedMaxAge
|
|
@@ -561,27 +582,27 @@ def cul_received(payload, source_cul):
|
|
|
if payload[:2] == 'is':
|
|
|
pass
|
|
|
|
|
|
- elif payload[:1] == 'i':
|
|
|
+ elif payload[:1] == 'i':
|
|
|
|
|
|
if culSendsRSSI:
|
|
|
- inCmd = payload[:-2]
|
|
|
+ inCmd = payload[:-2]
|
|
|
else:
|
|
|
inCmd = payload
|
|
|
|
|
|
if verbose:
|
|
|
- log_write("inCmd: " + inCmd + ", receiving CUL: " + source_cul)
|
|
|
+ log_write(" inCmd: " + inCmd + ", receiving CUL: " + source_cul)
|
|
|
|
|
|
-
|
|
|
+
|
|
|
ignoreCommand = False
|
|
|
if inCmd in lastReceivedTime.keys():
|
|
|
lastTime = int(lastReceivedTime[inCmd])
|
|
|
now = int(round(time.time() * 1000))
|
|
|
tdelta = (now - lastTime)
|
|
|
- if debug: log_write("TDELTA = " + str(tdelta))
|
|
|
- if debug: log_write("lastTime = " + str(lastTime))
|
|
|
+ if debug: log_write(" TDELTA = " + str(tdelta))
|
|
|
+ if debug: log_write(" lastTime = " + str(lastTime))
|
|
|
|
|
|
if tdelta < lastReceivedMaxAge:
|
|
|
- if verbose: log_write("ignoring command from CUL '" + source_cul + "', CMD: '" + inCmd + "' - already received " + str(tdelta) + " ms ago")
|
|
|
+ if verbose: log_write(" ignoring code from CUL '" + source_cul + "', CMD: '" + inCmd + "' - already received " + str(tdelta) + " ms ago")
|
|
|
ignoreCommand = True
|
|
|
|
|
|
if not ignoreCommand:
|
|
@@ -593,37 +614,37 @@ def cul_received(payload, source_cul):
|
|
|
|
|
|
splitPayload = payload.split(' ')
|
|
|
actualPayload = splitPayload[len(splitPayload)-1]
|
|
|
- if debug: log_write("actualPayload: '" + actualPayload)
|
|
|
+ if debug: log_write(" actualPayload: '" + actualPayload)
|
|
|
|
|
|
ignoreCommand = False
|
|
|
|
|
|
-
|
|
|
+
|
|
|
isITrepetation = False
|
|
|
if ('i'+actualPayload) in lastReceivedTime.keys():
|
|
|
isITrepetation = True
|
|
|
- if debug: log_write("IS IT REPETATION")
|
|
|
+ if debug: log_write(" skipping repeated Intertechno code")
|
|
|
lastTime = int(lastReceivedTime['i'+actualPayload])
|
|
|
now = int(round(time.time() * 1000))
|
|
|
tdelta = (now - lastTime)
|
|
|
- if debug: log_write("TDELTA = " + str(tdelta))
|
|
|
+ if debug: log_write(" TDELTA = " + str(tdelta))
|
|
|
if tdelta < lastReceivedMaxAge:
|
|
|
- if verbose: log_write("ignoring command from CUL '" + source_cul + "', CMD: '" + 'i'+actualPayload + "' - already received " + str(tdelta) + " ms ago")
|
|
|
+ if verbose: log_write(" ignoring code from CUL '" + source_cul + "', CMD: '" + 'i'+actualPayload + "' - already received " + str(tdelta) + " ms ago")
|
|
|
ignoreCommand = True
|
|
|
|
|
|
-
|
|
|
+
|
|
|
if not isITrepetation:
|
|
|
if actualPayload in lastReceivedTime.keys():
|
|
|
lastTime = int(lastReceivedTime[actualPayload])
|
|
|
now = int(round(time.time() * 1000))
|
|
|
tdelta = (now - lastTime)
|
|
|
- if debug: log_write("TDELTA = " + str(tdelta))
|
|
|
+ if debug: log_write(" TDELTA = " + str(tdelta))
|
|
|
if tdelta < lastReceivedMaxAge:
|
|
|
- if verbose: log_write("ignoring command from CUL '" + source_cul + "', CMD: '" + actualPayload + "' - already received " + str(tdelta) + " ms ago")
|
|
|
+ if verbose: log_write(" ignoring code from CUL '" + source_cul + "', CMD: '" + actualPayload + "' - already received " + str(tdelta) + " ms ago")
|
|
|
ignoreCommand = True
|
|
|
|
|
|
if not ignoreCommand:
|
|
|
if isITrepetation:
|
|
|
-
|
|
|
+
|
|
|
lastReceivedTime['i'+actualPayload] = int(round(time.time() * 1000))
|
|
|
parseRXCode('i'+actualPayload, source_cul)
|
|
|
else:
|
|
@@ -773,7 +794,7 @@ def cul_send(device, cmd):
|
|
|
else:
|
|
|
log_write("WARNING: CUL send command repeated too quickly.")
|
|
|
|
|
|
- publish_device_statusupdate(device, cmd)
|
|
|
+ publish_device_statusupdate(device, cmd, None)
|
|
|
|
|
|
|
|
|
|
|
@@ -794,7 +815,7 @@ if receive_from_serial_cul or send_on_serial_cul:
|
|
|
log_write("")
|
|
|
log_write("")
|
|
|
else:
|
|
|
- log_write("opening connection to serial CUL...")
|
|
|
+ log_write("opening connection to UART-CUL...")
|
|
|
serLine = ""
|
|
|
ser = serial.Serial(port=serialPort,baudrate=serialBaudrate,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=serialTimeout)
|
|
|
sleep(serialCulInitTimeout)
|
|
@@ -809,7 +830,7 @@ if receive_from_serial_cul or send_on_serial_cul:
|
|
|
ser.write(culInitCmd.encode('ascii'))
|
|
|
sleep(0.5)
|
|
|
else:
|
|
|
- log_write("WARNING: could not connect serial CUL")
|
|
|
+ log_write("WARNING: could not connect UART-CUL")
|
|
|
receive_from_serial_cul = False
|
|
|
send_on_serial_cul = False
|
|
|
serialCULAvailable = False
|
|
@@ -845,7 +866,7 @@ while True:
|
|
|
else:
|
|
|
if verbose:
|
|
|
log_write("")
|
|
|
- log_write("Serial-CUL RX: '" + recvCmd + "'")
|
|
|
+ log_write("UART-CUL RX: '" + recvCmd + "'")
|
|
|
cul_received(recvCmd, "UART")
|
|
|
|
|
|
sleep(0.05)
|