diff --git a/cbpi/__init__.py b/cbpi/__init__.py index c772fb0..94300f0 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.4.1.a11" +__version__ = "4.4.1.a12" __codename__ = "Yeast Starter" diff --git a/cbpi/controller/notification_controller.py b/cbpi/controller/notification_controller.py index 1040bf0..5cbd20c 100644 --- a/cbpi/controller/notification_controller.py +++ b/cbpi/controller/notification_controller.py @@ -4,6 +4,7 @@ from cbpi.api.dataclasses import NotificationType from cbpi.api import * import logging import shortuuid +from datetime import datetime class NotificationController: def __init__(self, cbpi): @@ -15,6 +16,9 @@ class NotificationController: logging.root.addFilter(self.notify_log_event) self.callback_cache = {} self.listener = {} + self.notifications = [] + self.update_key="notificationupdate" + self.sorting=False def notify_log_event(self, record): NOTIFY_ON_ERROR = self.cbpi.config.get("NOTIFY_ON_ERROR", "No") @@ -32,6 +36,10 @@ class NotificationController: return True return True + def get_state(self): + result = self.notifications + return result + def add_listener(self, method): listener_id = shortuuid.uuid() self.listener[listener_id] = method @@ -69,8 +77,16 @@ class NotificationController: self.cbpi.ws.send(dict(id=notifcation_id, topic="notifiaction", type=type.value, title=title, message=message, action=actions, timeout=timeout)) data = dict(type=type.value, title=title, message=message, action=actions, timeout=timeout) self.cbpi.push_update(topic="cbpi/notification", data=data) + self.notifications.insert(0,[f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}: {title} | {message}']) + if len(self.notifications) > 100: + self.notifications = self.notifications[:100] + self.cbpi.ws.send(dict(topic=self.update_key, data=self.notifications),self.sorting) asyncio.create_task(self._call_listener(title, message, type, action)) + def delete_all_notifications(self): + self.notifications = [] + self.cbpi.ws.send(dict(topic=self.update_key, data=self.notifications),self.sorting) + def notify_callback(self, notification_id, action_id) -> None: try: @@ -79,5 +95,5 @@ class NotificationController: asyncio.create_task(action.method()) del self.callback_cache[notification_id] except Exception as e: - self.logger.error("Failed to call notificatoin callback") + self.logger.error("Failed to call notification callback") \ No newline at end of file diff --git a/cbpi/http_endpoints/http_notification.py b/cbpi/http_endpoints/http_notification.py index b25c4e3..4fd0eff 100644 --- a/cbpi/http_endpoints/http_notification.py +++ b/cbpi/http_endpoints/http_notification.py @@ -37,4 +37,18 @@ class NotificationHttpEndpoints: action_id = request.match_info['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 + return web.Response(status=200) + + @request_mapping("/delete", method="POST", auth_required=False) + async def restart(self, request): + """ + --- + description: DeleteNotifications + tags: + - Notification + responses: + "200": + description: successful operation + """ + self.cbpi.notification.delete_all_notifications() + return web.Response(status=200) \ No newline at end of file diff --git a/cbpi/http_endpoints/http_system.py b/cbpi/http_endpoints/http_system.py index 906d49e..7a556d2 100644 --- a/cbpi/http_endpoints/http_system.py +++ b/cbpi/http_endpoints/http_system.py @@ -42,6 +42,7 @@ class SystemHttpEndpoints: step=self.cbpi.step.get_state(), fermentersteps=self.cbpi.fermenter.get_fermenter_steps(), config=self.cbpi.config.get_state(), + notifications=self.cbpi.notification.get_state(), version=__version__, guiversion=version, codename=__codename__)