Причесан разбор параметров, добавлены унифицированные с Tasmota команды POWER и DIMMER.
authorRoman Bazalevsky <rvb@rvb.name>
Thu, 26 Dec 2019 18:32:40 +0000 (21:32 +0300)
committerRoman Bazalevsky <rvb@rvb.name>
Fri, 27 Dec 2019 14:24:21 +0000 (17:24 +0300)
nmd/nl_mqtt.py

index 4a0ccb4298323342680bff92805b99151a50d26d..ab7b997c3e747fb1dafc0929c878c6b932a6d5cd 100644 (file)
@@ -45,7 +45,7 @@ class MqttDriver(metaclass=Singleton):
             topic = message.topic
             payload = message.publish_packet.payload.data
 
-            logger.info('In message: {} -> {}'.format(topic, payload.decode('utf-8')))
+            logger.info('In message: {}\n{}'.format(topic, payload))
 
             if topic.startswith(self.read_topic):
               subtopic = topic[len(self.read_topic)+1:]
@@ -60,7 +60,6 @@ class MqttDriver(metaclass=Singleton):
                 await self.commands_to_send_queue.put(payload)
               else:
                 try:
-                  mtrf_command = None
                   address = subtopic.split('/')
                   if len(address)==2:
                     channel = int(address[0])
@@ -74,106 +73,81 @@ class MqttDriver(metaclass=Singleton):
                     command = address[0]
                     channel = None
                     id = None
+
                   command = command.lower()  
-                  id = id.upper()
+
                   print("%s: %s (%s)" % (command,channel,id))
-                  if command == "on":
-                    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 }
-                    elif id == 'TX':
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 2 }
-                    else:
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 2 }
+
+                  mtrf_command = { "ch": channel }
+                  if id == '.' or id == 'TX-F':
+                    mtrf_command["mode"] = 2
+                  elif id =='TX':
+                    mtrf_command["mode"] = 0
+                  elif id =='RX':
+                    mtrf_command["mode"] = 1
+                  elif id =='RX-F':
+                    mtrf_command["mode"] = 3
+                  elif id:
+                    mtrf_command["mode"] = 2
+                    mtrf_command["ctr"] = 8
+                    mtrf_command["id0"] = int(id[0:2],16)
+                    mtrf_command["id1"] = int(id[2:4],16)
+                    mtrf_command["id2"] = int(id[4:6],16)
+                    mtrf_command["id3"] = int(id[6:8],16)
+                  else:
+                    mtrf_command["mode"] = 0                    
+
+                  if command == "power":
+                    payload = payload.decode('utf-8').lower()
+                    print( "command: POWER " + payload )
+                    if payload == "off":
+                      mtrf_command["cmd"] = 0
+                    else:  
+                      mtrf_command["cmd"] = 2
+
+                  elif command == "on":
+                    mtrf_command["cmd"] = 2
+
                   elif command == "off":
-                    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 }
-                    elif id == 'TX':
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 0 }
-                    else:
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 0 }
+                    mtrf_command["cmd"] = 0
+
                   elif command == "brightness":
-                    brightness = int(payload)
-                    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 }
-                    elif id == 'TX':
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 6, "d0": brightness }
-                    else:
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 6, "d0": brightness }
+                    mtrf_command["cmd"] = 6
+                    mtrf_command["d0"] = int(float(payload))
+                      
+                  elif command == "dimmer":
+                    mtrf_command["cmd"] = 6
+                    mtrf_command["d0"] = int(round(float(payload)*255/100))
+
                   elif command == "state":
-                    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 }
+                    mtrf_command["cmd"] = 128
+
                   elif command == "load_preset":
-                    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 }
-                    elif id == 'TX':
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 7 }
-                    else:
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 7 }
+                    mtrf_command["cmd"] = 7
+
                   elif command == "save_preset":
-                    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 }
-                    elif id == 'TX':
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 8 }
-                    else:
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 8 }
+                    mtrf_command["cmd"] = 8
+
                   elif command == "temp_on":
                     delay = (int(payload) + 3)//5
-                    d0 = delay % 256
-                    d1 = delay // 256
-                    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 }
-                    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 }
+                    mtrf_command["cmd"] = 25
+                    mtrf_command["d0"] = delay % 256
+                    mtrf_command["d1"] = delay // 256
+                    mtfr_command["fmt"] = 6
+
                   elif command == "bind":
-                    if id == 'TX-F':
-                      mtrf_command = { "mode": 2, "ch": channel, "cmd": 15 }
-                    elif id == 'TX':
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 15 }
-                    elif id == 'RX':
-                      mtrf_command = { "mode": 1, "ch": channel, "cmd": 15 }
-                    elif id == 'RX-F':
-                      mtrf_command = { "mode": 3, "ch": channel, "cmd": 15 }
-                    else:
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 15 }
+                    mtrf_command["cmd"] = 15
+
                   elif command == "unbind":
-                    if id == '.' or id == 'TX-F':
-                      mtrf_command = { "mode": 2, "ch": channel, "cmd": 9 }
-                    elif id == 'TX':
-                      mtrf_command = { "mode": 0, "ch": channel, "cmd": 9 }
-                    elif id == 'RX':
-                      mtrf_command = { "mode": 1, "ch": channel, "cmd": 9, "ctr": 5 }
-                    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":
-                    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["cmd"] = 9
 
-                  if mtrf_command:
-                    await self.commands_to_send_queue.put(mtrf_command)
+                  elif command == "service":
+                    mtrf_command["cmd"] = 131
 
                 except Exception as e:
                   logger.exception(e)
                   continue
+                await self.commands_to_send_queue.put(mtrf_command)
                  
 
     async def send_command_to_noolite(self):
@@ -198,8 +172,8 @@ class MqttDriver(metaclass=Singleton):
             last_command_send_time = time.time()
 
     async def input_serial_data(self, command):
-        command = self.noolite_response_to_payload(command.to_list())
         logger.info('Pub command: {}'.format(command))
+        command = self.noolite_response_to_payload(command.to_list())
         try:
           topic = "%s/%s/%s" % (self.write_topic, command['ch'], command['id'])
         except:
@@ -324,12 +298,14 @@ class MqttDriver(metaclass=Singleton):
             dev_state = data[2] % 16
             try:
               message['dev_state'] = [ 'OFF', 'ON', 'TEMPORARY_ON' ][dev_state]
+              message['POWER'] = message['dev_state']
             finally:
               None
             dev_mode = data[2] // 128
             if dev_mode:
               message['dev_binding'] = 'ON'
             message['brightness'] = data[3]
+            message['DIMMER'] = int(round(data[3]*100/255))
           elif fmt == 1:
             message['dev_aux'] = data[2]
             message['dev_legacy'] = data[3]