增加通过前端页面控制扫描开始和停止
This commit is contained in:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user