feat: wire WebSocket router and add background refresh loop
Register ws router in main.py and add periodic ping/odoo refresh that broadcasts updates to connected WebSocket clients. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
@@ -25,13 +26,53 @@ odoo_client = OdooClient(
|
||||
)
|
||||
|
||||
|
||||
async def refresh_loop():
|
||||
from routers.ws import broadcast
|
||||
from modules.network_scanner import NetworkScanner
|
||||
|
||||
refresh = app_config.get_settings().get("refresh", {})
|
||||
odoo_interval = refresh.get("odoo_minutes", 5) * 60
|
||||
network_interval = refresh.get("network_minutes", 10) * 60
|
||||
ping_interval = refresh.get("ping_seconds", 60)
|
||||
|
||||
last_odoo = 0
|
||||
last_network = 0
|
||||
last_ping = 0
|
||||
|
||||
while True:
|
||||
now = asyncio.get_event_loop().time()
|
||||
|
||||
if now - last_ping >= ping_interval:
|
||||
try:
|
||||
scan_config = app_config.get_network_scan_config()
|
||||
scanner = NetworkScanner(scan_config.get("subnet", "192.168.1.0/24"))
|
||||
nodes = app_config.get_nodes()
|
||||
ips = [n["ip"] for n in nodes]
|
||||
statuses = await scanner.ping_all(ips)
|
||||
await broadcast("ping_update", statuses)
|
||||
except Exception:
|
||||
pass
|
||||
last_ping = now
|
||||
|
||||
if now - last_odoo >= odoo_interval:
|
||||
try:
|
||||
await broadcast("odoo_refresh", {"trigger": "scheduled"})
|
||||
except Exception:
|
||||
pass
|
||||
last_odoo = now
|
||||
|
||||
await asyncio.sleep(10)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
try:
|
||||
await odoo_client.authenticate()
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not connect to Odoo: {e}")
|
||||
task = asyncio.create_task(refresh_loop())
|
||||
yield
|
||||
task.cancel()
|
||||
|
||||
|
||||
app = FastAPI(title="TV Dashboard API", lifespan=lifespan)
|
||||
@@ -43,12 +84,13 @@ app.add_middleware(
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
from routers import network, tasks, calendar, services
|
||||
from routers import network, tasks, calendar, services, ws
|
||||
|
||||
app.include_router(network.router)
|
||||
app.include_router(tasks.router)
|
||||
app.include_router(calendar.router)
|
||||
app.include_router(services.router)
|
||||
app.include_router(ws.router)
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
|
||||
Reference in New Issue
Block a user