BBFbyOpus
This commit is contained in:
@@ -11,7 +11,7 @@ from app.models.event_log import EventLog
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(dependencies=[Depends(verify_token)])
|
||||
router = APIRouter()
|
||||
wiz = WizDriver()
|
||||
|
||||
|
||||
@@ -34,19 +34,21 @@ async def _log_event(
|
||||
logger.error(f"Ошибка записи в лог: {e}")
|
||||
|
||||
|
||||
def _classify_action(params: dict) -> str:
|
||||
"""Определить тип действия по параметрам."""
|
||||
if "state" in params and len(params) == 1:
|
||||
return "toggle_on" if params["state"] else "toggle_off"
|
||||
if "sceneId" in params or "scene" in params:
|
||||
return "scene"
|
||||
if "r" in params or "g" in params or "b" in params:
|
||||
return "color"
|
||||
if "temp" in params:
|
||||
return "temperature"
|
||||
if "dimming" in params:
|
||||
return "brightness"
|
||||
return "control"
|
||||
async def log_toggle(auth: AuthContext, target_type: str, target_id: str, params: dict):
|
||||
"""Логирует toggle_on/toggle_off если в params есть state."""
|
||||
if "state" in params:
|
||||
action = "toggle_on" if params["state"] else "toggle_off"
|
||||
await _log_event(auth, action, target_type, target_id, params)
|
||||
|
||||
|
||||
async def log_toggle_by_name(
|
||||
key_name: str, target_type: str, target_id: str, params: dict
|
||||
):
|
||||
"""Логирует toggle из контекста без AuthContext (для планировщика)."""
|
||||
if "state" in params:
|
||||
auth = AuthContext(is_master=False, is_admin=False, key_name=key_name)
|
||||
action = "toggle_on" if params["state"] else "toggle_off"
|
||||
await _log_event(auth, action, target_type, target_id, params)
|
||||
|
||||
|
||||
@router.post("/device/{device_id}")
|
||||
@@ -83,8 +85,7 @@ async def control_device(
|
||||
|
||||
result = await wiz.set_pilot(device.ip, params)
|
||||
|
||||
# Логируем
|
||||
await _log_event(auth, _classify_action(params), "device", device_id, params)
|
||||
await log_toggle(auth, "device", device_id, params)
|
||||
|
||||
return {"device_id": device_id, "applied": params, "result": result}
|
||||
|
||||
@@ -121,14 +122,13 @@ async def control_group(
|
||||
tasks = [wiz.set_pilot(ip, params) for ip in ips]
|
||||
await asyncio.gather(*tasks, return_exceptions=True)
|
||||
|
||||
# Логируем
|
||||
await _log_event(auth, _classify_action(params), "group", group_id, params)
|
||||
await log_toggle(auth, "group", group_id, params)
|
||||
|
||||
return {"status": "ok", "applied": params, "sent_to": ips}
|
||||
|
||||
|
||||
@router.post("/device/{device_id}/blink")
|
||||
async def blink_device(device_id: str):
|
||||
async def blink_device(device_id: str, _auth: AuthContext = Depends(verify_token)):
|
||||
device = state_manager.devices.get(device_id)
|
||||
if not device:
|
||||
raise HTTPException(status_code=404, detail="Лампа оффлайн")
|
||||
@@ -146,7 +146,7 @@ async def blink_device(device_id: str):
|
||||
|
||||
|
||||
@router.get("/device/{device_id}/status")
|
||||
async def get_device_status(device_id: str):
|
||||
async def get_device_status(device_id: str, _auth: AuthContext = Depends(verify_token)):
|
||||
"""Опрос реального состояния конкретной лампы."""
|
||||
device = state_manager.devices.get(device_id)
|
||||
if not device:
|
||||
@@ -160,7 +160,7 @@ async def get_device_status(device_id: str):
|
||||
|
||||
|
||||
@router.get("/group/{group_id}/status")
|
||||
async def get_group_status(group_id: str):
|
||||
async def get_group_status(group_id: str, _auth: AuthContext = Depends(verify_token)):
|
||||
"""Опрос состояния всей группы (возвращает список статусов)."""
|
||||
ips = state_manager.get_group_ips(group_id)
|
||||
if not ips:
|
||||
|
||||
Reference in New Issue
Block a user