From 82dc74877d0757ad4562ead7bc68e45f65a8ff14 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 16 十月 2024 16:29:09 +0800 Subject: [PATCH] 增加excel merge websocket demo --- app/api/excel.py | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 42 insertions(+), 6 deletions(-) diff --git a/app/api/excel.py b/app/api/excel.py index 78a5e03..1ff4741 100644 --- a/app/api/excel.py +++ b/app/api/excel.py @@ -1,13 +1,17 @@ -from fastapi import APIRouter, File, UploadFile +from fastapi import APIRouter, File, UploadFile, Depends from fastapi.responses import JSONResponse, FileResponse from fastapi.exceptions import HTTPException +from sqlalchemy.orm import Session +from starlette.websockets import WebSocket, WebSocketDisconnect from werkzeug.utils import secure_filename + +from app.api import get_current_user_websocket +from app.models.agent_model import AgentModel, AgentType +from app.models.base_model import get_db +from app.models.user_model import UserModel from app.utils.excelmerge.conformity import run_conformity -from pathlib import Path -import subprocess import shutil import os - router = APIRouter() @@ -79,7 +83,8 @@ # 杩愯鏂规硶 output_path = run_conformity() output_path_value = output_path - return JSONResponse(content={"message": "conformity.py 杩愯鎴愬姛", "output_path": str(output_path)}, status_code=200) + return JSONResponse(content={"message": "conformity.py 杩愯鎴愬姛", "output_path": str(output_path)}, + status_code=200) except Exception as e: return JSONResponse(content={"error": str(e)}, status_code=500) @@ -102,4 +107,35 @@ except FileNotFoundError: raise HTTPException(status_code=404, detail="鏂囦欢涓嶅瓨鍦�") except Exception as e: - raise HTTPException(status_code=500, detail="鏈嶅姟鍣ㄩ敊璇�") \ No newline at end of file + raise HTTPException(status_code=500, detail="鏈嶅姟鍣ㄩ敊璇�") + + +@router.websocket("/ws/{agent_id}/{chat_id}") +async def excel_chat(websocket: WebSocket, + agent_id: str, + chat_id: str, + db: Session = Depends(get_db)): + agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first() + if not agent: + ret = {"message": "Agent not found", "type": "close"} + return websocket.send_json(ret) + agent_type = agent.agent_type + if chat_id == "" or chat_id == "0": + ret = {"message": "Chat ID not found", "type": "close"} + return websocket.send_json(ret) + + if agent_type != AgentType.BASIC: + ret = {"message": "agent type error", "type": "close"} + return websocket.send_json(ret) + + await websocket.accept() + try: + while True: + message = await websocket.receive_json() + print(message) # 鎵撳嵃鎺ユ敹鍒扮殑娑堟伅 + result = {"message": "宸茬敓鎴愭枃浠�", "type": "file", "url": "ip/download?id=xxxx"} + # 鍙戦�佸搷搴� + await websocket.send_json(result) + except WebSocketDisconnect as e: + + print(f"Client {chat_id} disconnected") -- Gitblit v1.8.0