Temperature Alert
This commit is contained in:
+31
-7
@@ -85,7 +85,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):
|
||||
if field == MicrowaveStateFields.STATE and state.state in (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)
|
||||
|
||||
@@ -266,7 +266,7 @@ async def display_broadcast_worker_task():
|
||||
def button_callback():
|
||||
"""Button physical interrupt callback."""
|
||||
global button_state
|
||||
if microwave_states.get("2").cooking_state == CookingStates.COOKING or microwave_states.get("2").cooking_state == CookingStates.DONE or microwave_states.get("2").cooking_state == CookingStates.STIRRING_REQUIRED:
|
||||
if microwave_states.get("2").cooking_state in (CookingStates.COOKING, CookingStates.DONE, CookingStates.STIRRING_REQUIRED, CookingStates.ALERT):
|
||||
print("[Button] Toggling pause/resume for microwave '2'.")
|
||||
lora.send_reliable({"id": DEVICE_ID, "microwave_id": "2", "action": LoraCommands.TOGGLE_PAUSE}, max_retries=6)
|
||||
else:
|
||||
@@ -280,7 +280,7 @@ button.start_button_monitoring_thread()
|
||||
# --- Alert manager ---
|
||||
alert_manager = None
|
||||
|
||||
def on_new_alert(alert):
|
||||
def on_new_alert(alert: Alert):
|
||||
# Sends to cloud server
|
||||
alert_dict = alert.to_dict()
|
||||
alert_dict["orchestrator_id"] = DEVICE_ID
|
||||
@@ -294,10 +294,12 @@ def on_new_alert(alert):
|
||||
res_json = response.json()
|
||||
print(f"[Alert Handling] Alert sent to cloud: {alert_dict}, Response: {res_json}")
|
||||
|
||||
# Send the alert to the lora device
|
||||
lora.send_reliable(payloads.lora_new_alert(alert))
|
||||
|
||||
microwave_states["2"].set_cooking_state(CookingStates.ALERT)
|
||||
if not alert.redistributed: # If not redistributed from microwaves
|
||||
# Send the alert to the lora device
|
||||
payload = payloads.lora_new_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)
|
||||
|
||||
# --- HARDWARE CONTROLLERS ---
|
||||
def _stop_hardware(microwave_id: str):
|
||||
@@ -320,6 +322,14 @@ def read_local_sensors(microwave_id, initial_dish_height):
|
||||
if temp is not None:
|
||||
sensor_data["temperature"] = temp
|
||||
sensor_data["humidity"] = hum
|
||||
if config.DEBUG_TEMPERATURE_ALERT or temp > config.MAX_ALLOWED_TEMPERATURE_TECHNICAL_COMPARTMENT:
|
||||
print(f"[{microwave_id}] ALERT: Unsafe temperature in technical compartment detected! Temp: {temp}°C, Humidity: {hum}%")
|
||||
alert = Alert(
|
||||
AlertType.TEMPERATURE_ALERT,
|
||||
f"Unsafe temperature in technical compartment detected! Temp: {temp}/{config.MAX_ALLOWED_TEMPERATURE_COOKING_COMPARTMENT}°C, Humidity: {hum}%"
|
||||
)
|
||||
lora.send_reliable(payloads.lora_new_alert(alert), max_retries=5)
|
||||
return None # Abort further processing if unsafe temperature detected
|
||||
except Exception as e:
|
||||
log(f"[{microwave_id}] DHT read warning: {e}")
|
||||
|
||||
@@ -354,6 +364,11 @@ async def handle_new_dish(microwave_id, detected_height):
|
||||
|
||||
# 4. Wait for local sensors to finish reading
|
||||
sensors_data = await sensor_task
|
||||
|
||||
if sensors_data is None:
|
||||
print(f"[{microwave_id}] Aborting cooking plan because of empty sensor data")
|
||||
microwave_states[microwave_id].set_state(MicrowaveState.ALERT)
|
||||
return
|
||||
|
||||
# Check if dish was removed while reading sensors
|
||||
if microwave_states.get(microwave_id).state != MicrowaveState.ANALYZING:
|
||||
@@ -510,6 +525,9 @@ async def process_messages_task():
|
||||
microwave_states[mw_id].set_state(MicrowaveState.DONE)
|
||||
print(f"[{mw_id}] Cooking finished. Waiting for user to remove dish.")
|
||||
if "action" in data.get("data", {}):
|
||||
if type(data["data"]) != dict:
|
||||
print(f"[LoRa] Invalid data format received: {data['data']}")
|
||||
continue
|
||||
action = data["data"]["action"]
|
||||
if action == LoraCommands.COOKING_UPDATE:
|
||||
mw_id = data["data"].get("id")
|
||||
@@ -518,6 +536,12 @@ async def process_messages_task():
|
||||
microwave_states[mw_id].set_cooking_state(state_info.get("cooking_state", microwave_states[mw_id].cooking_state))
|
||||
microwave_states[mw_id].set_cooking_estimated_remaining_time(state_info.get("estimated_remaining_time", microwave_states[mw_id].cooking_estimated_remaining_time))
|
||||
microwave_states[mw_id].set_paused(state_info.get("paused", microwave_states[mw_id].paused))
|
||||
elif action == LoraCommands.NEW_ALERT:
|
||||
alert_type = data["data"].get("alert_type")
|
||||
alert_message = data["data"].get("alert_message")
|
||||
alert = Alert(alert_type, alert_message, redistributed=True)
|
||||
alert_manager.add_alert(alert)
|
||||
print(f"[LoRa] New alert received: {alert.to_dict()}")
|
||||
else:
|
||||
print(f"[LoRa] Unhandled action received: {action} with data: {data['data']}")
|
||||
elif source == "MQTT":
|
||||
|
||||
Reference in New Issue
Block a user