diff --git a/z_server/devdi b/z_server/devdi index 0620158..0f5ad2a 160000 --- a/z_server/devdi +++ b/z_server/devdi @@ -1 +1 @@ -Subproject commit 0620158064ab5bbb932c2afb9f557cf0b18b37e3 +Subproject commit 0f5ad2a18a72191e4fbf5ec49c5a41552f0c2296 diff --git a/z_server/devices/__init__.py b/z_server/devices/__init__.py index f16cd71..2537a59 100644 --- a/z_server/devices/__init__.py +++ b/z_server/devices/__init__.py @@ -247,3 +247,57 @@ class audio_status(base): class remote(base): pass + + +class my_ambient(base): + KEY_TEMPERATURE = 'temperature' + KEY_PRESSURE = 'pressure' + KEY_HUMIDITY = 'humidity' + # + LAST_MSG_WARNING = 20 * 60 + LAST_MSG_ERROR = 30 * 60 + + def __init__(self, mqtt_client: mqtt.mqtt_client, topic): + super().__init__(mqtt_client, topic) + self.__last_value_rx__ = { + self.KEY_HUMIDITY: None, + self.KEY_PRESSURE: None, + self.KEY_TEMPERATURE: None + } + + def __rx__(self, client, userdata, message): + try: + payload = json.loads(message.payload) + except json.decoder.JSONDecodeError: + pass + else: + key = message.topic.split('/')[-1] + if key in self.__last_value_rx__: + self.__last_value_rx__[key] = time.time() + return super().__rx__(client, userdata, message) + + def status(self, key): + if key == DID_HEARTBEAT: + status = nagios.Nagios.OK + msg = "" + for key in self.__last_value_rx__: + last_value_rx = self.__last_value_rx__[key] + if len(msg) != 0: + msg += "; " + if last_value_rx is None: + target_status = nagios.Nagios.UNKNOWN + msg += "%s (never)" % key + else: + dt = time.time() - last_value_rx + msg += "%s (%.1f min ago)" % (key, dt / 60) + if dt > self.LAST_MSG_ERROR: + target_status = nagios.Nagios.ERROR + elif dt > self.LAST_MSG_WARNING: + target_status = nagios.Nagios.WARNING + else: + target_status = nagios.Nagios.OK + if target_status > status: + status = target_status + return self.__nagios_return__(DID_HEARTBEAT, status, msg) + else: + return super().status(key)