Procházet zdrojové kódy

send mean values of last 120s via mqtt

master
Dirk Alders před 1 rokem
rodič
revize
3c34127656
1 změnil soubory, kde provedl 61 přidání a 14 odebrání
  1. 61
    14
      ambient_info.py

+ 61
- 14
ambient_info.py Zobrazit soubor

@@ -1,6 +1,6 @@
1 1
 import config
2 2
 import logging
3
-import mqtt
3
+import paho.mqtt.client as paho
4 4
 import socket
5 5
 import report
6 6
 import time
@@ -15,27 +15,74 @@ except ImportError:
15 15
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
16 16
 
17 17
 
18
-mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
18
+class meaner(dict):
19
+    def __init__(self):
20
+        super().__init__(self)
21
+        self.stop_adding = False
22
+        self.start_time = time.time()
23
+
24
+    def add_data(self, **kwargs):
25
+        while self.stop_adding:
26
+            time.sleep(0.1)
27
+        for key in kwargs:
28
+            if key not in self:
29
+                self[key] = kwargs[key]
30
+                self['__' + key + '_n__'] = 1
31
+            else:
32
+                self[key] += kwargs[key]
33
+                self['__' + key + '_n__'] += 1
34
+
35
+    def pop(self):
36
+        self.stop_adding = True
37
+        rv = {}
38
+        for key in self:
39
+            if not key.startswith('__'):
40
+                rv[key] = self[key] / self['__' + key + '_n__']
41
+        self.clear()
42
+        self.start_time = time.time()
43
+        self.stop_adding = False
44
+        return rv
45
+
46
+    def meaning_time(self):
47
+        return time.time() - self.start_time
48
+
49
+
50
+dht_meaner = meaner()
51
+bmp_meaner = meaner()
19 52
 
20 53
 def send_data_to_mqtt(data):
21
-    for key in data:
22
-        topic = config.MQTT_TOPIC + "/" + key
23
-        logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
24
-        mc.send(topic, data[key])
54
+    client = paho.Client("temp_sens")
55
+    client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
56
+    try:
57
+        client.connect(config.MQTT_SERVER, 1883)
58
+        for key in data:
59
+            topic = config.MQTT_TOPIC + "/" + key
60
+            logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
61
+            client.publish(topic, data[key])
62
+    except (socket.timeout, OSError) as e:
63
+        logger.warning("Erro while sending ambient information")
64
+
25 65
 
26 66
 def dht_callback(**data):
27
-    del(data["time"])
28
-    send_data_to_mqtt(data)
67
+    dht_meaner.add_data(**data)
68
+    if dht_meaner.meaning_time() > 2 * 60:
69
+        sd = dht_meaner.pop()
70
+        del (sd["time"])
71
+        send_data_to_mqtt(sd)
72
+
29 73
 
30 74
 def bmp_callback(**data):
31
-    del(data["time"])
32
-    del(data["temperature"])
33
-    send_data_to_mqtt(data)
34
-    
75
+    bmp_meaner.add_data(**data)
76
+    if bmp_meaner.meaning_time() > 2 * 30:
77
+        sd = bmp_meaner.pop()
78
+        del (sd["time"])
79
+        del (sd["temperature"])
80
+        send_data_to_mqtt(sd)
35 81
 
36 82
 
37
-if __name__ == '__main__': 
38
-    report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
83
+if __name__ == '__main__':
84
+    report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ),
85
+                               fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
39 86
 
40 87
     #
41 88
     # Initialise data collectors

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