alert finish

This commit is contained in:
邓智航
2026-02-28 18:11:03 +08:00
parent 26ff511ddc
commit fd4d180eb1
4 changed files with 217 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
import threading
import time
import asyncio
from minio import Minio
import socketio
import numpy as np
import cv2
@@ -18,11 +19,20 @@ from aiortc import (
from aiortc.mediastreams import VIDEO_CLOCK_RATE, VIDEO_TIME_BASE
from dotenv import load_dotenv
from alert import Alert
load_dotenv()
WEBRTC_SERVER_URL = os.getenv("WEBRTC_SERVER_URL", "ws://localhost:5000")
WEBRTC_DATA_HISTORY_MAXLEN = int(os.getenv("WEBRTC_DATA_HISTORY_MAXLEN", 200))
minio_client = Minio(
endpoint=os.getenv("MINIO_ENDPOINT"),
access_key=os.getenv("MINIO_ACCESS_KEY"),
secret_key=os.getenv("MINIO_SECRET_KEY"),
secure=False,
)
class RingBuffer:
def __init__(self, maxlen):
@@ -84,6 +94,7 @@ class WebRTCServer:
self.fps = fps
self.frameContainer = [None]
self.hub = MonitoringHub()
self.sio = None
self.background_loop = asyncio.new_event_loop()
@@ -104,6 +115,7 @@ class WebRTCServer:
async def _websocket_start(self):
await self.hub.start()
sio = socketio.AsyncClient()
self.sio = sio
@sio.event
async def connect():
@@ -172,6 +184,21 @@ class WebRTCServer:
def send_data(self, data):
self.hub.send_data(data)
def alert(self, timestamp, summary, level):
payload = {
"seat_id": self.seat,
"timestamp": timestamp,
"summary": summary,
"level": level,
}
if self.sio is not None and self.sio.connected:
asyncio.run_coroutine_threadsafe(
self.sio.emit("alert", payload), self.background_loop
)
else:
print("Warning: sio is not connected, skip signaling alert emit")
return Alert(name=f"seat{self.seat}_{timestamp}.mp4", client=minio_client)
def stop(self):
if self.background_loop.is_running():
self.background_loop.call_soon_threadsafe(self.background_loop.stop)
@@ -228,4 +255,4 @@ def route_channel(channel):
print(f"Latency: {now - pre}ms")
case _:
print(f"Unknown Channel {channel.label}")
print(f"Unknown Channel {channel.label}")