From a8b7016504e06561aad62f0759d63324290a6c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9A=D0=BE=D0=BA=D0=BE?= =?UTF-8?q?=D1=81?= Date: Sat, 21 Feb 2026 13:48:59 +0700 Subject: [PATCH] New endpoint: device & groups status --- app/api/routes/control.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/api/routes/control.py b/app/api/routes/control.py index d5a4164..94bc96f 100644 --- a/app/api/routes/control.py +++ b/app/api/routes/control.py @@ -89,3 +89,39 @@ async def blink_device(device_id: str): await wiz.set_pilot(device.ip, {"state": False}) return {"status": "blink_sent"} + + +@router.get("/device/{device_id}/status") +async def get_device_status(device_id: str): + """Опрос реального состояния конкретной лампы.""" + device = state_manager.devices.get(device_id) + if not device: + raise HTTPException(status_code=404, detail="Лампа оффлайн или не найдена") + + try: + status = await wiz.get_pilot(device.ip) + return {"device_id": device_id, "status": status.get("result", {})} + except Exception as e: + raise HTTPException(status_code=500, detail=f"Ошибка опроса лампы: {e}") + + +@router.get("/group/{group_id}/status") +async def get_group_status(group_id: str): + """Опрос состояния всей группы (возвращает список статусов).""" + ips = state_manager.get_group_ips(group_id) + if not ips: + raise HTTPException(status_code=404, detail="Группа не найдена или оффлайн") + + # Опрашиваем все лампы группы параллельно + tasks = [wiz.get_pilot(ip) for ip in ips] + results = await asyncio.gather(*tasks, return_exceptions=True) + + # Формируем красивый ответ + status_report = [] + for ip, res in zip(ips, results): + if isinstance(res, Exception): + status_report.append({"ip": ip, "error": str(res)}) + else: + status_report.append({"ip": ip, "status": res.get("result", {})}) + + return {"group_id": group_id, "results": status_report}