diff --git a/backend/main.py b/backend/main.py index 3510c84..f0c6608 100644 --- a/backend/main.py +++ b/backend/main.py @@ -777,3 +777,36 @@ async def power_control(power_data: dict): except Exception as e: print(f"电源控制时发生错误: {e}") raise HTTPException(status_code=500, detail=str(e)) + +# 扫描控制API - 控制Scanner的启动与停止 +@app.post("/api/scanctrl") +async def scan_control(scan_data: dict): + """ + 控制Scanner的启动与停止 + 参数: + - action: 操作,可选值: 'start' (启动) 或 'stop' (停止) + """ + try: + action = scan_data.get("action") + + # 验证操作类型 + if action not in ['start', 'stop']: + raise HTTPException(status_code=400, detail="action 必须是 'start' 或 'stop'") + + try: + path = "/home/admin/.config/cpy/config.json" + with open(path, "r") as f: + config = json.load(f) + sn = config.get('sn', "") + if sn == "": + raise HTTPException(status_code=400, detail="配置文件中缺少 sn 字段") + except FileNotFoundError: + raise HTTPException(status_code=400, detail="配置文件不存在") + + mqtt_client.publish(f"v1/cpy/{sn}/control", json.dumps({"method": "scan_control", "action": action})) + return {"action": action, "status": "success"} + except HTTPException: + raise + except Exception as e: + print(f"扫描控制时发生错误: {e}") + raise HTTPException(status_code=500, detail=str(e)) diff --git a/frontend/src/views/PowerControl.vue b/frontend/src/views/PowerControl.vue index 874661c..2e3744c 100644 --- a/frontend/src/views/PowerControl.vue +++ b/frontend/src/views/PowerControl.vue @@ -1,6 +1,6 @@