Browse Source

new data source for server implemented

master
Dirk Alders 4 years ago
parent
commit
14c84008c5
1 changed files with 27 additions and 10 deletions
  1. 27
    10
      __init__.py

+ 27
- 10
__init__.py View File

@@ -4,8 +4,10 @@
4 4
 import config
5 5
 import logging
6 6
 import socket_protocol
7
+import time
7 8
 
8
-import rpi_envsens as envsens
9
+from rpi_envsens.dht import dht_22
10
+from rpi_envsens.bmp import bmp_180
9 11
 
10 12
 logger = logging.getLogger('APP')
11 13
 
@@ -18,13 +20,28 @@ class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
18 20
 
19 21
 
20 22
 class my_server_protocol_tcp(my_base_protocol_tcp):
21
-    def __init__(self, comm_instance, envsens, secret=None):
23
+    def __init__(self, comm_instance, dht_data, bmp_data, secret=None):
22 24
         socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
23
-        self.envsens = envsens
24
-        self.register_callback(self.SID_READ_REQUEST, self.CURRENT_ENVDATA, self.rd_temperature)
25
-
26
-    def rd_temperature(self, msg):
27
-        return self.STATUS_OKAY, self.envsens.get_env_data()
25
+        self.dht_data = dht_data
26
+        self.bmp_data = bmp_data
27
+        self.register_callback(self.SID_READ_REQUEST, self.CURRENT_ENVDATA, self.current_envdata)
28
+
29
+    def data_age_okay(self, dd, bd):
30
+        MAX_AGE = 60.
31
+        tm = time.time()
32
+        return dd[dht_22.KEY_TIME] + MAX_AGE >= tm and bd[bmp_180.KEY_TIME] + MAX_AGE >= tm 
33
+
34
+    def current_envdata(self, msg):
35
+        dd = self.dht_data.current
36
+        bd = self.bmp_data.current
37
+        if self.data_age_okay(bd, dd):
38
+            rv = {}
39
+            rv[dht_22.KEY_TEMPERATURE] = dd[dht_22.KEY_TEMPERATURE]
40
+            rv[dht_22.KEY_HUMIDITY] = dd[dht_22.KEY_HUMIDITY]
41
+            rv[bmp_180.KEY_PRESSURE] = bd[bmp_180.KEY_PRESSURE]
42
+            return self.STATUS_OKAY, rv
43
+        else:
44
+            return self.STATUS_OPERATION_NOT_PERMITTED, None
28 45
 
29 46
 
30 47
 class my_client_protocol_tcp(my_base_protocol_tcp):
@@ -44,9 +61,9 @@ class my_client_protocol_tcp(my_base_protocol_tcp):
44 61
     def read_current_envdata(self, msg):
45 62
         if msg.get_status() == self.STATUS_OKAY:
46 63
             env_data = msg.get_data()
47
-            print("Temperature: %6.1f °C" % env_data[envsens.KEY_TEMPERATURE])
48
-            print("Humidity:    %6.1f %%" % env_data[envsens.KEY_HUMIDITY])
49
-            print("Pressure:    %6.1f mbar" % env_data[envsens.KEY_PRESSURE])
64
+            print("Temperature: %6.1f °C" % env_data[dht_22.KEY_TEMPERATURE])
65
+            print("Humidity:    %6.1f %%" % env_data[dht_22.KEY_HUMIDITY])
66
+            print("Pressure:    %6.1f mbar" % env_data[bmp_180.KEY_PRESSURE])
50 67
             return self.STATUS_OKAY, None
51 68
         else:
52 69
             print('No environmental data received!')

Loading…
Cancel
Save