a1090a5f4c
Phase 4a deliverable (no filter rendering yet — это Phase 4b).
End-to-end pipeline: HA/HTTP/MQTT → controller → ZMQ → FFmpeg (logged).
Modules:
- overlays.py — 7 discriminated union types через pydantic:
rect, text, icon, image, dim, graph, chat. Normalized coords (0.0-1.0),
optional cell binding, z_order, opacity, visible.
- state.py — overlay storage per instance (CRUD: add/remove/update/get/clear)
- dispatch.py — overlay.add/remove/clear actions:
- parses JSON payload в Overlay через TypeAdapter
- serializes to ZMQ string: "<id> <type> <full-json>"
- sends via FFmpeg process_command (filter will парсить в Phase 4b)
- updates state + publishes events (overlay_added, overlay_removed, overlays_cleared)
- http_api.py — REST endpoints:
- POST /overlay/{inst}/add (body = Overlay JSON, returns id)
- GET /overlay/{inst} — list all
- DELETE /overlay/{inst}/{id} — single
- DELETE /overlay/{inst} — clear all
- PATCH /overlay/{inst}/{id} — update
- mqtt_loop.py — already subscribes cuda_grid/cmd/<inst>/+/+; teper handles
overlay/add (JSON payload), overlay/remove (id), overlay/clear
- frigate_bridge.py — FrigateBridge skeleton:
- subscribe frigate/+/motion + frigate/events
- mapping camera_name → target_instance + cell index
- Phase 4a: log received events (rendering в Phase 4b)
- config.py — frigate: optional section
- examples/controller.yaml — frigate mappings для 4 наших камер
State management:
- ControllerState.add/remove/update/get/clear_overlay (asyncio.Lock guarded)
- InstanceState.overlays: dict[str, Overlay]
- IDs generated via uuid4()[:8]
Phase 4a limitations:
- Filter side ничего не рендерит (just logs ZMQ commands)
- Frigate bridge принимает events но не auto-generates overlays
- HA Discovery не имеет overlay-specific entities (overlays через REST API)
Phase 4b: filter-side AVFrame side data + CUDA kernels (rect first, NPP-based,
потом text via freetype atlas, потом icon sprite blit).
119 lines
3.0 KiB
Python
119 lines
3.0 KiB
Python
"""Entry point: `cuda-grid-controller --config controller.yaml`."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import structlog
|
|
import typer
|
|
import uvicorn
|
|
|
|
from .config import Config
|
|
from .dispatch import CommandDispatcher
|
|
from .frigate_bridge import FrigateBridge, FrigateBridgeCfg
|
|
from .http_api import create_app
|
|
from .mqtt_loop import MqttLoop
|
|
from .state import ControllerState
|
|
|
|
cli = typer.Typer(add_completion=False)
|
|
|
|
|
|
def _configure_logging(level: str) -> None:
|
|
logging.basicConfig(
|
|
format="%(message)s",
|
|
level=getattr(logging, level.upper(), logging.INFO),
|
|
)
|
|
structlog.configure(
|
|
processors=[
|
|
structlog.processors.add_log_level,
|
|
structlog.processors.TimeStamper(fmt="iso"),
|
|
structlog.dev.ConsoleRenderer(),
|
|
]
|
|
)
|
|
|
|
|
|
async def _run(cfg: Config) -> None:
|
|
state = ControllerState()
|
|
# Init active_layout = default_layout per instance
|
|
for inst in cfg.instances:
|
|
await state.set_layout(inst.name, inst.default_layout)
|
|
|
|
dispatcher = CommandDispatcher(cfg, state)
|
|
|
|
# Frigate bridge (опционально)
|
|
frigate_bridge: FrigateBridge | None = None
|
|
if cfg.frigate:
|
|
try:
|
|
fcfg = FrigateBridgeCfg.model_validate(cfg.frigate)
|
|
if fcfg.enabled:
|
|
frigate_bridge = FrigateBridge(fcfg)
|
|
except Exception as e:
|
|
structlog.get_logger().warning(
|
|
"frigate_bridge.config_invalid", error=str(e)
|
|
)
|
|
|
|
mqtt = MqttLoop(cfg, state, dispatcher.handle, frigate_bridge=frigate_bridge)
|
|
|
|
# Wire dispatcher events → MQTT publishes
|
|
dispatcher.on_state_change = mqtt.publish_state
|
|
dispatcher.on_event = mqtt.publish_event
|
|
|
|
# HTTP REST
|
|
app = create_app(cfg, state, dispatcher)
|
|
server = uvicorn.Server(
|
|
uvicorn.Config(
|
|
app,
|
|
host=cfg.http.host,
|
|
port=cfg.http.port,
|
|
log_level=cfg.log.level.lower(),
|
|
)
|
|
)
|
|
|
|
log = structlog.get_logger()
|
|
log.info(
|
|
"controller.starting",
|
|
instances=[i.name for i in cfg.instances],
|
|
mqtt=f"{cfg.broker.host}:{cfg.broker.port}",
|
|
http=f"{cfg.http.host}:{cfg.http.port}",
|
|
)
|
|
|
|
try:
|
|
await asyncio.gather(
|
|
mqtt.run(),
|
|
server.serve(),
|
|
)
|
|
except asyncio.CancelledError:
|
|
log.info("controller.shutdown")
|
|
finally:
|
|
await dispatcher.close()
|
|
await mqtt.stop()
|
|
|
|
|
|
@cli.command()
|
|
def run(
|
|
config: Path = typer.Option(
|
|
Path("controller.yaml"),
|
|
"--config",
|
|
"-c",
|
|
help="YAML config path",
|
|
),
|
|
) -> None:
|
|
"""Запустить controller."""
|
|
if not config.exists():
|
|
typer.echo(f"config not found: {config}", err=True)
|
|
raise typer.Exit(1)
|
|
cfg = Config.from_yaml(config)
|
|
_configure_logging(cfg.log.level)
|
|
asyncio.run(_run(cfg))
|
|
|
|
|
|
def main() -> None:
|
|
cli()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|