Files
sound-analyze/services/api/app/ws/broadcaster.py
Iwwww 1b864228d4 feat(api): add backend
routes and WebSockets
2025-12-26 19:26:06 +03:00

42 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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)