47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
import time
|
|
|
|
class AlertManager:
|
|
def __init__(self):
|
|
self.alerts = []
|
|
self.on_alert_callback = None
|
|
|
|
def set_on_alert_callback(self, callback):
|
|
self.on_alert_callback = callback
|
|
|
|
def add_alert(self, alert):
|
|
self.alerts.append(alert)
|
|
if self.on_alert_callback:
|
|
self.on_alert_callback(alert)
|
|
|
|
def get_alerts(self):
|
|
return self.alerts
|
|
|
|
def clear_alerts(self):
|
|
self.alerts.clear()
|
|
|
|
class Alert:
|
|
def __init__(self, alertType: "AlertType", message: str):
|
|
self.alertType = alertType
|
|
self.message = message
|
|
self.timestamp = time.time()
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"type": self.alertType,
|
|
"message": self.message,
|
|
"timestamp": self.timestamp
|
|
}
|
|
|
|
def to_json(self):
|
|
import json
|
|
return json.dumps(self.to_dict())
|
|
|
|
def from_json(json_string: str) -> "Alert":
|
|
import json
|
|
data = json.loads(json_string)
|
|
alert = Alert(data["type"], data["message"])
|
|
alert.timestamp = data["timestamp"]
|
|
return alert
|
|
|
|
class AlertType:
|
|
COOKING_SAFETY = "COOKING_SAFETY" |