|
@@ -23,13 +23,14 @@ except ImportError:
|
23
|
23
|
|
24
|
24
|
class base(mqtt_base):
|
25
|
25
|
KEY_INFO = '__info__'
|
|
26
|
+ #
|
|
27
|
+ SET_TOPIC = "set"
|
26
|
28
|
|
27
|
29
|
def __init__(self, mqtt_client, topic, default_values=None):
|
28
|
30
|
super().__init__(mqtt_client, topic, default_values=default_values)
|
29
|
31
|
self.__display_dict__ = {}
|
30
|
32
|
self.__control_dict__ = {}
|
31
|
33
|
self.__capabilities__ = None
|
32
|
|
- self.__active_tx__ = {}
|
33
|
34
|
|
34
|
35
|
def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
|
35
|
36
|
"""
|
|
@@ -49,15 +50,13 @@ class base(mqtt_base):
|
49
|
50
|
|
50
|
51
|
def __rx_ext_device_data__(self, ext_device, ext_key, data):
|
51
|
52
|
my_key = self.__display_dict__[(id(ext_device), ext_key)]
|
52
|
|
- self[my_key] = data
|
|
53
|
+ self.set(my_key, data)
|
53
|
54
|
self.__tx__(my_key, data)
|
54
|
55
|
|
55
|
56
|
def __tx__(self, key, data):
|
56
|
|
- if key in self.__control_dict__:
|
57
|
|
- self.__active_tx__[key] = (time.time(), data)
|
58
|
57
|
if type(data) not in (str, ):
|
59
|
58
|
data = json.dumps(data)
|
60
|
|
- self.mqtt_client.send(self.topic + '/' + key, data)
|
|
59
|
+ self.mqtt_client.send('/'.join([self.topic, key]), data)
|
61
|
60
|
self.__tx_capabilities__()
|
62
|
61
|
|
63
|
62
|
def __tx_capabilities__(self):
|
|
@@ -71,27 +70,20 @@ class base(mqtt_base):
|
71
|
70
|
# store information to identify callback from videv
|
72
|
71
|
self.__control_dict__[my_key] = (ext_device, ext_key, on_change_only)
|
73
|
72
|
# add callback for videv changes
|
74
|
|
- self.mqtt_client.add_callback(self.topic + '/' + my_key, self.__rx_videv_data__)
|
|
73
|
+ self.mqtt_client.add_callback('/'.join([self.topic, my_key, self.SET_TOPIC]), self.__rx_videv_data__)
|
75
|
74
|
|
76
|
75
|
def __rx_videv_data__(self, client, userdata, message):
|
77
|
|
- my_key = message.topic.split('/')[-1]
|
|
76
|
+ my_key = message.topic.split('/')[-2]
|
78
|
77
|
try:
|
79
|
78
|
data = json.loads(message.payload)
|
80
|
79
|
except json.decoder.JSONDecodeError:
|
81
|
80
|
data = message.payload
|
82
|
|
- if my_key in self.__active_tx__:
|
83
|
|
- tm, tx_data = self.__active_tx__.pop(my_key)
|
84
|
|
- do_ex = data != tx_data and time.time() - tm < 2
|
|
81
|
+ ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
|
|
82
|
+ if my_key in self.keys():
|
|
83
|
+ if data != self[my_key] or not on_change_only:
|
|
84
|
+ ext_device.send_command(ext_key, data)
|
85
|
85
|
else:
|
86
|
|
- do_ex = True
|
87
|
|
- if do_ex:
|
88
|
|
- ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
|
89
|
|
- if my_key in self.keys():
|
90
|
|
- if data != self[my_key] or not on_change_only:
|
91
|
|
- ext_device.send_command(ext_key, data)
|
92
|
|
- self.set(my_key, data)
|
93
|
|
- else:
|
94
|
|
- self.logger.info("Ignoring rx message with topic %s", message.topic)
|
|
86
|
+ self.logger.info("Ignoring rx message with topic %s", message.topic)
|
95
|
87
|
|
96
|
88
|
def add_routing(self, my_key, ext_device, ext_key, on_change_only_disp=True, on_change_only_videv=True):
|
97
|
89
|
"""
|