Compare commits

..

2 Commits

Author SHA1 Message Date
Ninluc c58ae4603e Skip sending implicit new state
Build, push image, and notify Watchtower / build-image (push) Successful in 39s
Build, push image, and notify Watchtower / notify (push) Successful in 11s
2026-08-23 17:07:18 +02:00
Ninluc e7f6374200 Skip food safety alert for webex and sms 2026-08-23 17:06:59 +02:00
3 changed files with 88 additions and 84 deletions
+84 -80
View File
@@ -323,97 +323,101 @@ def alert():
alert_type = alert_info.get("type", "Unknown") alert_type = alert_info.get("type", "Unknown")
alert_message = alert_info.get("message", "No message provided") alert_message = alert_info.get("message", "No message provided")
client_name = f"Unknown Client (Orchestrator {orchestrator_id})" if orchestrator_id else "Unknown Client"
client_email = None
client_phone = None
if orchestrator_id:
client_doc = client_collection.find_one({
"devices": {
"$elemMatch": {
"device_type": "orchestrator",
"device_id": str(orchestrator_id)
}
}
})
if client_doc:
client_name = client_doc.get("client_name", client_name)
contact_info = client_doc.get("contact_info", {})
client_email = contact_info.get("email")
client_phone = contact_info.get("phone")
webex_room_id = None webex_room_id = None
room_title = f"Support - {client_name}"
room_link = None room_link = None
room_title = None
webex_status = "skipped" webex_status = "skipped"
sms_sent = False sms_sent = False
three_minutes_ago = datetime.now(timezone.utc) - timedelta(minutes=3)
recent_alert = alert_collection.find_one({ if alert_type != "COOKING_SAFETY":
"orchestrator_id": orchestrator_id,
"webex_room_id": {"$exists": True, "$ne": None},
"created_at": {"$gte": three_minutes_ago},
"_id": {"$ne": saved_alert.inserted_id}
}, sort=[("created_at", DESCENDING)])
if recent_alert: client_name = f"Unknown Client (Orchestrator {orchestrator_id})" if orchestrator_id else "Unknown Client"
webex_room_id = recent_alert["webex_room_id"] client_email = None
room_title = recent_alert.get("webex_room_title", room_title) client_phone = None
try:
webex_manager.send_alert_info_message( if orchestrator_id:
room_id=webex_room_id, client_doc = client_collection.find_one({
alert_type=alert_type, "devices": {
alert_message=alert_message, "$elemMatch": {
alert_id=doc_id, "device_type": "orchestrator",
added_alert_info=True "device_id": str(orchestrator_id)
) }
webex_status = "reused_room" }
room_link = recent_alert.get("webex_room_link") or f"https://web.webex.com/spaces/{webex_room_id}" })
except Exception as e:
current_app.logger.exception("Failed to send follow-up message to Webex room")
webex_status = f"message_failed: {str(e)}"
else:
try:
room_details = webex_manager.create_support_room(
client_name,
client_email,
alert_type,
alert_message,
doc_id
)
if isinstance(room_details, dict): if client_doc:
webex_room_id = room_details.get("id") client_name = client_doc.get("client_name", client_name)
room_title = room_details.get("title", room_title) contact_info = client_doc.get("contact_info", {})
room_link = room_details.get("meetingLink") or f"https://web.webex.com/spaces/{webex_room_id}" client_email = contact_info.get("email")
else: client_phone = contact_info.get("phone")
webex_room_id = room_details
room_link = f"https://web.webex.com/spaces/{webex_room_id}"
webex_status = "created_new_room" room_title = f"Support - {client_name}"
except Exception as e:
current_app.logger.exception("Failed to create Webex support room")
webex_status = f"creation_failed: {str(e)}"
if webex_room_id and client_phone and not SAVEUP_TWILIO_API_TOKEN: three_minutes_ago = datetime.now(timezone.utc) - timedelta(minutes=3)
sms_sent = send_alert_sms(
to_phone=client_phone, recent_alert = alert_collection.find_one({
room_title=room_title, "orchestrator_id": orchestrator_id,
room_link=room_link, "webex_room_id": {"$exists": True, "$ne": None},
client_email=client_email "created_at": {"$gte": three_minutes_ago},
) "_id": {"$ne": saved_alert.inserted_id}
}, sort=[("created_at", DESCENDING)])
update_fields = {"sms_sent": sms_sent} if recent_alert:
if webex_room_id: webex_room_id = recent_alert["webex_room_id"]
update_fields.update({ room_title = recent_alert.get("webex_room_title", room_title)
"webex_room_id": webex_room_id, try:
"webex_room_title": room_title, webex_manager.send_alert_info_message(
"webex_room_link": room_link room_id=webex_room_id,
}) alert_type=alert_type,
alert_message=alert_message,
alert_id=doc_id,
added_alert_info=True
)
webex_status = "reused_room"
room_link = recent_alert.get("webex_room_link") or f"https://web.webex.com/spaces/{webex_room_id}"
except Exception as e:
current_app.logger.exception("Failed to send follow-up message to Webex room")
webex_status = f"message_failed: {str(e)}"
else:
try:
room_details = webex_manager.create_support_room(
client_name,
client_email,
alert_type,
alert_message,
doc_id
)
if isinstance(room_details, dict):
webex_room_id = room_details.get("id")
room_title = room_details.get("title", room_title)
room_link = room_details.get("meetingLink") or f"https://web.webex.com/spaces/{webex_room_id}"
else:
webex_room_id = room_details
room_link = f"https://web.webex.com/spaces/{webex_room_id}"
alert_collection.update_one({"_id": saved_alert.inserted_id}, {"$set": update_fields}) webex_status = "created_new_room"
except Exception as e:
current_app.logger.exception("Failed to create Webex support room")
webex_status = f"creation_failed: {str(e)}"
if webex_room_id and client_phone and not SAVEUP_TWILIO_API_TOKEN:
sms_sent = send_alert_sms(
to_phone=client_phone,
room_title=room_title,
room_link=room_link,
client_email=client_email
)
update_fields = {"sms_sent": sms_sent}
if webex_room_id:
update_fields.update({
"webex_room_id": webex_room_id,
"webex_room_title": room_title,
"webex_room_link": room_link
})
alert_collection.update_one({"_id": saved_alert.inserted_id}, {"$set": update_fields})
# OData v4 Created Response Payload # OData v4 Created Response Payload
return jsonify({ return jsonify({
+1
View File
@@ -346,6 +346,7 @@ async def lora_process_task():
if alert: if alert:
alert.timestamp = data.get("timestamp", time.time()) alert.timestamp = data.get("timestamp", time.time())
alert_manager.add_alert(alert) alert_manager.add_alert(alert)
microwave_state = MicrowaveState.ALERT
elif action == LoraCommands.MICROVAVE_STATE_UPDATE: elif action == LoraCommands.MICROVAVE_STATE_UPDATE:
new_state = data.get("new_microwave_state") new_state = data.get("new_microwave_state")
+3 -4
View File
@@ -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 if field != MicrowaveStateFields.STATE: # No external screen or lcd uses this field, so we can skip updates for it
notify_display_update() notify_display_update()
if field == MicrowaveStateFields.STATE and state.state in (MicrowaveState.IDLE, 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):
# Send to LoRa device if state changed # Send to LoRa device if state changed
lora.send_reliable(payloads.lora_microwave_state(state.state), max_retries=5) lora.send_reliable(payloads.lora_microwave_state(state.state), max_retries=5)
@@ -472,7 +472,7 @@ async def on_new_alert(alert: Alert):
response.raise_for_status() response.raise_for_status()
res_json = response.json() res_json = response.json()
print(f"[Alert Handling] Alert sent to cloud: {alert_dict}, Response: {res_json}") log(f"[Alert Handling] Alert sent to cloud: {alert_dict}, Response: {res_json}")
if not alert.redistributed: # If not redistributed from microwaves if not alert.redistributed: # If not redistributed from microwaves
# Send the alert to the lora device # Send the alert to the lora device
@@ -480,7 +480,6 @@ async def on_new_alert(alert: Alert):
print(f"[Alert Handling] Sending alert to LoRa device: {payload}") print(f"[Alert Handling] Sending alert to LoRa device: {payload}")
if lora.send_reliable(payload): if lora.send_reliable(payload):
microwave_states["2"].set_cooking_state(CookingStates.ALERT) microwave_states["2"].set_cooking_state(CookingStates.ALERT)
buzzer.buzzer_siren(beatsNb=5)
# --- HARDWARE CONTROLLERS --- # --- HARDWARE CONTROLLERS ---
def _stop_hardware(microwave_id: str): def _stop_hardware(microwave_id: str):
@@ -669,7 +668,7 @@ async def request_cloud_cooking_plan(microwave_id, sensors_data):
microwave_states[microwave_id].set_state(MicrowaveState.DONE) microwave_states[microwave_id].set_state(MicrowaveState.DONE)
if not res_json['is_safe']: # The cooking area is not safe, raise an alert if not res_json['is_safe']: # The cooking area is not safe, raise an alert
alert = Alert(AlertType.COOKING_SAFETY, res_json['warning_message']) alert = Alert(AlertType.COOKING_SAFETY, res_json['warning_message'], redistributed=False)
alert_manager.add_alert(alert) alert_manager.add_alert(alert)
else: else:
alert = Alert(AlertType.ERROR, f"Cloud API error for microwave {microwave_id}: {res_json['error']}") alert = Alert(AlertType.ERROR, f"Cloud API error for microwave {microwave_id}: {res_json['error']}")