craftbeerpi4-pione/cbpi/controller/notification_controller.py

111 lines
4.3 KiB
Python
Raw Permalink Normal View History

2021-03-07 22:11:25 +01:00
import asyncio
from email import message
2021-03-14 11:52:46 +01:00
from cbpi.api.dataclasses import NotificationType
2022-12-02 20:05:30 +01:00
from cbpi.api import *
2021-03-07 22:11:25 +01:00
import logging
import shortuuid
from datetime import datetime
import os
2021-03-07 22:11:25 +01:00
class NotificationController:
def __init__(self, cbpi):
'''
:param cbpi: craftbeerpi object
'''
self.cbpi = cbpi
self.logger = logging.getLogger(__name__)
2022-12-02 20:25:27 +01:00
logging.root.addFilter(self.notify_log_event)
2021-03-14 11:52:46 +01:00
self.callback_cache = {}
self.listener = {}
self.notifications = []
self.update_key="notificationupdate"
self.sorting=False
self.check_startup_message()
def check_startup_message(self):
self.restore_error = self.cbpi.config_folder.get_file_path("restore_error.log")
try:
with open(self.restore_error) as f:
for line in f:
self.notifications.insert(0,[f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}: Restore Error | {line}'])
os.remove(self.restore_error)
except Exception as e:
pass
2021-03-14 11:52:46 +01:00
def notify_log_event(self, record):
2022-12-02 20:25:27 +01:00
NOTIFY_ON_ERROR = self.cbpi.config.get("NOTIFY_ON_ERROR", "No")
if NOTIFY_ON_ERROR == "Yes":
try:
if record.levelno > 20:
# on log events higher then INFO we want to notify all clients
type = NotificationType.WARNING
if record.levelno > 30:
type = NotificationType.ERROR
self.cbpi.notify(title=f"{record.levelname}", message=record.msg, type = type)
except Exception as e:
pass
finally:
return True
return True
def get_state(self):
result = self.notifications
return result
2021-03-14 11:52:46 +01:00
def add_listener(self, method):
listener_id = shortuuid.uuid()
self.listener[listener_id] = method
return listener_id
def remove_listener(self, listener_id):
try:
del self.listener[listener_id]
except:
2022-04-02 16:27:09 +02:00
self.logger.error("Failed to remove listener {}".format(listener_id))
2021-03-14 11:52:46 +01:00
async def _call_listener(self, title, message, type, action):
for id, method in self.listener.items():
2023-01-22 16:37:10 +01:00
#print(id, method)
2021-03-14 11:52:46 +01:00
asyncio.create_task(method(self.cbpi, title, message, type, action ))
2021-03-07 22:11:25 +01:00
2021-03-14 11:52:46 +01:00
def notify(self, title, message: str, type: NotificationType = NotificationType.INFO, action=[], timeout: int=5000) -> None:
2021-03-07 22:11:25 +01:00
'''
This is a convinience method to send notification to the client
:param key: notification key
:param message: notification message
:param type: notification type (info,warning,danger,successs)
:return:
'''
notifcation_id = shortuuid.uuid()
2021-03-14 11:52:46 +01:00
2021-03-07 22:11:25 +01:00
def prepare_action(item):
item.id = shortuuid.uuid()
return item.to_dict()
actions = list(map(lambda item: prepare_action(item), action))
self.callback_cache[notifcation_id] = action
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)
2021-03-14 11:52:46 +01:00
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)
2021-03-14 11:52:46 +01:00
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)
2021-03-07 22:11:25 +01:00
def notify_callback(self, notification_id, action_id) -> None:
try:
action = next((item for item in self.callback_cache[notification_id] if item.id == action_id), None)
if action.method is not None:
asyncio.create_task(action.method())
del self.callback_cache[notification_id]
except Exception as e:
self.logger.error("Failed to call notification callback")
2021-03-07 22:11:25 +01:00