From 9e50b790e0b230edc5bc2b061fffedc5d88144c0 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Mon, 23 Jan 2023 07:15:29 +0100 Subject: [PATCH] aded sensor name and timestamp of last data to notification --- cbpi/__init__.py | 2 +- cbpi/extension/httpsensor/__init__.py | 7 ++++++- cbpi/extension/mqtt_sensor/__init__.py | 7 ++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 023dc6d..6c0ca09 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.1.0.rc4" +__version__ = "4.1.0.rc5" __codename__ = "Groundhog Day" diff --git a/cbpi/extension/httpsensor/__init__.py b/cbpi/extension/httpsensor/__init__.py index 7da1cdf..0abd2a4 100644 --- a/cbpi/extension/httpsensor/__init__.py +++ b/cbpi/extension/httpsensor/__init__.py @@ -3,6 +3,7 @@ import asyncio from aiohttp import web from cbpi.api import * import time +from datetime import datetime import re import logging from cbpi.api.dataclasses import NotificationAction, NotificationType @@ -21,6 +22,8 @@ class HTTPSensor(CBPiSensor): self.starttime = time.time() self.notificationsend = False self.nextchecktime=self.starttime+self.timeout + self.sensor=self.get_sensor(self.id) + self.lastdata=time.time() async def Confirm(self, **kwargs): self.nextchecktime = time.time() + self.timeout @@ -28,7 +31,8 @@ class HTTPSensor(CBPiSensor): pass async def message(self): - self.cbpi.notify("HTTPSensor Timeout", "Sensor " + str(self.id) + " did not respond", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) + target_timestring= datetime.fromtimestamp(self.lastdata) + self.cbpi.notify("HTTPSensor Timeout", "Sensor '" + str(self.sensor.name) + "' did not respond. Last data received: "+target_timestring.strftime("%D %H:%M"), NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) pass async def run(self): @@ -50,6 +54,7 @@ class HTTPSensor(CBPiSensor): if self.timeout !=0: self.nextchecktime = currenttime + self.timeout self.notificationsend = False + self.lastdata=time.time() except Exception as e: logging.error(e) pass diff --git a/cbpi/extension/mqtt_sensor/__init__.py b/cbpi/extension/mqtt_sensor/__init__.py index e121eb8..c50a776 100644 --- a/cbpi/extension/mqtt_sensor/__init__.py +++ b/cbpi/extension/mqtt_sensor/__init__.py @@ -6,6 +6,7 @@ from cbpi.api import * import logging import json import time +from datetime import datetime @parameters([Property.Text(label="Topic", configurable=True, description="MQTT Topic"), Property.Text(label="PayloadDictionary", configurable=True, default_value="", @@ -26,6 +27,8 @@ class MQTTSensor(CBPiSensor): self.starttime = time.time() self.notificationsend = False self.nextchecktime=self.starttime+self.timeout + self.lastdata=time.time() + self.sensor=self.get_sensor(self.id) async def Confirm(self, **kwargs): self.nextchecktime = time.time() + self.timeout @@ -33,7 +36,8 @@ class MQTTSensor(CBPiSensor): pass async def message(self): - self.cbpi.notify("MQTTSensor Timeout", "Sensor " + str(self.Topic) + " did not respond", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) + target_timestring= datetime.fromtimestamp(self.lastdata) + self.cbpi.notify("MQTTSensor Timeout", "Sensor '" + str(self.sensor.name) + "' did not respond. Last data received: "+target_timestring.strftime("%D %H:%M"), NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) pass async def on_message(self, message): @@ -50,6 +54,7 @@ class MQTTSensor(CBPiSensor): if self.timeout !=0: self.nextchecktime = time.time() + self.timeout self.notificationsend = False + self.lastdata=time.time() except Exception as e: logging.info("MQTT Sensor Error {}".format(e))