Procházet zdrojové kódy

videv initialisation and periodic status publishing

tags/v1.3.0
Dirk Alders před 1 rokem
rodič
revize
2ec5487f00
1 změnil soubory, kde provedl 13 přidání a 3 odebrání
  1. 13
    3
      base.py

+ 13
- 3
base.py Zobrazit soubor

@@ -1,5 +1,6 @@
1 1
 import json
2 2
 import logging
3
+import task
3 4
 
4 5
 try:
5 6
     from config import APP_NAME as ROOT_LOGGER_NAME
@@ -16,7 +17,7 @@ class common_base(dict):
16 17
         self.__callback_list__ = []
17 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 22
         key: key or None for all keys
22 23
         data: data or None for all data
@@ -24,6 +25,8 @@ class common_base(dict):
24 25
         cb_tup = (key, data, callback, on_change_only)
25 26
         if cb_tup not in self.__callback_list__:
26 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 31
     def set(self, key, data, block_callback=[]):
29 32
         if key in self.keys():
@@ -58,6 +61,13 @@ class videv_base(mqtt_base):
58 61
         super().__init__(mqtt_client, topic, default_values=default_values)
59 62
         self.__display_dict__ = {}
60 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 72
     def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
63 73
         """
@@ -69,12 +79,12 @@ class videv_base(mqtt_base):
69 79
             # store information to identify callback from ext_device
70 80
             self.__display_dict__[(id(ext_device[0]), ext_key)] = my_key
71 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 83
         else:
74 84
             # store information to identify callback from ext_device
75 85
             self.__display_dict__[(id(ext_device), ext_key)] = my_key
76 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 88
         # send initial display data to videv interface
79 89
         data = ext_device.get(ext_key)
80 90
         if data is not None:

Načítá se…
Zrušit
Uložit