From 47e3bcb5297565435349eaf175e2320397a68709 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Sun, 22 Jan 2023 16:37:10 +0100 Subject: [PATCH] added alarm to httpsensor on timeout --- cbpi/__init__.py | 2 +- cbpi/controller/dashboard_controller.py | 2 +- cbpi/controller/notification_controller.py | 2 +- cbpi/controller/satellite_controller.py | 1 - cbpi/extension/httpsensor/__init__.py | 30 ++++++++++++++++++--- cbpi/http_endpoints/http_actor.py | 2 +- cbpi/http_endpoints/http_dashboard.py | 2 +- cbpi/http_endpoints/http_fermenterrecipe.py | 4 +-- cbpi/http_endpoints/http_log.py | 6 ++--- cbpi/http_endpoints/http_notification.py | 2 +- cbpi/http_endpoints/http_recipe.py | 4 +-- cbpi/http_endpoints/http_sensor.py | 2 +- cbpi/utils/encoder.py | 4 +-- 13 files changed, 42 insertions(+), 21 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 3330e38..ecee302 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.1.0.rc2" +__version__ = "4.1.0.rc3" __codename__ = "Groundhog Day" diff --git a/cbpi/controller/dashboard_controller.py b/cbpi/controller/dashboard_controller.py index feef806..2bb77b3 100644 --- a/cbpi/controller/dashboard_controller.py +++ b/cbpi/controller/dashboard_controller.py @@ -34,7 +34,7 @@ class DashboardController: return {'elements': [], 'pathes': []} async def add_content(self, dashboard_id, data): - print(data) + #print(data) self.path = self.cbpi.config_folder.get_dashboard_path("cbpi_dashboard_" + str(dashboard_id)+ ".json") with open(self.path, 'w') as outfile: json.dump(data, outfile, indent=4, sort_keys=True) diff --git a/cbpi/controller/notification_controller.py b/cbpi/controller/notification_controller.py index 49b1ac5..1040bf0 100644 --- a/cbpi/controller/notification_controller.py +++ b/cbpi/controller/notification_controller.py @@ -45,7 +45,7 @@ class NotificationController: async def _call_listener(self, title, message, type, action): for id, method in self.listener.items(): - print(id, method) + #print(id, method) asyncio.create_task(method(self.cbpi, title, message, type, action )) diff --git a/cbpi/controller/satellite_controller.py b/cbpi/controller/satellite_controller.py index c5856be..d11885e 100644 --- a/cbpi/controller/satellite_controller.py +++ b/cbpi/controller/satellite_controller.py @@ -40,7 +40,6 @@ class SatelliteController: #not sure if required like done in the old routine async def cancel_tasks(tasks): for task in tasks: - print("3232") if task.done(): continue task.cancel() diff --git a/cbpi/extension/httpsensor/__init__.py b/cbpi/extension/httpsensor/__init__.py index 3b89c69..5b77b6a 100644 --- a/cbpi/extension/httpsensor/__init__.py +++ b/cbpi/extension/httpsensor/__init__.py @@ -2,19 +2,34 @@ import asyncio from aiohttp import web from cbpi.api import * - +import time import re -import random - +import logging +from cbpi.api.dataclasses import NotificationAction, NotificationType cache = {} -@parameters([Property.Text(label="Key", configurable=True, description="Http Key")]) +@parameters([Property.Text(label="Key", configurable=True, description="Http Key"), + Property.Number(label="Timeout", configurable="True",unit="sec",description="Timeout in seconds to send notification (default:60)") +]) class HTTPSensor(CBPiSensor): def __init__(self, cbpi, id, props): super(HTTPSensor, self).__init__(cbpi, id, props) self.running = True self.value = 0 + self.timeout=int(self.props.get("Timeout", 60)) + self.starttime = time.time() + self.notificationsend = False + self.nextchecktime=self.starttime+self.timeout + + async def Confirm(self, **kwargs): + self.nextchecktime = time.time() + self.timeout + self.notificationsend = False + pass + + async def message(self): + self.cbpi.notify("HTTPSensor Timeout", "Sensor " + str(self.id) + " did not respond", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) + pass async def run(self): ''' @@ -22,12 +37,19 @@ class HTTPSensor(CBPiSensor): In this example the code is executed every second ''' while self.running is True: + currenttime=time.time() + if currenttime > self.nextchecktime and self.notificationsend == False: + await self.message() + self.notificationsend=True try: cache_value = cache.pop(self.props.get("Key"), None) if cache_value is not None: self.value = float(cache_value) self.push_update(self.value) + self.nextchecktime = currenttime + self.timeout + self.notificationsend = False except Exception as e: + logging.error(e) pass await asyncio.sleep(1) diff --git a/cbpi/http_endpoints/http_actor.py b/cbpi/http_endpoints/http_actor.py index 7738a5a..d3bec9b 100644 --- a/cbpi/http_endpoints/http_actor.py +++ b/cbpi/http_endpoints/http_actor.py @@ -247,7 +247,7 @@ class ActorHttpEndpoints(): """ actor_id = request.match_info['id'] data = await request.json() - print(data) + #print(data) await self.controller.call_action(actor_id, data.get("action"), data.get("parameter")) return web.Response(status=204) \ No newline at end of file diff --git a/cbpi/http_endpoints/http_dashboard.py b/cbpi/http_endpoints/http_dashboard.py index f24fe0c..38073cf 100644 --- a/cbpi/http_endpoints/http_dashboard.py +++ b/cbpi/http_endpoints/http_dashboard.py @@ -69,7 +69,7 @@ class DashBoardHttpEndpoints: data = await request.json() dashboard_id = int(request.match_info['id']) await self.cbpi.dashboard.add_content(dashboard_id, data) - print("##### SAVE") + #print("##### SAVE") return web.Response(status=204) @request_mapping(path="/{id:\d+}/content", method="DELETE", auth_required=False) diff --git a/cbpi/http_endpoints/http_fermenterrecipe.py b/cbpi/http_endpoints/http_fermenterrecipe.py index e974e3e..e653d76 100644 --- a/cbpi/http_endpoints/http_fermenterrecipe.py +++ b/cbpi/http_endpoints/http_fermenterrecipe.py @@ -58,7 +58,7 @@ class FermenterRecipeHttpEndpoints(): description: successful operation """ data = await request.json() - print(data) + #print(data) return web.json_response(dict(id=await self.controller.create(data.get("name")))) @@ -90,7 +90,7 @@ class FermenterRecipeHttpEndpoints(): data = await request.json() name = request.match_info['name'] await self.controller.save(name, data) - print(data) + #print(data) return web.Response(status=204) @request_mapping(path="/{name}", method="DELETE", auth_required=False) diff --git a/cbpi/http_endpoints/http_log.py b/cbpi/http_endpoints/http_log.py index efc716a..8183edb 100644 --- a/cbpi/http_endpoints/http_log.py +++ b/cbpi/http_endpoints/http_log.py @@ -239,7 +239,7 @@ class LogHttpEndpoints: data = await request.json() result = await self.cbpi.log.get_data(data) - print("JSON") - print(json.dumps(result, cls=ComplexEncoder)) - print("JSON----") + #print("JSON") + #print(json.dumps(result, cls=ComplexEncoder)) + #print("JSON----") return web.json_response(result, dumps=json_dumps) diff --git a/cbpi/http_endpoints/http_notification.py b/cbpi/http_endpoints/http_notification.py index b61e6f2..b25c4e3 100644 --- a/cbpi/http_endpoints/http_notification.py +++ b/cbpi/http_endpoints/http_notification.py @@ -35,6 +35,6 @@ class NotificationHttpEndpoints: notification_id = request.match_info['id'] action_id = request.match_info['action_id'] - print(notification_id, action_id) + #print(notification_id, action_id) self.cbpi.notification.notify_callback(notification_id, action_id) return web.Response(status=204) \ No newline at end of file diff --git a/cbpi/http_endpoints/http_recipe.py b/cbpi/http_endpoints/http_recipe.py index 734149b..9b8a6cc 100644 --- a/cbpi/http_endpoints/http_recipe.py +++ b/cbpi/http_endpoints/http_recipe.py @@ -57,7 +57,7 @@ class RecipeHttpEndpoints(): description: successful operation """ data = await request.json() - print(data) + #print(data) return web.json_response(dict(id=await self.controller.create(data.get("name")))) @@ -89,7 +89,7 @@ class RecipeHttpEndpoints(): data = await request.json() name = request.match_info['name'] await self.controller.save(name, data) - print(data) + #print(data) return web.Response(status=204) @request_mapping(path="/{name}", method="DELETE", auth_required=False) diff --git a/cbpi/http_endpoints/http_sensor.py b/cbpi/http_endpoints/http_sensor.py index bea7c08..d47aa9a 100644 --- a/cbpi/http_endpoints/http_sensor.py +++ b/cbpi/http_endpoints/http_sensor.py @@ -224,7 +224,7 @@ class SensorHttpEndpoints(): """ sensor_id = request.match_info['id'] data = await request.json() - print(data) + #print(data) await self.controller.call_action(sensor_id, data.get("action"), data.get("parameter")) return web.Response(status=204) diff --git a/cbpi/utils/encoder.py b/cbpi/utils/encoder.py index 616d3a9..475ec4d 100644 --- a/cbpi/utils/encoder.py +++ b/cbpi/utils/encoder.py @@ -13,10 +13,10 @@ class ComplexEncoder(JSONEncoder): elif isinstance(obj, datetime.datetime): return obj.__str__() elif isinstance(obj, Timestamp): - print("TIMe") + #print("TIMe") return obj.__str__() else: - print(type(obj)) + #print(type(obj)) raise TypeError() except Exception as e: