New endpoint: device & groups status
This commit is contained in:
@@ -89,3 +89,39 @@ async def blink_device(device_id: str):
|
|||||||
await wiz.set_pilot(device.ip, {"state": False})
|
await wiz.set_pilot(device.ip, {"state": False})
|
||||||
|
|
||||||
return {"status": "blink_sent"}
|
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}
|
||||||
|
|||||||
Reference in New Issue
Block a user