feat(api): add backend
routes and WebSockets
This commit is contained in:
41
services/api/app/ws/broadcaster.py
Normal file
41
services/api/app/ws/broadcaster.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from contextlib import suppress
|
||||
from datetime import timezone
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.db.session import SessionLocal
|
||||
from app.repositories.audio_repository import AudioRepository
|
||||
from app.ws.manager import manager
|
||||
|
||||
|
||||
def _iso_z(dt) -> str:
|
||||
# dt ожидается timezone-aware
|
||||
return dt.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
|
||||
|
||||
|
||||
async def audio_live_broadcaster(poll_interval_sec: float = 0.2) -> None:
|
||||
last_time = None
|
||||
|
||||
while True:
|
||||
try:
|
||||
async with SessionLocal() as db: # AsyncSession
|
||||
repo = AudioRepository(db)
|
||||
rows = await repo.latest(1)
|
||||
if rows:
|
||||
row = rows[0]
|
||||
if last_time is None or row.time > last_time:
|
||||
last_time = row.time
|
||||
await manager.broadcast_json(
|
||||
{
|
||||
"time": _iso_z(row.time),
|
||||
"rms_db": float(row.rms_db),
|
||||
"freq_hz": int(row.frequency_hz),
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
# чтобы WS не умирал из-за временных проблем с БД
|
||||
pass
|
||||
|
||||
await asyncio.sleep(poll_interval_sec)
|
||||
Reference in New Issue
Block a user