Преглед изворни кода

videv loopback prevention ignore rx after tx

videv_dev
Dirk Alders пре 1 година
родитељ
комит
3a4d43c0c4
1 измењених фајлова са 20 додато и 10 уклоњено
  1. 20
    10
      function/videv.py

+ 20
- 10
function/videv.py Прегледај датотеку

@@ -13,6 +13,7 @@ Targets:
13 13
 from base import mqtt_base
14 14
 import devices
15 15
 import json
16
+import time
16 17
 
17 18
 try:
18 19
     from config import APP_NAME as ROOT_LOGGER_NAME
@@ -28,6 +29,7 @@ class base(mqtt_base):
28 29
         self.__display_dict__ = {}
29 30
         self.__control_dict__ = {}
30 31
         self.__capabilities__ = None
32
+        self.__active_tx__ = {}
31 33
 
32 34
     def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
33 35
         """
@@ -49,6 +51,8 @@ class base(mqtt_base):
49 51
         self.__tx__(self.__display_dict__[(id(ext_device), ext_key)], data)
50 52
 
51 53
     def __tx__(self, key, data):
54
+        if key in self.__control_dict__:
55
+            self.__active_tx__[key] = (time.time(), data)
52 56
         if type(data) not in (str, ):
53 57
             data = json.dumps(data)
54 58
         self.mqtt_client.send(self.topic + '/' + key, data)
@@ -69,17 +73,23 @@ class base(mqtt_base):
69 73
 
70 74
     def __rx_videv_data__(self, client, userdata, message):
71 75
         my_key = message.topic.split('/')[-1]
72
-        ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
73
-        if my_key in self.keys():
74
-            try:
75
-                data = json.loads(message.payload)
76
-            except json.decoder.JSONDecodeError:
77
-                data = message.payload
78
-            if data != self[my_key] or not on_change_only:
79
-                ext_device.set(ext_key, data)
80
-            self.set(my_key, data)
76
+        try:
77
+            data = json.loads(message.payload)
78
+        except json.decoder.JSONDecodeError:
79
+            data = message.payload
80
+        if my_key in self.__active_tx__:
81
+            tm, tx_data = self.__active_tx__.pop(my_key)
82
+            do_ex = data != tx_data and time.time() - tm < 2
81 83
         else:
82
-            self.logger.info("Ignoring rx message with topic %s", message.topic)
84
+            do_ex = True
85
+        if do_ex:
86
+            ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
87
+            if my_key in self.keys():
88
+                if data != self[my_key] or not on_change_only:
89
+                    ext_device.set(ext_key, data)
90
+                self.set(my_key, data)
91
+            else:
92
+                self.logger.info("Ignoring rx message with topic %s", message.topic)
83 93
 
84 94
     def add_routing(self, my_key, ext_device, ext_key, on_change_only_disp=True, on_change_only_videv=True):
85 95
         """

Loading…
Откажи
Сачувај