浏览代码

Some services added

master
Dirk Alders 4 年前
父节点
当前提交
bc4808b313
共有 1 个文件被更改,包括 70 次插入20 次删除
  1. 70
    20
      __init__.py

+ 70
- 20
__init__.py 查看文件

@@ -6,17 +6,21 @@ import logging
6 6
 import socket_protocol
7 7
 import time
8 8
 
9
+from helpers import continues_statistic
9 10
 from rpi_envsens.dht import dht_22
10 11
 from rpi_envsens.bmp import bmp_180
11 12
 
12 13
 logger = logging.getLogger('APP')
13 14
 
14 15
 
15
-
16
-
17 16
 class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
18 17
     # EXECUTE_REQUESTs
19 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
20 24
 
21 25
 
22 26
 class my_server_protocol_tcp(my_base_protocol_tcp):
@@ -24,24 +28,57 @@ class my_server_protocol_tcp(my_base_protocol_tcp):
24 28
         socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
25 29
         self.dht_data = dht_data
26 30
         self.bmp_data = bmp_data
27
-        self.register_callback(self.SID_READ_REQUEST, self.CURRENT_ENVDATA, self.current_envdata)
31
+        self.register_callback(self.SID_READ_REQUEST, None, self.read_request)
28 32
 
29 33
     def data_age_okay(self, dd, bd):
30 34
         MAX_AGE = 60.
31 35
         tm = time.time()
32 36
         return dd[dht_22.KEY_TIME] + MAX_AGE >= tm and bd[bmp_180.KEY_TIME] + MAX_AGE >= tm 
33 37
 
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]
38
+    def read_request(self, msg):
39
+        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]))
42 79
             return self.STATUS_OKAY, rv
43 80
         else:
44
-            return self.STATUS_OPERATION_NOT_PERMITTED, None
81
+            return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
45 82
 
46 83
 
47 84
 class my_client_protocol_tcp(my_base_protocol_tcp):
@@ -49,7 +86,7 @@ class my_client_protocol_tcp(my_base_protocol_tcp):
49 86
 
50 87
     def __init__(self, comm_instance, secret=None):
51 88
         socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
52
-        self.register_callback(self.SID_READ_RESPONSE, self.CURRENT_ENVDATA, self.read_current_envdata)
89
+        self.register_callback(self.SID_READ_RESPONSE, None, self.print_read_response)
53 90
 
54 91
     def __authentificate_process_feedback__(self, msg):
55 92
         if msg.get_data() is True:
@@ -58,14 +95,27 @@ class my_client_protocol_tcp(my_base_protocol_tcp):
58 95
             print("AUTHENTIFICATION ERROR")
59 96
         return my_base_protocol_tcp.__authentificate_process_feedback__(self, msg)
60 97
 
61
-    def read_current_envdata(self, msg):
98
+    def print_read_response(self, msg):
62 99
         if msg.get_status() == self.STATUS_OKAY:
63
-            env_data = msg.get_data()
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])
100
+            did = msg.get_data_id()
101
+            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)
116
+            else:
117
+                return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
67 118
             return self.STATUS_OKAY, None
68 119
         else:
69
-            print('No environmental data received!')
120
+            print('No data received!')
70 121
             return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
71
-

正在加载...
取消
保存