add notifications to ui state variables and functionality to delete notifications

This commit is contained in:
avollkopf 2024-05-30 19:01:06 +02:00
parent f594ed04a0
commit e623aa2e5e
4 changed files with 34 additions and 3 deletions

View file

@ -1,3 +1,3 @@
__version__ = "4.4.1.a11"
__version__ = "4.4.1.a12"
__codename__ = "Yeast Starter"

View file

@ -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")

View file

@ -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)
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)

View file

@ -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__)