Pārlūkot izejas kodu

z_server: Extended HEARTBEAT monitoring for my ambient information added

master
Dirk Alders 4 mēnešus atpakaļ
vecāks
revīzija
a1eb3275b4
2 mainītis faili ar 55 papildinājumiem un 1 dzēšanām
  1. 1
    1
      z_server/devdi
  2. 54
    0
      z_server/devices/__init__.py

+ 1
- 1
z_server/devdi

@@ -1 +1 @@
1
-Subproject commit 0620158064ab5bbb932c2afb9f557cf0b18b37e3
1
+Subproject commit 0f5ad2a18a72191e4fbf5ec49c5a41552f0c2296

+ 54
- 0
z_server/devices/__init__.py Parādīt failu

@@ -247,3 +247,57 @@ class audio_status(base):
247 247
 
248 248
 class remote(base):
249 249
     pass
250
+
251
+
252
+class my_ambient(base):
253
+    KEY_TEMPERATURE = 'temperature'
254
+    KEY_PRESSURE = 'pressure'
255
+    KEY_HUMIDITY = 'humidity'
256
+    #
257
+    LAST_MSG_WARNING = 20 * 60
258
+    LAST_MSG_ERROR = 30 * 60
259
+
260
+    def __init__(self, mqtt_client: mqtt.mqtt_client, topic):
261
+        super().__init__(mqtt_client, topic)
262
+        self.__last_value_rx__ = {
263
+            self.KEY_HUMIDITY: None,
264
+            self.KEY_PRESSURE: None,
265
+            self.KEY_TEMPERATURE: None
266
+        }
267
+
268
+    def __rx__(self, client, userdata, message):
269
+        try:
270
+            payload = json.loads(message.payload)
271
+        except json.decoder.JSONDecodeError:
272
+            pass
273
+        else:
274
+            key = message.topic.split('/')[-1]
275
+            if key in self.__last_value_rx__:
276
+                self.__last_value_rx__[key] = time.time()
277
+        return super().__rx__(client, userdata, message)
278
+
279
+    def status(self, key):
280
+        if key == DID_HEARTBEAT:
281
+            status = nagios.Nagios.OK
282
+            msg = ""
283
+            for key in self.__last_value_rx__:
284
+                last_value_rx = self.__last_value_rx__[key]
285
+                if len(msg) != 0:
286
+                    msg += "; "
287
+                if last_value_rx is None:
288
+                    target_status = nagios.Nagios.UNKNOWN
289
+                    msg += "%s (never)" % key
290
+                else:
291
+                    dt = time.time() - last_value_rx
292
+                    msg += "%s (%.1f min ago)" % (key, dt / 60)
293
+                    if dt > self.LAST_MSG_ERROR:
294
+                        target_status = nagios.Nagios.ERROR
295
+                    elif dt > self.LAST_MSG_WARNING:
296
+                        target_status = nagios.Nagios.WARNING
297
+                    else:
298
+                        target_status = nagios.Nagios.OK
299
+            if target_status > status:
300
+                status = target_status
301
+            return self.__nagios_return__(DID_HEARTBEAT, status, msg)
302
+        else:
303
+            return super().status(key)

Notiek ielāde…
Atcelt
Saglabāt