Initial commit
This commit is contained in:
38
app/core/state.py
Normal file
38
app/core/state.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import logging
|
||||
from typing import Dict, List, Optional
|
||||
from app.models.device import Device, Group
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class StateManager:
|
||||
def __init__(self):
|
||||
# Храним устройства по MAC (уникальный ID)
|
||||
self.devices: Dict[str, Device] = {}
|
||||
# Группы (люстры) по их ID
|
||||
self.groups: Dict[str, Group] = {}
|
||||
|
||||
def update_device(self, device_data: dict):
|
||||
"""Обновляет или добавляет устройство в состояние."""
|
||||
mac = device_data["mac"]
|
||||
device = Device(
|
||||
id=mac,
|
||||
ip=device_data["ip"],
|
||||
name=f"WiZ {mac[-4:]}", # Временное имя
|
||||
room="Default",
|
||||
)
|
||||
self.devices[mac] = device
|
||||
|
||||
def get_group_ips(self, group_id: str) -> List[str]:
|
||||
"""Возвращает список IP всех ламп, входящих в группу."""
|
||||
group = self.groups.get(group_id)
|
||||
if not group:
|
||||
return []
|
||||
|
||||
return [
|
||||
self.devices[d_id].ip for d_id in group.device_ids if d_id in self.devices
|
||||
]
|
||||
|
||||
|
||||
# Создаем синглтон для использования во всем приложении
|
||||
state_manager = StateManager()
|
||||
Reference in New Issue
Block a user