ソースを参照

Battery Levels adapted and battery driven devices added

master
Dirk Alders 1年前
コミット
c21bbf4716
5個のファイルの変更148行の追加29行の削除
  1. 1
    0
      check_z_motion_battery
  2. 43
    0
      check_z_motion_heartbeat
  3. 1
    0
      check_z_tr_inp_battery
  4. 43
    0
      check_z_tr_inp_heartbeat
  5. 60
    29
      z_server/devices/__init__.py

+ 1
- 0
check_z_motion_battery ファイルの表示

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

+ 43
- 0
check_z_motion_heartbeat ファイルの表示

@@ -0,0 +1,43 @@
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)

+ 1
- 0
check_z_tr_inp_battery ファイルの表示

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

+ 43
- 0
check_z_tr_inp_heartbeat ファイルの表示

@@ -0,0 +1,43 @@
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)

+ 60
- 29
z_server/devices/__init__.py ファイルの表示

@@ -16,8 +16,8 @@ class base(object):
16 16
     FOLLOW_REQUEST_ERROR = 60       # Seconds, till error comes up, if device does not follow the command
17 17
     FOLLOW_KEYS = ["current_heating_setpoint", ]
18 18
     #
19
-    BATTERY_LVL_WARNING = 20
20
-    BATTERY_LVL_ERROR = 10
19
+    BATTERY_LVL_WARNING = 15
20
+    BATTERY_LVL_ERROR = 5
21 21
     #
22 22
     LAST_MSG_WARNING = 6 * 24 * 60 * 60
23 23
     LAST_MSG_ERROR = 24 * 24 * 60 * 60
@@ -89,9 +89,9 @@ class base(object):
89 89
         elif key == "battery":
90 90
             if self.battery is None:
91 91
                 return {"status": nagios.Nagios.UNKNOWN, "msg": "Device exists, but no data received or unknown monitoring"}
92
-            elif self.battery < self.BATTERY_LVL_ERROR:
92
+            elif self.battery <= self.BATTERY_LVL_ERROR:
93 93
                 return {"status": nagios.Nagios.ERROR, "msg": "Battery level critical low (%.1f%%)" % self.battery}
94
-            elif self.battery < self.BATTERY_LVL_WARNING:
94
+            elif self.battery <= self.BATTERY_LVL_WARNING:
95 95
                 return {"status": nagios.Nagios.WARNING, "msg": "Battery level low (%.1f%%)" % self.battery}
96 96
             else:
97 97
                 return {"status": nagios.Nagios.OK, "msg": "Battery okay (%.1f%%)" % self.battery}
@@ -119,7 +119,22 @@ class tradfri_sw_br_ct(base):
119 119
 
120 120
 
121 121
 class tradfri_button(base):
122
-    pass
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"]
123 138
 
124 139
 
125 140
 class livarno_sw_br_ct(base):
@@ -127,32 +142,33 @@ class livarno_sw_br_ct(base):
127 142
 
128 143
 
129 144
 class brennenstuhl_heatingvalve(base):
130
-    BATTERY_LVL_WARNING = 10
131
-    BATTERY_LVL_ERROR = 5
132
-
133
-    def __init__(self, mqtt_client: mqtt.mqtt_client, topic):
134
-        base.__init__(self, mqtt_client, topic)
145
+    BATTERY_LVL_WARNING = 4
146
+    BATTERY_LVL_ERROR = 3
135 147
 
136 148
     def __rx__(self, client, userdata, message):
137
-        payload = json.loads(message.payload)
138
-        #
139
-        # heartbeat
140
-        #
141
-        if message.topic == self.topic:
142
-            self.last_device_msg = time.time()
143
-        #
144
-        # follow setpoint
145
-        #
146
-        if "current_heating_setpoint" in payload:
147
-            if message.topic == self.topic + '/set':
148
-                self.target("current_heating_setpoint", payload["current_heating_setpoint"])
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
+            #
149 157
             if message.topic == self.topic:
150
-                self.state("current_heating_setpoint", payload["current_heating_setpoint"])
151
-        #
152
-        # battery level
153
-        #
154
-        if "battery" in payload and message.topic == self.topic:
155
-            self.battery = payload["battery"]
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"]
156 172
 
157 173
 
158 174
 class silvercrest_powerplug(base):
@@ -160,7 +176,22 @@ class silvercrest_powerplug(base):
160 176
 
161 177
 
162 178
 class silvercrest_motion_sensor(base):
163
-    pass
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"]
164 195
 
165 196
 
166 197
 class my_powerplug(base):

読み込み中…
キャンセル
保存