Browse Source

videv initialisation and periodic status publishing

tags/v1.3.0
Dirk Alders 1 year ago
parent
commit
2ec5487f00
1 changed files with 13 additions and 3 deletions
  1. 13
    3
      base.py

+ 13
- 3
base.py View File

1
 import json
1
 import json
2
 import logging
2
 import logging
3
+import task
3
 
4
 
4
 try:
5
 try:
5
     from config import APP_NAME as ROOT_LOGGER_NAME
6
     from config import APP_NAME as ROOT_LOGGER_NAME
16
         self.__callback_list__ = []
17
         self.__callback_list__ = []
17
         self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild("devices")
18
         self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild("devices")
18
 
19
 
19
-    def add_callback(self, key, data, callback, on_change_only=False):
20
+    def add_callback(self, key, data, callback, on_change_only=False, init_now=False):
20
         """
21
         """
21
         key: key or None for all keys
22
         key: key or None for all keys
22
         data: data or None for all data
23
         data: data or None for all data
24
         cb_tup = (key, data, callback, on_change_only)
25
         cb_tup = (key, data, callback, on_change_only)
25
         if cb_tup not in self.__callback_list__:
26
         if cb_tup not in self.__callback_list__:
26
             self.__callback_list__.append(cb_tup)
27
             self.__callback_list__.append(cb_tup)
28
+        if init_now and self.get(key) is not None:
29
+            callback(self, key, self[key])
27
 
30
 
28
     def set(self, key, data, block_callback=[]):
31
     def set(self, key, data, block_callback=[]):
29
         if key in self.keys():
32
         if key in self.keys():
58
         super().__init__(mqtt_client, topic, default_values=default_values)
61
         super().__init__(mqtt_client, topic, default_values=default_values)
59
         self.__display_dict__ = {}
62
         self.__display_dict__ = {}
60
         self.__control_dict__ = {}
63
         self.__control_dict__ = {}
64
+        self.__periodic__ = task.periodic(300, self.send_all)
65
+        self.__periodic__.run()
66
+
67
+    def send_all(self, rt):
68
+        for key in self:
69
+            if self[key] is not None:
70
+                self.__tx__(key, self[key])
61
 
71
 
62
     def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
72
     def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
63
         """
73
         """
69
             # store information to identify callback from ext_device
79
             # store information to identify callback from ext_device
70
             self.__display_dict__[(id(ext_device[0]), ext_key)] = my_key
80
             self.__display_dict__[(id(ext_device[0]), ext_key)] = my_key
71
             # register a callback to listen for data from external device
81
             # register a callback to listen for data from external device
72
-            ext_device[0].add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
82
+            ext_device[0].add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only, init_now=True)
73
         else:
83
         else:
74
             # store information to identify callback from ext_device
84
             # store information to identify callback from ext_device
75
             self.__display_dict__[(id(ext_device), ext_key)] = my_key
85
             self.__display_dict__[(id(ext_device), ext_key)] = my_key
76
             # register a callback to listen for data from external device
86
             # register a callback to listen for data from external device
77
-            ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
87
+            ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only, init_now=True)
78
         # send initial display data to videv interface
88
         # send initial display data to videv interface
79
         data = ext_device.get(ext_key)
89
         data = ext_device.get(ext_key)
80
         if data is not None:
90
         if data is not None:

Loading…
Cancel
Save