Добавлена нечувствительность к регистру ID/режимов устройств. Доработано протоколиров...
authorRoman Bazalevskiy <rvb@rvb.name>
Mon, 2 Apr 2018 15:01:38 +0000 (18:01 +0300)
committerRoman Bazalevskiy <rvb@rvb.name>
Mon, 2 Apr 2018 15:01:38 +0000 (18:01 +0300)
nmd/nl_mqtt.py

index 25f96da05b21e419fabe42eb63a857eb304c4c9f..4a0ccb4298323342680bff92805b99151a50d26d 100644 (file)
@@ -45,7 +45,7 @@ class MqttDriver(metaclass=Singleton):
             topic = message.topic
             payload = message.publish_packet.payload.data
 
             topic = message.topic
             payload = message.publish_packet.payload.data
 
-            logger.info('In message: {}\n{}'.format(topic, payload))
+            logger.info('In message: {} -> {}'.format(topic, payload.decode('utf-8')))
 
             if topic.startswith(self.read_topic):
               subtopic = topic[len(self.read_topic)+1:]
 
             if topic.startswith(self.read_topic):
               subtopic = topic[len(self.read_topic)+1:]
@@ -75,82 +75,95 @@ class MqttDriver(metaclass=Singleton):
                     channel = None
                     id = None
                   command = command.lower()  
                     channel = None
                     id = None
                   command = command.lower()  
+                  id = id.upper()
                   print("%s: %s (%s)" % (command,channel,id))
                   if command == "on":
                   print("%s: %s (%s)" % (command,channel,id))
                   if command == "on":
-                    if id == '.':
+                    if id == '.' or id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 2, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 2, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 2, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 2, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
+                    elif id == 'TX':
+                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 2 }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 2 }
                   elif command == "off":
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 2 }
                   elif command == "off":
-                    if id == '.':
+                    if id == '.' or id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 0, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 0, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 0, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 0, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
+                    elif id == 'TX':
+                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 0 }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 0 }
                   elif command == "brightness":
                     brightness = int(payload)
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 0 }
                   elif command == "brightness":
                     brightness = int(payload)
-                    if id == '.':
+                    if id == '.' or id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 6, "d0": brightness, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 6, "d0": brightness, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 6, "d0": brightness, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 6, "d0": brightness, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
+                    elif id == 'TX':
+                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 6, "d0": brightness }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 6, "d0": brightness }
                   elif command == "state":
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 6, "d0": brightness }
                   elif command == "state":
-                    if id == '.':
+                    if id == '.' or id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 128, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 128, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
                   elif command == "load_preset":
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 128, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 128, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
                   elif command == "load_preset":
-                    if id == '.':
+                    if id == '.' or id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 7, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 7, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 7, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 7, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
+                    elif id == 'TX':
+                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 7 }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 7 }
                   elif command == "save_preset":
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 7 }
                   elif command == "save_preset":
-                    if id == '.':
+                    if id == '.' or id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 8, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 8, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 8, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 8, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
+                    elif id == 'TX':
+                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 8 }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 8 }
                   elif command == "temp_on":
                     delay = (int(payload) + 3)//5
                     d0 = delay % 256
                     d1 = delay // 256
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 8 }
                   elif command == "temp_on":
                     delay = (int(payload) + 3)//5
                     d0 = delay % 256
                     d1 = delay // 256
-                    if id == '.':
+                    if id == '.' or id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 25, "fmt": 6, "d0": d0, "d1": d1, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 25, "fmt": 6, "d0": d0, "d1": d1, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 25, "fmt": 6, "d0": d0, "d1": d1, "ctr": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 25, "fmt": 6, "d0": d0, "d1": d1, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
+                    elif id == 'TX':
+                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 25, "fmt": 6, "d0": d0, "d1": d1 }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 25, "fmt": 6, "d0": d0, "d1": d1 }
                   elif command == "bind":
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 25, "fmt": 6, "d0": d0, "d1": d1 }
                   elif command == "bind":
-                    if id == 'tx-f':
+                    if id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 15 }
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 15 }
-                    elif id == 'tx':
+                    elif id == 'TX':
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 15 }
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 15 }
-                    elif id == 'rx':
+                    elif id == 'RX':
                       mtrf_command = { "mode": 1, "ch": channel, "cmd": 15 }
                       mtrf_command = { "mode": 1, "ch": channel, "cmd": 15 }
-                    elif id == 'rx-f':
+                    elif id == 'RX-F':
                       mtrf_command = { "mode": 3, "ch": channel, "cmd": 15 }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 15 }
                   elif command == "unbind":
                       mtrf_command = { "mode": 3, "ch": channel, "cmd": 15 }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 15 }
                   elif command == "unbind":
-                    if id == '.' or id == 'tx-f':
+                    if id == '.' or id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 9 }
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 9 }
-                    elif id == 'tx':
+                    elif id == 'TX':
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 9 }
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 9 }
-                    elif id == 'rx':
+                    elif id == 'RX':
                       mtrf_command = { "mode": 1, "ch": channel, "cmd": 9, "ctr": 5 }
                       mtrf_command = { "mode": 1, "ch": channel, "cmd": 9, "ctr": 5 }
-                    elif id == 'rx-f':
+                    elif id == 'RX-F':
                       mtrf_command = { "mode": 3, "ch": channel, "cmd": 9, "ctr": 5 }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 9 }
                   elif command == "service":
                       mtrf_command = { "mode": 3, "ch": channel, "cmd": 9, "ctr": 5 }
                     else:
                       mtrf_command = { "mode": 0, "ch": channel, "cmd": 9 }
                   elif command == "service":
-                    if id == '.':
+                    if id == '.' or id == 'TX-F':
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 131, "d0": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 131, "d0": 1, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 131, "d0": 1 }
                     elif id:
                       mtrf_command = { "mode": 2, "ch": channel, "cmd": 131, "d0": 1, "id0": int(id[0:2],16), "id1": int(id[2:4],16), "id2": int(id[4:6],16), "id3": int(id[6:8],16), "ctr": 8 }
@@ -185,8 +198,8 @@ class MqttDriver(metaclass=Singleton):
             last_command_send_time = time.time()
 
     async def input_serial_data(self, command):
             last_command_send_time = time.time()
 
     async def input_serial_data(self, command):
-        logger.info('Pub command: {}'.format(command))
         command = self.noolite_response_to_payload(command.to_list())
         command = self.noolite_response_to_payload(command.to_list())
+        logger.info('Pub command: {}'.format(command))
         try:
           topic = "%s/%s/%s" % (self.write_topic, command['ch'], command['id'])
         except:
         try:
           topic = "%s/%s/%s" % (self.write_topic, command['ch'], command['id'])
         except: