Parcourir la source

data transfer changed to continues_statistic per sensor

master
Dirk Alders il y a 4 ans
Parent
révision
e9c6321772
1 fichiers modifiés avec 14 ajouts et 70 suppressions
  1. 14
    70
      __init__.py

+ 14
- 70
__init__.py Voir le fichier

@@ -9,18 +9,14 @@ import time
9 9
 from helpers import continues_statistic
10 10
 from rpi_envsens.dht import dht_22
11 11
 from rpi_envsens.bmp import bmp_180
12
+import helpers
12 13
 
13 14
 logger = logging.getLogger('APP')
14 15
 
15 16
 
16 17
 class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
17
-    # EXECUTE_REQUESTs
18
-    CURRENT_ENVDATA = 0
19
-    TODAYS_STATISTIC_ENVDATA = 1
20
-    YESTERDAYS_STATISTIC_ENVDATA = 2
21
-    BUFFERED_TEMPERATURE = 3
22
-    BUFFERED_HUMIDITY = 4
23
-    BUFFERED_PRESSURE = 5
18
+    ENVDATA_STATISTIC_DHT = 0
19
+    ENVDATA_STATISTIC_BMP = 1
24 20
 
25 21
 
26 22
 class my_server_protocol_tcp(my_base_protocol_tcp):
@@ -28,57 +24,16 @@ class my_server_protocol_tcp(my_base_protocol_tcp):
28 24
         socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
29 25
         self.dht_data = dht_data
30 26
         self.bmp_data = bmp_data
31
-        self.register_callback(self.SID_READ_REQUEST, None, self.read_request)
27
+        self.register_callback(self.SID_READ_REQUEST, self.ENVDATA_STATISTIC_BMP, self.envdata_statistic_request)
28
+        self.register_callback(self.SID_READ_REQUEST, self.ENVDATA_STATISTIC_DHT, self.envdata_statistic_request)
32 29
 
33
-    def data_age_okay(self, dd, bd):
34
-        MAX_AGE = 60.
35
-        tm = time.time()
36
-        return dd[dht_22.KEY_TIME] + MAX_AGE >= tm and bd[bmp_180.KEY_TIME] + MAX_AGE >= tm 
37
-
38
-    def read_request(self, msg):
30
+    def envdata_statistic_request(self, msg):
39 31
         did = msg.get_data_id()
40
-        if did == self.CURRENT_ENVDATA:
41
-            dd = self.dht_data.current
42
-            bd = self.bmp_data.current
43
-            if dd is not None and bd is not None:
44
-                if self.data_age_okay(bd, dd):
45
-                    rv = {}
46
-                    rv[dht_22.KEY_TEMPERATURE] = dd[dht_22.KEY_TEMPERATURE]
47
-                    rv[dht_22.KEY_HUMIDITY] = dd[dht_22.KEY_HUMIDITY]
48
-                    rv[bmp_180.KEY_PRESSURE] = bd[bmp_180.KEY_PRESSURE]
49
-                    return self.STATUS_OKAY, rv
50
-            return self.STATUS_OPERATION_NOT_PERMITTED, None
51
-        elif did in [self.TODAYS_STATISTIC_ENVDATA, self.YESTERDAYS_STATISTIC_ENVDATA]:
52
-            if did == self.TODAYS_STATISTIC_ENVDATA:
53
-                dd = self.dht_data.todays_statistic
54
-                bd = self.bmp_data.todays_statistic
55
-            else:
56
-                dd = self.dht_data.yesterdays_statistic
57
-                bd = self.bmp_data.yesterdays_statistic
58
-            if dd is None or bd is None:
59
-                return self.STATUS_OPERATION_NOT_PERMITTED, None
60
-            else:
61
-                rv = {}
62
-                rv[dht_22.KEY_TEMPERATURE] = dd[dht_22.KEY_TEMPERATURE]
63
-                rv[dht_22.KEY_HUMIDITY] = dd[dht_22.KEY_HUMIDITY]
64
-                rv[bmp_180.KEY_PRESSURE] = bd[bmp_180.KEY_PRESSURE]
65
-                return self.STATUS_OKAY, rv
66
-        elif did in [self.BUFFERED_HUMIDITY, self.BUFFERED_PRESSURE, self.BUFFERED_TEMPERATURE]:
67
-            if did == self.BUFFERED_HUMIDITY:
68
-                db = self.dht_data.databuffer
69
-                key = dht_22.KEY_HUMIDITY
70
-            elif did == self.BUFFERED_PRESSURE:
71
-                db = self.bmp_data.databuffer
72
-                key = bmp_180.KEY_PRESSURE
73
-            else:
74
-                db = self.dht_data.databuffer
75
-                key = dht_22.KEY_TEMPERATURE
76
-            rv = []
77
-            for entry in db:
78
-                rv.append((entry[dht_22.KEY_TIME], entry[key]))
79
-            return self.STATUS_OKAY, rv
80
-        else:
81
-            return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
32
+        if did == self.ENVDATA_STATISTIC_BMP:
33
+            return self.STATUS_OKAY, dict(self.bmp_data.pop())
34
+        elif did == self.ENVDATA_STATISTIC_DHT:
35
+            return self.STATUS_OKAY, dict(self.dht_data.pop())
36
+        return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
82 37
 
83 38
 
84 39
 class my_client_protocol_tcp(my_base_protocol_tcp):
@@ -99,23 +54,12 @@ class my_client_protocol_tcp(my_base_protocol_tcp):
99 54
         if msg.get_status() == self.STATUS_OKAY:
100 55
             did = msg.get_data_id()
101 56
             data = msg.get_data()
102
-            if did == self.CURRENT_ENVDATA:
103
-                print("Temperature: %6.1f °C" % data[dht_22.KEY_TEMPERATURE])
104
-                print("Humidity:    %6.1f %%" % data[dht_22.KEY_HUMIDITY])
105
-                print("Pressure:    %6.1f mbar" % data[bmp_180.KEY_PRESSURE])
106
-            elif did in [self.TODAYS_STATISTIC_ENVDATA, self.YESTERDAYS_STATISTIC_ENVDATA]:
107
-                print(continues_statistic(**data))
108
-            elif did in [self.BUFFERED_HUMIDITY, self.BUFFERED_PRESSURE, self.BUFFERED_TEMPERATURE]:
109
-                if did == self.BUFFERED_HUMIDITY:
110
-                    print("Humidity:")
111
-                elif did == self.BUFFERED_PRESSURE:
112
-                    print("Pressure:")
113
-                else:
114
-                    print("Temperature:")
115
-                print(data)
57
+            if did in [self.ENVDATA_STATISTIC_BMP, self.ENVDATA_STATISTIC_DHT]:
58
+                print(helpers.continues_statistic_multivalue(**data))
116 59
             else:
117 60
                 return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
118 61
             return self.STATUS_OKAY, None
119 62
         else:
120 63
             print('No data received!')
121 64
             return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
65
+

Chargement…
Annuler
Enregistrer