Send alerts and reset
This commit is contained in:
@@ -104,7 +104,7 @@ def on_microwave_change(state: MicrowaveState, field: MicrowaveStateFields):
|
||||
if field != MicrowaveStateFields.STATE: # No external screen or lcd uses this field, so we can skip updates for it
|
||||
notify_display_update()
|
||||
|
||||
if field == MicrowaveStateFields.STATE and state.state in (MicrowaveState.DONE, MicrowaveState.ANALYZING, MicrowaveState.WAITING_FOR_CLOUD, MicrowaveState.ALERT):
|
||||
if field == MicrowaveStateFields.STATE and state.state in (MicrowaveState.IDLE, MicrowaveState.DONE, MicrowaveState.ANALYZING, MicrowaveState.WAITING_FOR_CLOUD, MicrowaveState.ALERT):
|
||||
# Send to LoRa device if state changed
|
||||
lora.send_reliable(payloads.lora_microwave_state(state.state), max_retries=5)
|
||||
|
||||
@@ -457,11 +457,11 @@ button.start_button_monitoring_thread()
|
||||
# --- Alert manager ---
|
||||
alert_manager = None
|
||||
|
||||
def on_new_alert(alert: Alert):
|
||||
async def on_new_alert(alert: Alert):
|
||||
# Sends to cloud server
|
||||
alert_dict = alert.to_dict()
|
||||
|
||||
logs_list = asyncio.run(get_systemd_logs(lines=200, service_name="smartwave"))
|
||||
logs_list = await get_systemd_logs(lines=200, service_name="smartwave")
|
||||
|
||||
payload = {
|
||||
"orchestrator_id": DEVICE_ID,
|
||||
@@ -485,6 +485,7 @@ def on_new_alert(alert: Alert):
|
||||
print(f"[Alert Handling] Sending alert to LoRa device: {payload}")
|
||||
if lora.send_reliable(payload):
|
||||
microwave_states["2"].set_cooking_state(CookingStates.ALERT)
|
||||
buzzer.buzzer_siren(beatsNb=5)
|
||||
|
||||
# --- HARDWARE CONTROLLERS ---
|
||||
def _stop_hardware(microwave_id: str):
|
||||
|
||||
+16
-2
@@ -1,4 +1,6 @@
|
||||
import time
|
||||
import asyncio
|
||||
import inspect
|
||||
|
||||
class AlertManager:
|
||||
def __init__(self):
|
||||
@@ -8,10 +10,22 @@ class AlertManager:
|
||||
def set_on_alert_callback(self, callback):
|
||||
self.on_alert_callback = callback
|
||||
|
||||
def call_on_alert_callback(self, alert):
|
||||
if self.on_alert_callback:
|
||||
if inspect.iscoroutinefunction(self.on_alert_callback):
|
||||
# Schedule coroutine on the running loop without blocking
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
loop.create_task(self.on_alert_callback(alert))
|
||||
except RuntimeError:
|
||||
# Fallback if called outside an active event loop
|
||||
asyncio.run(self.on_alert_callback(alert))
|
||||
else:
|
||||
self.on_alert_callback(alert)
|
||||
|
||||
def add_alert(self, alert):
|
||||
self.alerts.append(alert)
|
||||
if self.on_alert_callback:
|
||||
self.on_alert_callback(alert)
|
||||
self.call_on_alert_callback(alert)
|
||||
|
||||
def get_alerts(self):
|
||||
return self.alerts
|
||||
|
||||
Reference in New Issue
Block a user