modify: sophon 视频文件上传、告警接口

This commit is contained in:
2026-07-28 14:16:00 +08:00
parent 79eb7fe5cc
commit 320f6c19a2
17 changed files with 575 additions and 52 deletions
+5
View File
@@ -5,10 +5,15 @@ from .board_ping import board_ping
from .list_devices import list_devices
from .get_device import get_device
from .get_offline_devices import get_offline_devices
from .list_alarms import list_alarms
from .get_alarm import get_alarm
__all__ = [
"board_ping",
"list_devices",
"get_device",
"get_offline_devices",
"list_alarms",
"get_alarm",
]
+3 -1
View File
@@ -32,11 +32,13 @@ async def board_ping(request: BoardPingRequest, worker_id: str = "") -> dict:
board_id = request.BoardId
try:
if db is None:
raise RuntimeError("Database not initialized")
async with db.get_session() as session:
from ..models.orm import BoardHeartbeat
from sqlalchemy import select
stmt = select(BoardHeartbeat).where(BoardHeartbeat.board_id == board_id)
stmt = select(BoardHeartbeat).where(BoardHeartbeat.board_id == board_id) # type: ignore
result = await session.execute(stmt)
record: BoardHeartbeat | None = result.scalar_one_or_none()
+57
View File
@@ -0,0 +1,57 @@
"""
查询单条告警详情 — GET /aibox/get_alarm?alarm_id=xxx
"""
import json
import logging
from chongming_worker import rpc_method
logger = logging.getLogger(__name__)
@rpc_method(
subject="aibox_sophon_adapter.get_alarm",
method="GET",
summary="查询单条告警详情",
)
async def get_alarm(alarm_id: str, worker_id: str = "") -> dict:
"""根据 AlarmId 查询完整的告警报文数据"""
from ..global_var import db
try:
if db is None:
raise RuntimeError("Database not initialized")
async with db.get_session() as session:
from ..models.orm import AlarmRecord
from sqlalchemy import select
stmt = select(AlarmRecord).where(AlarmRecord.alarm_id == alarm_id) # type: ignore
result = await session.execute(stmt)
record = result.scalar_one_or_none()
if record is None:
return {"error": f"告警不存在: {alarm_id}"}
alarm_data = {}
try:
alarm_data = json.loads(record.alarm_data) if record.alarm_data else {}
except json.JSONDecodeError:
pass
return {
"alarm_id": record.alarm_id,
"board_id": record.board_id,
"board_ip": record.board_ip,
"summary": record.summary,
"task_session": record.task_session,
"media_name": record.media_name,
"video_file": record.video_file,
"alarm_time": record.alarm_time,
"alarm_timestamp": record.alarm_timestamp,
"alarm_data": alarm_data,
"created_at": record.created_at.isoformat() if record.created_at else None,
}
except Exception as e:
logger.error(f"[get_alarm] 查询失败 (alarm_id={alarm_id}): {e}")
return {"error": str(e)}
+3 -1
View File
@@ -20,11 +20,13 @@ async def get_device(board_id: str, worker_id: str = "") -> dict:
from ..global_var import db, heartbeat_timeout
try:
if db is None:
raise RuntimeError("Database not initialized")
async with db.get_session() as session:
from ..models.orm import BoardHeartbeat
from sqlalchemy import select
stmt = select(BoardHeartbeat).where(BoardHeartbeat.board_id == board_id)
stmt = select(BoardHeartbeat).where(BoardHeartbeat.board_id == board_id) # type: ignore
result = await session.execute(stmt)
record: BoardHeartbeat | None = result.scalar_one_or_none()
@@ -21,11 +21,13 @@ async def get_offline_devices(worker_id: str = "") -> dict:
offline_devices = []
try:
if db is None:
raise RuntimeError("Database not initialized")
async with db.get_session() as session:
from ..models.orm import BoardHeartbeat
from sqlalchemy import select
from sqlalchemy import desc, select
stmt = select(BoardHeartbeat).order_by(BoardHeartbeat.last_ping_time.desc())
stmt = select(BoardHeartbeat).order_by(desc(BoardHeartbeat.last_ping_time)) # type: ignore
result = await session.execute(stmt)
records = result.scalars().all()
@@ -0,0 +1,85 @@
"""
查询告警记录列表 — GET /aibox/list_alarms
"""
import json
import logging
from chongming_worker import rpc_method
logger = logging.getLogger(__name__)
@rpc_method(
subject="aibox_sophon_adapter.list_alarms",
method="GET",
summary="查询告警记录列表",
)
async def list_alarms(
board_id: str = "",
summary: str = "",
page: int = 1,
page_size: int = 20,
worker_id: str = "",
) -> dict:
"""分页查询告警记录,支持按设备ID和告警类型筛选"""
from ..global_var import db
alarms = []
total = 0
try:
if db is None:
raise RuntimeError("Database not initialized")
async with db.get_session() as session:
from ..models.orm import AlarmRecord
from sqlalchemy import select, func, desc
# 构建查询
conditions = []
if board_id:
conditions.append(AlarmRecord.board_id == board_id) # type: ignore
if summary:
conditions.append(AlarmRecord.summary == summary) # type: ignore
# 总数
count_stmt = select(func.count()).select_from(AlarmRecord)
if conditions:
from sqlalchemy import and_
count_stmt = count_stmt.where(and_(*conditions))
count_result = await session.execute(count_stmt)
total = count_result.scalar() or 0
# 分页数据
stmt = select(AlarmRecord).order_by(desc(AlarmRecord.created_at))
if conditions:
from sqlalchemy import and_
stmt = stmt.where(and_(*conditions))
stmt = stmt.offset((page - 1) * page_size).limit(page_size)
result = await session.execute(stmt)
records = result.scalars().all()
for record in records:
alarm_data = {}
try:
alarm_data = json.loads(record.alarm_data) if record.alarm_data else {}
except json.JSONDecodeError:
pass
alarms.append({
"alarm_id": record.alarm_id,
"board_id": record.board_id,
"board_ip": record.board_ip,
"summary": record.summary,
"task_session": record.task_session,
"media_name": record.media_name,
"video_file": record.video_file,
"alarm_time": record.alarm_time,
"alarm_timestamp": record.alarm_timestamp,
"result_type": alarm_data.get("Result", {}).get("Type", ""),
"created_at": record.created_at.isoformat() if record.created_at else None,
})
except Exception as e:
logger.error(f"[list_alarms] 查询失败: {e}")
return {"alarms": [], "total": 0, "page": page, "page_size": page_size, "error": str(e)}
return {"alarms": alarms, "total": total, "page": page, "page_size": page_size}
+4 -2
View File
@@ -21,11 +21,13 @@ async def list_devices(worker_id: str = "") -> dict:
devices = []
try:
if db is None:
raise RuntimeError("Database not initialized")
async with db.get_session() as session:
from ..models.orm import BoardHeartbeat
from sqlalchemy import select
from sqlalchemy import select, desc
stmt = select(BoardHeartbeat).order_by(BoardHeartbeat.updated_at.desc())
stmt = select(BoardHeartbeat).order_by(desc(BoardHeartbeat.updated_at)) # type: ignore
result = await session.execute(stmt)
records = result.scalars().all()