Webex first draft
This commit is contained in:
+79
-7
@@ -5,10 +5,14 @@ import uuid
|
||||
import datetime
|
||||
import sys
|
||||
import json
|
||||
from flask import Flask, request, jsonify, current_app
|
||||
import time
|
||||
import urllib.parse
|
||||
import requests
|
||||
from flask import Flask, request, jsonify, current_app, redirect, url_for
|
||||
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
|
||||
|
||||
@@ -29,9 +33,18 @@ MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017/")
|
||||
client = MongoClient(MONGO_URI)
|
||||
db = client["microwave_network_db"]
|
||||
|
||||
client_collection = db["client_data"]
|
||||
cooking_collection = db["cooking_parameters"]
|
||||
telemetry_collection = db["telemetry_data"]
|
||||
alert_collection = db["alert_data"]
|
||||
webex_tokens_collection = db["webex_tokens"] # Collection for Webex OAuth tokens
|
||||
|
||||
# Webex Credentials & Configuration from Environment Variables
|
||||
WEBEX_CLIENT_ID = os.getenv("WEBEX_CLIENT_ID", "YOUR_WEBEX_CLIENT_ID")
|
||||
WEBEX_CLIENT_SECRET = os.getenv("WEBEX_CLIENT_SECRET", "YOUR_WEBEX_CLIENT_SECRET")
|
||||
WEBEX_REDIRECT_URI = os.getenv("WEBEX_REDIRECT_URI", "https://smartwave.matthiasg.dev/oauth/callback")
|
||||
WEBEX_TEAM_ID = os.getenv("WEBEX_TEAM_ID", "YOUR_WEBEX_TEAM_ID")
|
||||
WEBEX_NINLUC_ID = os.getenv("WEBEX_NINLUC_ID", "YOUR_WEBEX_NINLUC_ID")
|
||||
|
||||
# Ensure the camera image storage directory exists when the app starts
|
||||
CAMERA_IMAGE_DIR = "storage/dishPhotos"
|
||||
@@ -41,9 +54,49 @@ os.makedirs(CAMERA_IMAGE_DIR, exist_ok=True)
|
||||
# Classes
|
||||
# ---------------------------------------------------------
|
||||
microwave_cook_planner = MicrowaveCookPlanner()
|
||||
# Instantiate Webex Manager
|
||||
webex_manager = WebexManager(
|
||||
db_collection=webex_tokens_collection,
|
||||
client_id=WEBEX_CLIENT_ID,
|
||||
client_secret=WEBEX_CLIENT_SECRET,
|
||||
redirect_uri=WEBEX_REDIRECT_URI,
|
||||
team_id=WEBEX_TEAM_ID,
|
||||
user_id=WEBEX_NINLUC_ID
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Routes
|
||||
# OAuth Routes
|
||||
# ---------------------------------------------------------
|
||||
|
||||
@app.route("/oauth/login")
|
||||
def oauth_login():
|
||||
"""Redirects developer/admin to Webex for initial OAuth authorization."""
|
||||
scopes = "spark:rooms_write spark:rooms_read spark:memberships_write spark:memberships_read spark:messages_write"
|
||||
params = {
|
||||
"client_id": WEBEX_CLIENT_ID,
|
||||
"response_type": "code",
|
||||
"redirect_uri": WEBEX_REDIRECT_URI,
|
||||
"scope": scopes
|
||||
}
|
||||
url = f"https://webexapis.com/v1/authorize?{urllib.parse.urlencode(params)}"
|
||||
return redirect(url)
|
||||
|
||||
@app.route("/oauth/callback")
|
||||
def oauth_callback():
|
||||
"""OAuth redirect endpoint that receives the authorization code."""
|
||||
code = request.args.get("code")
|
||||
if not code:
|
||||
return jsonify({"error": "Missing code parameter"}), 400
|
||||
|
||||
try:
|
||||
webex_manager.exchange_code(code)
|
||||
return jsonify({"status": "success", "message": "Webex tokens stored successfully in MongoDB!"}), 200
|
||||
except Exception as e:
|
||||
current_app.logger.exception("Failed to exchange OAuth code")
|
||||
return jsonify({"error": f"OAuth exchange failed: {str(e)}"}), 500
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Application Routes
|
||||
# ---------------------------------------------------------
|
||||
|
||||
@app.route("/")
|
||||
@@ -133,15 +186,34 @@ async def cooking_params():
|
||||
except Exception as e:
|
||||
current_app.logger.exception("Exception in /cooking-params")
|
||||
|
||||
|
||||
@app.route("/alert", methods=["POST"])
|
||||
def alert():
|
||||
# Save the alert in alert_collection
|
||||
data = request.get_json()
|
||||
data = request.get_json() or {}
|
||||
alert_collection.insert_one(data)
|
||||
|
||||
# TODO : Créer une room webex et envoyer un sms
|
||||
|
||||
return jsonify({"status": "success", "message": "Alert saved"}), 200
|
||||
# Extract client metadata from alert payload (or use fallback values)
|
||||
client_name = data.get("client_name", "Unknown Client")
|
||||
client_email = data.get("client_email")
|
||||
|
||||
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)}"
|
||||
|
||||
return jsonify({
|
||||
"status": "success",
|
||||
"message": "Alert saved",
|
||||
"webex_room_id": room_id,
|
||||
"webex_status": webex_status
|
||||
}), 200
|
||||
|
||||
|
||||
@app.route("/telemetry", methods=["POST"])
|
||||
def telemetry():
|
||||
|
||||
Reference in New Issue
Block a user