Temperature Alert
This commit is contained in:
@@ -17,6 +17,7 @@ from shared.logging import log
|
||||
from shared.lora_device import LoraCommands
|
||||
from shared.alerts import Alert, AlertType, AlertManager
|
||||
from shared.microwave_state import MicrowaveState
|
||||
from shared.payloads import lora_new_alert
|
||||
from lib.microwaveScreen import MicrowaveScreen
|
||||
|
||||
# --- READ DEVICE ID ---
|
||||
@@ -76,7 +77,7 @@ def send_cooking_update(state=None, start_time=None, estimated_remaining_time=No
|
||||
def on_new_alert(alert: Alert):
|
||||
global microwave_screen
|
||||
print(f"[Alert Manager] New alert received: {alert.to_dict()}")
|
||||
microwave_screen.message(alert.message, True)
|
||||
microwave_screen.message(alert.small_message if alert.small_message else alert.message, True)
|
||||
|
||||
def init_hardware():
|
||||
"""Initializes all hardware components."""
|
||||
@@ -131,7 +132,7 @@ async def lora_rx_and_heartbeat_task():
|
||||
|
||||
# 2. Listen for incoming packets (Must be longer than LoRa Time-on-Air)
|
||||
if lora:
|
||||
paquet = lora.receive_reliable(timeout_ms=350)
|
||||
paquet = lora.receive_reliable(timeout_ms=500)
|
||||
if paquet is not None:
|
||||
log(f"[LoRa Task] New Packet Received: {paquet}")
|
||||
data_queue.put(paquet)
|
||||
@@ -169,6 +170,21 @@ def cooking_state_temperature_provider():
|
||||
temps = [float(current_temp[0]), float(current_temp[1])]
|
||||
last_temp = [temps[0], temps[1]]
|
||||
current_temp = [None, None]
|
||||
|
||||
if config.DEBUG_TEMPERATURE_ALERT or temps[0] > config.MAX_ALLOWED_TEMPERATURE_COOKING_COMPARTMENT or temps[1] > config.MAX_ALLOWED_TEMPERATURE_TECHNICAL_COMPARTMENT:
|
||||
print(f"[CookingState] ALERT: Unsafe temperature detected! Dish: {temps[0]}°C, Ambient: {temps[1]}°C")
|
||||
# Send alert to orchestrator via LoRa
|
||||
alert = Alert(
|
||||
AlertType.TEMPERATURE_ALERT,
|
||||
f"Unsafe temperature detected! Dish: {temps[0]}/{config.MAX_ALLOWED_TEMPERATURE_COOKING_COMPARTMENT}°C, Ambient: {temps[1]}/{config.MAX_ALLOWED_TEMPERATURE_TECHNICAL_COMPARTMENT}°C",
|
||||
small_message=f"Temp too high"
|
||||
)
|
||||
alert_manager.add_alert(alert)
|
||||
if lora:
|
||||
lora.send_reliable(lora_new_alert(alert, with_message=True), max_retries=5)
|
||||
cooking_state.set_state(cookingState.CookingStates.ALERT)
|
||||
cooking_state.pause()
|
||||
|
||||
return temps
|
||||
|
||||
# 4. Fallback: Use last valid reading
|
||||
@@ -182,7 +198,7 @@ def cooking_state_on_state_change(state):
|
||||
global cooking_start_time
|
||||
print(f"[CookingState] State changed to: {state.state}")
|
||||
|
||||
if state.paused or state.state == cookingState.CookingStates.DONE or state.state == cookingState.CookingStates.IDLE:
|
||||
if state.paused or state.state in (cookingState.CookingStates.DONE, cookingState.CookingStates.IDLE, cookingState.CookingStates.ALERT):
|
||||
magnetron_led.off()
|
||||
else:
|
||||
magnetron_led.on()
|
||||
@@ -291,7 +307,8 @@ async def lora_process_task():
|
||||
continue
|
||||
|
||||
if "action" in data:
|
||||
if data["action"] == LoraCommands.TOGGLE_PAUSE:
|
||||
action = data["action"]
|
||||
if action == LoraCommands.TOGGLE_PAUSE:
|
||||
if cooking_state is not None:
|
||||
if cooking_state.state == cookingState.CookingStates.DONE:
|
||||
print("[LoRa Process] Cooking is done. Resetting microwave for next session.")
|
||||
@@ -304,23 +321,29 @@ async def lora_process_task():
|
||||
else:
|
||||
log("[LoRa Process] No active cooking state to toggle pause/resume.")
|
||||
|
||||
elif data["action"] == LoraCommands.TOGGLE_DEFROST:
|
||||
elif action == LoraCommands.TOGGLE_DEFROST:
|
||||
print("[LoRa Process] Toggling defrost mode via orchestrator command.")
|
||||
defrost_mode = data.get("defrost_state", False)
|
||||
update_screen()
|
||||
|
||||
elif data["action"] == LoraCommands.NEW_ALERT:
|
||||
alert = Alert(data.get("alert_type"), data.get("message", "Unsafe area"))
|
||||
alert.timestamp = data.get("timestamp", time.time())
|
||||
print(f"[LoRa Process] New alert received via orchestrator: {alert.to_dict()}")
|
||||
alert_manager.add_alert(alert)
|
||||
elif action == LoraCommands.NEW_ALERT:
|
||||
alert: Alert | None = None
|
||||
if data.get("alert_type") == AlertType.TEMPERATURE_ALERT:
|
||||
alert = Alert(data.get("alert_type"), data.get("alert_message", "Temp too high"), redistributed=True)
|
||||
elif data.get("alert_type") == AlertType.COOKING_SAFETY:
|
||||
alert = Alert(data.get("alert_type"), data.get("alert_message", "Unsafe area"), redistributed=True)
|
||||
|
||||
if alert:
|
||||
alert.timestamp = data.get("timestamp", time.time())
|
||||
alert_manager.add_alert(alert)
|
||||
|
||||
elif data["action"] == LoraCommands.MICROVAVE_STATE_UPDATE:
|
||||
elif action == LoraCommands.MICROVAVE_STATE_UPDATE:
|
||||
new_state = data.get("new_microwave_state")
|
||||
if new_state:
|
||||
print(f"[LoRa Process] Microwave state update received: {new_state}")
|
||||
microwave_state = new_state
|
||||
update_screen()
|
||||
if new_state not in (MicrowaveState.ALERT): # Not alert so we can show the error
|
||||
update_screen()
|
||||
|
||||
await asyncio.sleep_ms(100)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user