feat: add Microduck UNO Q deployment and hotspot skill control
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from typing import Any
|
||||
from urllib.parse import parse_qs
|
||||
|
||||
from core.controller import MicroduckController
|
||||
from core.deploy_service import DeployService
|
||||
|
||||
from .api import (
|
||||
build_status_payload,
|
||||
dispatch_api_get,
|
||||
dispatch_api_post,
|
||||
handle_ws_message,
|
||||
load_index_html,
|
||||
)
|
||||
|
||||
|
||||
def create_fastapi_app(controller: MicroduckController, deploy_service: DeployService) -> Any:
|
||||
"""Create FastAPI app backed by the shared web.api dispatcher."""
|
||||
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
|
||||
app = FastAPI(title="Microduck UNO Q Controller", version="0.1.0")
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
@app.middleware("http")
|
||||
async def add_pna_header(request: Request, call_next: Any) -> Any:
|
||||
resp = await call_next(request)
|
||||
resp.headers["Access-Control-Allow-Private-Network"] = "true"
|
||||
return resp
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def index() -> HTMLResponse:
|
||||
return HTMLResponse(content=load_index_html().decode("utf-8"))
|
||||
|
||||
@app.get("/api/{subpath:path}")
|
||||
async def api_get(subpath: str) -> JSONResponse:
|
||||
code, payload = dispatch_api_get(f"/api/{subpath}", controller, deploy_service)
|
||||
return JSONResponse(status_code=code, content=payload)
|
||||
|
||||
@app.post("/api/{subpath:path}")
|
||||
async def api_post(subpath: str, request: Request) -> JSONResponse:
|
||||
body = await request.body()
|
||||
content_type = request.headers.get("content-type", "")
|
||||
query = parse_qs(request.url.query)
|
||||
code, payload = dispatch_api_post(
|
||||
f"/api/{subpath}",
|
||||
body,
|
||||
content_type,
|
||||
query,
|
||||
controller,
|
||||
deploy_service,
|
||||
)
|
||||
return JSONResponse(status_code=code, content=payload)
|
||||
|
||||
@app.websocket("/ws/control")
|
||||
async def ws_control(websocket: WebSocket) -> None:
|
||||
await websocket.accept()
|
||||
stop_event = asyncio.Event()
|
||||
|
||||
async def telemetry_loop() -> None:
|
||||
while not stop_event.is_set():
|
||||
msg = {"type": "telemetry", **build_status_payload(controller, deploy_service)}
|
||||
await websocket.send_text(json.dumps(msg))
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
sender = asyncio.create_task(telemetry_loop())
|
||||
try:
|
||||
while True:
|
||||
raw = await websocket.receive_text()
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except Exception:
|
||||
continue
|
||||
ack = handle_ws_message(controller, data)
|
||||
if ack is not None:
|
||||
await websocket.send_text(json.dumps(ack))
|
||||
except WebSocketDisconnect:
|
||||
pass
|
||||
finally:
|
||||
stop_event.set()
|
||||
sender.cancel()
|
||||
|
||||
return app
|
||||
Reference in New Issue
Block a user