better Webex
Build, push image, and notify Watchtower / build-image (push) Successful in 42s
Build, push image, and notify Watchtower / notify (push) Successful in 12s

This commit is contained in:
2026-08-17 14:46:35 +02:00
parent 0149c1e5b2
commit 44b4c8034f
3 changed files with 123 additions and 25 deletions
+90 -19
View File
@@ -2,19 +2,17 @@ import os
import asyncio
import base64
import uuid
import datetime
from datetime import datetime, timedelta, timezone
import sys
import json
import time
import urllib.parse
import requests
from flask import Flask, request, jsonify, current_app, redirect, url_for
from flask import Flask, request, jsonify, current_app
from pymongo import MongoClient
from APIs import generate, EdamamAPI
from APIs.mqtt import send_command
from APIs.webex import WebexManager
from microwaveCookPlanner import MicrowaveCookPlanner
import safety_checker
import pymongo
sys.path.insert(0, '..')
try:
@@ -177,27 +175,100 @@ async def cooking_params():
@app.route("/alert", methods=["POST"])
def alert():
data = request.get_json() or {}
alert_collection.insert_one(data)
# Extract client metadata from alert payload (or use fallback values)
client_name = data.get("client_name", "Unknown Client")
client_email = data.get("client_email")
# 1. Inject a UTC timestamp so we can easily query the 3-minute window
data["created_at"] = datetime.now(timezone.utc)
saved_alert = alert_collection.insert_one(data)
# Extract orchestrator_id from the incoming alert
orchestrator_id = data.get("orchestrator_id")
# Safely extract alert info (prevents errors if "alert" is missing)
alert_info = data.get("alert", {})
alert_type = alert_info.get("type", "Unknown")
alert_message = alert_info.get("message", "No message provided")
# --- GET CLIENT METADATA ---
client_name = f"Unknown Client (Orchestrator {orchestrator_id})" if orchestrator_id else "Unknown Client"
client_email = None
room_id = 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)
client_email = client_doc.get("contact_info", {}).get("email")
# --- WEBEX ROOM LOGIC ---
webex_room_id = None
webex_status = "skipped"
# Automatically create Webex room if email or client name is supplied
try:
room_id = webex_manager.create_support_room(client_name, client_email)
webex_status = "created"
except Exception as e:
current_app.logger.exception("Failed to create Webex support room")
webex_status = f"failed: {str(e)}"
# 2. Check if a room was created for this orchestrator in the last 3 minutes
three_minutes_ago = datetime.now(timezone.utc) - timedelta(minutes=3)
recent_alert = alert_collection.find_one({
"orchestrator_id": orchestrator_id,
"webex_room_id": {"$exists": True, "$ne": None},
"created_at": {"$gte": three_minutes_ago},
"_id": {"$ne": saved_alert.inserted_id} # Exclude the alert we just inserted
}, sort=[("created_at", pymongo.DESCENDING)])
if recent_alert:
# A room was created recently -> Reuse it and just send a message
webex_room_id = recent_alert["webex_room_id"]
try:
webex_manager.send_alert_info_message(
room_id=webex_room_id,
alert_type=alert_type,
alert_message=alert_message,
alert_id=str(saved_alert.inserted_id)
)
webex_status = "reused_room"
except Exception as e:
current_app.logger.exception("Failed to send follow-up message to existing Webex room")
webex_status = f"message_failed: {str(e)}"
else:
# No recent room -> Create a new one
try:
# Assuming your modified create_support_room returns a dict with {"id": ...}
room_details = webex_manager.create_support_room(
client_name,
client_email,
alert_type,
alert_message,
str(saved_alert.inserted_id) # Cast ObjectId to string for JSON serialization
)
# Extract ID based on whether your function returns a dict or just the ID string
webex_room_id = room_details["id"] if isinstance(room_details, dict) else room_details
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)}"
# 3. Update the alert document with the Webex Room ID (whether new or reused)
if webex_room_id:
alert_collection.update_one(
{"_id": saved_alert.inserted_id},
{"$set": {"webex_room_id": webex_room_id}}
)
# --- SEND SMS WITH DETAILS ---
# TODO: Add SMS logic here
return jsonify({
"status": "success",
"message": "Alert saved",
"webex_room_id": room_id,
"webex_room_id": webex_room_id,
"webex_status": webex_status
}), 200
@@ -220,7 +291,7 @@ def telemetry():
return jsonify({"error": "Expected a JSON object/dictionary"}), 400
# Stamp UTC timestamp for Node-RED queries
data["received_at"] = datetime.datetime.now(datetime.timezone.utc).isoformat()
data["received_at"] = datetime.now(datetime.timezone.utc).isoformat()
try:
telemetry_collection.insert_one(data)