소스 검색

zigbee stuff restructured

master
Dirk Alders 1 년 전
부모
커밋
149cf7cdeb
11개의 변경된 파일51개의 추가작업 그리고 170개의 파일을 삭제
  1. 1
    0
      check_z_battery
  2. 1
    0
      check_z_follow
  3. 8
    7
      check_z_heartbeat
  4. 0
    1
      check_z_heat_vlv_battery
  5. 0
    1
      check_z_heat_vlv_heartbeat
  6. 0
    1
      check_z_motion_battery
  7. 0
    43
      check_z_motion_heartbeat
  8. 0
    1
      check_z_tr_inp_battery
  9. 0
    43
      check_z_tr_inp_heartbeat
  10. 38
    67
      z_server/devices/__init__.py
  11. 3
    6
      z_server/z_protocol.py

+ 1
- 0
check_z_battery 파일 보기

@@ -0,0 +1 @@
1
+check_z_heartbeat

+ 1
- 0
check_z_follow 파일 보기

@@ -0,0 +1 @@
1
+check_z_heartbeat

check_z_heat_vlv_follow → check_z_heartbeat 파일 보기

@@ -7,7 +7,7 @@ import time
7 7
 from z_server import config
8 8
 from z_server import tcp_socket
9 9
 from z_server.z_protocol import server as client_prot
10
-from z_server.z_protocol import DID_FOLLOWS_HEATING_SETPOINT, DID_BATTERY_LEVEL, DID_HEARTBEAT
10
+from z_server.z_protocol import DID_FOLLOWS_SETPOINT, DID_BATTERY_LEVEL, DID_HEARTBEAT
11 11
 from z_server import socket_protocol
12 12
 import sys
13 13
 
@@ -19,6 +19,7 @@ if __name__ == '__main__':
19 19
     parser.add_argument('-s', '--stg', required=True)
20 20
     parser.add_argument('-l', '--loc', required=True)
21 21
     parser.add_argument('-r', '--roo', required=True)
22
+    parser.add_argument('-f', '--fun', required=True)
22 23
     args = parser.parse_args()
23 24
     #
24 25
     c = tcp_socket.tcp_client_stp('127.0.0.1', config.SOCK_PROT_PORT)
@@ -28,18 +29,18 @@ if __name__ == '__main__':
28 29
         "stg": args.stg,
29 30
         "loc": args.loc,
30 31
         "roo": args.roo,
31
-        "fun": "FUN_HEA"    # <-- Const, because script is for heat_vlv only
32
+        "fun": args.fun
32 33
     }
33 34
     #
34
-    if sys.argv[0].endswith('check_z_heat_vlv_heartbeat'):
35
+    if sys.argv[0].endswith('check_z_heartbeat'):
35 36
         sp.send(socket_protocol.SID_READ_REQUEST, DID_HEARTBEAT, data)
36 37
         sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_HEARTBEAT).get_data()
37
-    elif sys.argv[0].endswith('check_z_heat_vlv_follow'):
38
-        sp.send(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_HEATING_SETPOINT, data)
39
-        sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_FOLLOWS_HEATING_SETPOINT).get_data()
40
-    elif sys.argv[0].endswith('check_z_heat_vlv_battery'):
38
+    elif sys.argv[0].endswith('check_z_battery'):
41 39
         sp.send(socket_protocol.SID_READ_REQUEST, DID_BATTERY_LEVEL, data)
42 40
         sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_BATTERY_LEVEL).get_data()
41
+    elif sys.argv[0].endswith('check_z_follow'):
42
+        sp.send(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_SETPOINT, data)
43
+        sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_FOLLOWS_SETPOINT).get_data()
43 44
     else:
44 45
         sys.stderr.write('No action for command called "%s"\n' % sys.argv[0])
45 46
         sys.exit(100)

+ 0
- 1
check_z_heat_vlv_battery 파일 보기

@@ -1 +0,0 @@
1
-check_z_heat_vlv_follow

+ 0
- 1
check_z_heat_vlv_heartbeat 파일 보기

@@ -1 +0,0 @@
1
-check_z_heat_vlv_follow

+ 0
- 1
check_z_motion_battery 파일 보기

@@ -1 +0,0 @@
1
-check_z_motion_heartbeat

+ 0
- 43
check_z_motion_heartbeat 파일 보기

@@ -1,43 +0,0 @@
1
-#!/bin/python3
2
-#
3
-
4
-import argparse
5
-import nagios
6
-import time
7
-from z_server import config
8
-from z_server import tcp_socket
9
-from z_server.z_protocol import server as client_prot
10
-from z_server.z_protocol import DID_FOLLOWS_HEATING_SETPOINT, DID_BATTERY_LEVEL, DID_HEARTBEAT
11
-from z_server import socket_protocol
12
-import sys
13
-
14
-if __name__ == '__main__':
15
-    parser = argparse.ArgumentParser(
16
-        prog='ProgramName',
17
-        description='What the program does',
18
-        epilog='Text at the bottom of help')
19
-    parser.add_argument('-s', '--stg', required=True)
20
-    parser.add_argument('-l', '--loc', required=True)
21
-    parser.add_argument('-r', '--roo', required=True)
22
-    args = parser.parse_args()
23
-    #
24
-    c = tcp_socket.tcp_client_stp('127.0.0.1', config.SOCK_PROT_PORT)
25
-    sp = client_prot(c, channel_name='example_client')
26
-    #
27
-    data = {
28
-        "stg": args.stg,
29
-        "loc": args.loc,
30
-        "roo": args.roo,
31
-        "fun": "FUN_MSE"    # <-- Const, because script is for input_device
32
-    }
33
-    #
34
-    if sys.argv[0].endswith('check_z_motion_heartbeat'):
35
-        sp.send(socket_protocol.SID_READ_REQUEST, DID_HEARTBEAT, data)
36
-        sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_HEARTBEAT).get_data()
37
-    elif sys.argv[0].endswith('check_z_motion_battery'):
38
-        sp.send(socket_protocol.SID_READ_REQUEST, DID_BATTERY_LEVEL, data)
39
-        sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_BATTERY_LEVEL).get_data()
40
-    else:
41
-        sys.stderr.write('No action for command called "%s"\n' % sys.argv[0])
42
-        sys.exit(100)
43
-    nagios.Nagios().exit(**sp_data)

+ 0
- 1
check_z_tr_inp_battery 파일 보기

@@ -1 +0,0 @@
1
-check_z_tr_inp_heartbeat

+ 0
- 43
check_z_tr_inp_heartbeat 파일 보기

@@ -1,43 +0,0 @@
1
-#!/bin/python3
2
-#
3
-
4
-import argparse
5
-import nagios
6
-import time
7
-from z_server import config
8
-from z_server import tcp_socket
9
-from z_server.z_protocol import server as client_prot
10
-from z_server.z_protocol import DID_FOLLOWS_HEATING_SETPOINT, DID_BATTERY_LEVEL, DID_HEARTBEAT
11
-from z_server import socket_protocol
12
-import sys
13
-
14
-if __name__ == '__main__':
15
-    parser = argparse.ArgumentParser(
16
-        prog='ProgramName',
17
-        description='What the program does',
18
-        epilog='Text at the bottom of help')
19
-    parser.add_argument('-s', '--stg', required=True)
20
-    parser.add_argument('-l', '--loc', required=True)
21
-    parser.add_argument('-r', '--roo', required=True)
22
-    args = parser.parse_args()
23
-    #
24
-    c = tcp_socket.tcp_client_stp('127.0.0.1', config.SOCK_PROT_PORT)
25
-    sp = client_prot(c, channel_name='example_client')
26
-    #
27
-    data = {
28
-        "stg": args.stg,
29
-        "loc": args.loc,
30
-        "roo": args.roo,
31
-        "fun": "FUN_INP"    # <-- Const, because script is for input_device
32
-    }
33
-    #
34
-    if sys.argv[0].endswith('check_z_tr_inp_heartbeat'):
35
-        sp.send(socket_protocol.SID_READ_REQUEST, DID_HEARTBEAT, data)
36
-        sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_HEARTBEAT).get_data()
37
-    elif sys.argv[0].endswith('check_z_tr_inp_battery'):
38
-        sp.send(socket_protocol.SID_READ_REQUEST, DID_BATTERY_LEVEL, data)
39
-        sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_BATTERY_LEVEL).get_data()
40
-    else:
41
-        sys.stderr.write('No action for command called "%s"\n' % sys.argv[0])
42
-        sys.exit(100)
43
-    nagios.Nagios().exit(**sp_data)

+ 38
- 67
z_server/devices/__init__.py 파일 보기

@@ -14,7 +14,7 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
14 14
 class base(object):
15 15
     FOLLOW_REQUEST_WARNING = 5      # Seconds, till warning comes up, if device does not follow the command
16 16
     FOLLOW_REQUEST_ERROR = 60       # Seconds, till error comes up, if device does not follow the command
17
-    FOLLOW_KEYS = ["current_heating_setpoint", ]
17
+    FOLLOW_KEY = None
18 18
     #
19 19
     BATTERY_LVL_WARNING = 15
20 20
     BATTERY_LVL_ERROR = 5
@@ -36,7 +36,30 @@ class base(object):
36 36
         self.battery = None
37 37
 
38 38
     def __rx__(self, client, userdata, message):
39
-        pass
39
+        try:
40
+            payload = json.loads(message.payload)
41
+        except json.decoder.JSONDecodeError:
42
+            pass
43
+        else:
44
+            if type(payload) is dict:
45
+                #
46
+                # heartbeat
47
+                #
48
+                if message.topic == self.topic:
49
+                    self.last_device_msg = time.time()
50
+                #
51
+                # follow setpoint
52
+                #
53
+                if self.FOLLOW_KEY is not None and self.FOLLOW_KEY in payload:
54
+                    if message.topic == self.topic + '/set':
55
+                        self.target(self.FOLLOW_KEY, payload[self.FOLLOW_KEY])
56
+                    if message.topic == self.topic:
57
+                        self.state(self.FOLLOW_KEY, payload[self.FOLLOW_KEY])
58
+                #
59
+                # battery level
60
+                #
61
+                if "battery" in payload and message.topic == self.topic:
62
+                    self.battery = payload["battery"]
40 63
 
41 64
     def target(self, key, value):
42 65
         tm_t, value_t = self.__target_storage__.get(key, (0, None))
@@ -67,22 +90,24 @@ class base(object):
67 90
         #
68 91
         # FOLLOW SETPOINT
69 92
         #
70
-        elif key in self.FOLLOW_KEYS:
71
-            tm_s, value_s = self.__state_storage__.get(key, (0, None))
93
+        elif key == 'follow_setpoint':
94
+            if self.FOLLOW_KEY is None:
95
+                return {"status": nagios.Nagios.UNKNOWN, "msg": "Device exist, but does not follow any setpoint."}
96
+            tm_s, value_s = self.__state_storage__.get(self.FOLLOW_KEY, (0, None))
72 97
             try:
73
-                tm_t, value_t = self.__target_storage__[key]
98
+                tm_t, value_t = self.__target_storage__[self.FOLLOW_KEY]
74 99
             except KeyError:
75 100
                 if value_s is not None:
76
-                    return {"status": nagios.Nagios.WARNING, "msg": "Current temperature setpoint %.1f°C (age=%.1fs), but never received a setpoint" % (value_s, time.time()-tm_s)}
77
-                return {"status": nagios.Nagios.UNKNOWN, "msg": "Device exists, but no data received or unknown monitoring"}
101
+                    return {"status": nagios.Nagios.WARNING, "msg": "Current temperature setpoint %.1f°C (age=%.1fmin), but never received a setpoint" % (value_s, (time.time()-tm_s)/60)}
102
+                return {"status": nagios.Nagios.UNKNOWN, "msg": "Device exists, but no data received"}
78 103
             else:
79 104
                 tm = time.time()
80 105
                 dt = tm - tm_t
81 106
                 if value_t != value_s and dt > self.FOLLOW_REQUEST_ERROR:
82
-                    return {"status": nagios.Nagios.ERROR, "msg": "Requested setpoint unequal valve setpoint since %.1f°C (age=%.1fs)" % (value_s, time.time()-tm_s)}
107
+                    return {"status": nagios.Nagios.ERROR, "msg": "Requested setpoint unequal valve setpoint %.1f°C since %.1fmin" % (value_s, (time.time()-tm_s)/60)}
83 108
                 elif value_t != value_s and dt > self.FOLLOW_REQUEST_WARNING:
84
-                    return {"status": nagios.Nagios.WARNING, "msg": "Requested setpoint unequal valve setpoint since %.1f°C (age=%.1fs)" % (value_s, time.time()-tm_s)}
85
-                return {"status": nagios.Nagios.OK, "msg": "Requested setpoint equal valve setpoint %.1f°C (age=%.1fs)" % (value_s, time.time()-tm_s)}
109
+                    return {"status": nagios.Nagios.WARNING, "msg": "Requested setpoint unequal valve setpoint %.1f°C since %.1fmin" % (value_s, (time.time()-tm_s))}
110
+                return {"status": nagios.Nagios.OK, "msg": "Requested setpoint equal valve setpoint %.1f°C" % value_s}
86 111
         #
87 112
         # BATTERY
88 113
         #
@@ -119,22 +144,7 @@ class tradfri_sw_br_ct(base):
119 144
 
120 145
 
121 146
 class tradfri_button(base):
122
-    def __rx__(self, client, userdata, message):
123
-        try:
124
-            payload = json.loads(message.payload)
125
-        except json.decoder.JSONDecodeError:
126
-            logger.warning("JSON decode error %s", self.topic)
127
-        else:
128
-            #
129
-            # heartbeat
130
-            #
131
-            if message.topic == self.topic:
132
-                self.last_device_msg = time.time()
133
-            #
134
-            # battery level
135
-            #
136
-            if "battery" in payload and message.topic == self.topic:
137
-                self.battery = payload["battery"]
147
+    pass
138 148
 
139 149
 
140 150
 class livarno_sw_br_ct(base):
@@ -144,31 +154,7 @@ class livarno_sw_br_ct(base):
144 154
 class brennenstuhl_heatingvalve(base):
145 155
     BATTERY_LVL_WARNING = 4
146 156
     BATTERY_LVL_ERROR = 3
147
-
148
-    def __rx__(self, client, userdata, message):
149
-        try:
150
-            payload = json.loads(message.payload)
151
-        except json.decoder.JSONDecodeError:
152
-            logger.warning("JSON decode error %s", self.topic)
153
-        else:
154
-            #
155
-            # heartbeat
156
-            #
157
-            if message.topic == self.topic:
158
-                self.last_device_msg = time.time()
159
-            #
160
-            # follow setpoint
161
-            #
162
-            if "current_heating_setpoint" in payload:
163
-                if message.topic == self.topic + '/set':
164
-                    self.target("current_heating_setpoint", payload["current_heating_setpoint"])
165
-                if message.topic == self.topic:
166
-                    self.state("current_heating_setpoint", payload["current_heating_setpoint"])
167
-            #
168
-            # battery level
169
-            #
170
-            if "battery" in payload and message.topic == self.topic:
171
-                self.battery = payload["battery"]
157
+    FOLLOW_KEY = "current_heating_setpoint"
172 158
 
173 159
 
174 160
 class silvercrest_powerplug(base):
@@ -176,22 +162,7 @@ class silvercrest_powerplug(base):
176 162
 
177 163
 
178 164
 class silvercrest_motion_sensor(base):
179
-    def __rx__(self, client, userdata, message):
180
-        try:
181
-            payload = json.loads(message.payload)
182
-        except json.decoder.JSONDecodeError:
183
-            logger.warning("JSON decode error %s", self.topic)
184
-        else:
185
-            #
186
-            # heartbeat
187
-            #
188
-            if message.topic == self.topic:
189
-                self.last_device_msg = time.time()
190
-            #
191
-            # battery level
192
-            #
193
-            if "battery" in payload and message.topic == self.topic:
194
-                self.battery = payload["battery"]
165
+    pass
195 166
 
196 167
 
197 168
 class my_powerplug(base):

+ 3
- 6
z_server/z_protocol.py 파일 보기

@@ -2,7 +2,7 @@ import nagios
2 2
 import socket_protocol
3 3
 # from devdi.topic import topic_by_props
4 4
 
5
-DID_FOLLOWS_HEATING_SETPOINT = 'current_heating_setpoint'
5
+DID_FOLLOWS_SETPOINT = 'follow_setpoint'
6 6
 DID_BATTERY_LEVEL = 'battery'
7 7
 DID_HEARTBEAT = 'heartbeat'
8 8
 
@@ -13,11 +13,8 @@ class server(socket_protocol.pure_json_protocol):
13 13
             self.__devices__ = kwargs.pop('devices')
14 14
         socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs)
15 15
         #
16
-        # self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE),
17
-        #              DID_FOLLOWS_HEATING_SETPOINT, 'current_heating_setpoint')
18
-        #
19 16
         if not self.__comm_inst__.IS_CLIENT:
20
-            self.register_callback(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_HEATING_SETPOINT, self.device_status)
17
+            self.register_callback(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_SETPOINT, self.device_status)
21 18
             self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BATTERY_LEVEL, self.device_status)
22 19
             self.register_callback(socket_protocol.SID_READ_REQUEST, DID_HEARTBEAT, self.device_status)
23 20
 
@@ -26,7 +23,7 @@ class server(socket_protocol.pure_json_protocol):
26 23
             try:
27 24
                 dev = self.__devices__.get_str(**msg.get_data())
28 25
             except:
29
-                return socket_protocol.STATUS_CALLBACK_ERROR, {"status": nagios.Nagios.UNKNOWN, "msg": "Exception while getting device."}
26
+                return socket_protocol.STATUS_CALLBACK_ERROR, {"status": nagios.Nagios.UNKNOWN, "msg": "Device does not exist."}
30 27
             if dev is None:
31 28
                 return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, {"status": nagios.Nagios.UNKNOWN, "msg": "Device does not exist."}
32 29
             else:

Loading…
취소
저장