Alerts
This commit is contained in:
@@ -6,6 +6,7 @@ import shared.config as config
|
||||
import shared.payloads as payloads
|
||||
import shared.cookingState as cookingState
|
||||
import shared.safeQueue as safeQueue
|
||||
import shared.alerts as alerts
|
||||
try:
|
||||
import shared.lora_device as lora_device
|
||||
except ImportError:
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
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"
|
||||
+13
-1
@@ -1,5 +1,11 @@
|
||||
from time import time
|
||||
from shared.deviceTypes import DEVICE_TYPES
|
||||
from shared.alerts import Alert, AlertType
|
||||
import shared.config as config
|
||||
try:
|
||||
from shared.lora_device import LoraCommands
|
||||
except ImportError:
|
||||
pass # No need
|
||||
|
||||
|
||||
try:
|
||||
@@ -65,4 +71,10 @@ def telemetry_payload(device_id, microwave_states, button_state, cloud_alert, gp
|
||||
"ambient_humidity": ambient_humidity,
|
||||
"connected_components": connected_components,
|
||||
"logs": logs
|
||||
})
|
||||
})
|
||||
|
||||
def lora_new_alert(alert: Alert):
|
||||
return {
|
||||
"action": LoraCommands.NEW_ALERT,
|
||||
"alert_type": alert.alertType
|
||||
}
|
||||
Reference in New Issue
Block a user