37232ae1b9
cuda-grid-controller (Python 3.11+) — control plane между HA/MQTT/HTTP
и FFmpeg's vf_cuda_grid filter через ZMQ.
Modules (~700 LOC Python):
- config.py — Pydantic schema (broker, instances[], ha_discovery, http, log) + YAML loader
- layouts.py — registry известных layouts (sync с vf_cuda_grid.c Phase 2)
- ha_discovery.py — HA MQTT Discovery payloads (select.layout, sensor.current_layout,
binary_sensor.online per instance + global device entry)
- zmq_client.py — async ZMQ REQ socket к FFmpeg zmq filter
(target command args → reply parsing)
- state.py — in-memory ControllerState (active_layout per instance, asyncio.Lock)
- mqtt_loop.py — aiomqtt async loop: subscribe cuda_grid/cmd/<inst>/+/+,
publish cuda_grid/state/* (retained) + cuda_grid/event/*, LWT, HA status reconnect
- dispatch.py — CommandDispatcher: layout.set action → ZMQ send_command + state update + events
- http_api.py — FastAPI: /health, /layouts, /state, POST /layout/{inst}/set
- __main__.py — typer CLI, asyncio.gather(mqtt_loop, uvicorn.server)
Examples + Dockerfile:
- examples/controller.yaml — 2 instances (livingroom_tv, public_stream)
- Dockerfile — python:3.11-slim, ENTRYPOINT cuda-grid-controller
- README — overview, usage, FFmpeg side filter graph
End-to-end flow ready:
HA dashboard → MQTT → controller → ZMQ → FFmpeg process_command → layout switch
↓
state публикуется обратно в MQTT → HA UI обновляется
Phase 3 deliverable per gx/vf-cuda-grid#1. Phase 4 = overlays (rect/text/icon).
63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
"""HTTP REST API (FastAPI)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import structlog
|
|
from fastapi import FastAPI, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
from .config import Config
|
|
from .dispatch import CommandDispatcher
|
|
from .layouts import PREDEFINED_LAYOUTS
|
|
from .state import ControllerState
|
|
|
|
log = structlog.get_logger()
|
|
|
|
|
|
class LayoutSetReq(BaseModel):
|
|
layout: str
|
|
|
|
|
|
def create_app(
|
|
cfg: Config, state: ControllerState, dispatcher: CommandDispatcher
|
|
) -> FastAPI:
|
|
app = FastAPI(
|
|
title="cuda-grid-controller",
|
|
version="0.1.0",
|
|
description="Control plane для vf_cuda_grid FFmpeg filter",
|
|
)
|
|
|
|
@app.get("/health")
|
|
async def health() -> dict[str, Any]:
|
|
return {"status": "ok"}
|
|
|
|
@app.get("/layouts")
|
|
async def layouts() -> dict[str, Any]:
|
|
return {"predefined": PREDEFINED_LAYOUTS}
|
|
|
|
@app.get("/state")
|
|
async def get_state() -> dict[str, Any]:
|
|
out = {}
|
|
for inst in cfg.instances:
|
|
out[inst.name] = {
|
|
"active_layout": await state.get_layout(inst.name),
|
|
"zmq_endpoint": inst.zmq_endpoint,
|
|
}
|
|
return {"instances": out}
|
|
|
|
@app.post("/layout/{instance}/set")
|
|
async def set_layout(instance: str, req: LayoutSetReq) -> dict[str, Any]:
|
|
inst = next((i for i in cfg.instances if i.name == instance), None)
|
|
if inst is None:
|
|
raise HTTPException(404, f"unknown instance '{instance}'")
|
|
if req.layout not in PREDEFINED_LAYOUTS:
|
|
raise HTTPException(
|
|
400, f"unknown layout '{req.layout}'. Доступны: {PREDEFINED_LAYOUTS}"
|
|
)
|
|
await dispatcher.handle(instance, "layout.set", req.layout)
|
|
return {"ok": True, "instance": instance, "layout": req.layout}
|
|
|
|
return app
|