优化
This commit is contained in:
@ -1,25 +1,11 @@
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
|
||||
from fastapi import HTTPException, Query, APIRouter, Depends, Request
|
||||
from fastapi import APIRouter, Query, HTTPException
|
||||
from mysql.connector import Error as MySQLError
|
||||
|
||||
from ds.config import LIVE_CONFIG
|
||||
from ds.db import db
|
||||
from middle.auth_middleware import get_current_user
|
||||
# 注意:导入的Schema已更新字段
|
||||
from schema.device_schema import (
|
||||
DeviceCreateRequest,
|
||||
DeviceResponse,
|
||||
DeviceListResponse,
|
||||
md5_encrypt
|
||||
)
|
||||
from schema.device_schema import DeviceCreateRequest, DeviceResponse, DeviceListResponse
|
||||
from schema.response_schema import APIResponse
|
||||
from schema.user_schema import UserResponse
|
||||
|
||||
# 导入之前封装的WEBRTC处理函数
|
||||
from core.rtmp import rtmp_pull_video_stream
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/devices",
|
||||
@ -27,65 +13,128 @@ router = APIRouter(
|
||||
)
|
||||
|
||||
|
||||
# 在后台线程中运行WEBRTC处理
|
||||
def run_webrtc_processing(ip, webrtc_url):
|
||||
try:
|
||||
print(f"开始处理来自设备 {ip} 的WEBRTC流: {webrtc_url}")
|
||||
rtmp_pull_video_stream(webrtc_url)
|
||||
except Exception as e:
|
||||
print(f"WEBRTC处理出错: {str(e)}")
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 1. 创建设备信息
|
||||
# 内部工具方法 - 通过客户端IP增加设备报警次数
|
||||
# ------------------------------
|
||||
@router.post("/add", response_model=APIResponse, summary="创建设备信息")
|
||||
async def create_device(request: Request, device_data: DeviceCreateRequest):
|
||||
def increment_alarm_count_by_ip(client_ip: str) -> bool:
|
||||
"""
|
||||
通过客户端IP增加设备的报警次数(内部服务方法)
|
||||
|
||||
:param client_ip: 客户端IP地址
|
||||
:return: 操作是否成功
|
||||
"""
|
||||
if not client_ip:
|
||||
raise ValueError("客户端IP不能为空")
|
||||
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
conn = db.get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
# 检查设备是否存在
|
||||
cursor.execute("SELECT id FROM devices WHERE client_ip = %s", (client_ip,))
|
||||
if not cursor.fetchone():
|
||||
raise ValueError(f"客户端IP为 {client_ip} 的设备不存在")
|
||||
|
||||
# 报警次数加1,并更新时间戳
|
||||
update_query = """
|
||||
UPDATE devices
|
||||
SET alarm_count = alarm_count + 1,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE client_ip = %s
|
||||
"""
|
||||
cursor.execute(update_query, (client_ip,))
|
||||
conn.commit()
|
||||
|
||||
return True
|
||||
except MySQLError as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
raise Exception(f"更新报警次数失败:{str(e)}") from e
|
||||
finally:
|
||||
db.close_connection(conn, cursor)
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 内部工具方法 - 通过客户端IP更新设备在线状态
|
||||
# ------------------------------
|
||||
def update_online_status_by_ip(client_ip: str, online_status: int) -> bool:
|
||||
"""
|
||||
通过客户端IP更新设备的在线状态(内部服务方法)
|
||||
|
||||
:param client_ip: 客户端IP地址
|
||||
:param online_status: 在线状态(1-在线、0-离线)
|
||||
:return: 操作是否成功
|
||||
"""
|
||||
if not client_ip:
|
||||
raise ValueError("客户端IP不能为空")
|
||||
|
||||
# 验证状态值有效性
|
||||
if online_status not in (0, 1):
|
||||
raise ValueError("在线状态必须是0(离线)或1(在线)")
|
||||
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
conn = db.get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
# 检查设备是否存在
|
||||
cursor.execute("SELECT id FROM devices WHERE client_ip = %s", (client_ip,))
|
||||
if not cursor.fetchone():
|
||||
raise ValueError(f"客户端IP为 {client_ip} 的设备不存在")
|
||||
|
||||
# 更新在线状态和时间戳
|
||||
update_query = """
|
||||
UPDATE devices
|
||||
SET device_online_status = %s,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE client_ip = %s
|
||||
"""
|
||||
cursor.execute(update_query, (online_status, client_ip))
|
||||
conn.commit()
|
||||
|
||||
return True
|
||||
except MySQLError as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
raise Exception(f"更新设备在线状态失败:{str(e)}") from e
|
||||
finally:
|
||||
db.close_connection(conn, cursor)
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 原有接口保持不变
|
||||
# ------------------------------
|
||||
@router.post("/add", response_model=APIResponse, summary="创建设备信息")
|
||||
async def create_device(device_data: DeviceCreateRequest):
|
||||
# 原有代码保持不变
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
conn = db.get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
# 检查client_ip是否已存在
|
||||
cursor.execute("SELECT * FROM devices WHERE client_ip = %s", (device_data.ip,))
|
||||
existing_device = cursor.fetchone()
|
||||
if existing_device:
|
||||
# 设备创建成功后,在后台线程启动WEBRTC流处理
|
||||
threading.Thread(
|
||||
target=run_webrtc_processing,
|
||||
# args=(device_data.ip, existing_device["live_webrtc_url"]),
|
||||
args=(device_data.ip, existing_device["rtmp_push_url"]),
|
||||
|
||||
daemon=True # 设为守护线程,主程序退出时自动结束
|
||||
).start()
|
||||
# IP已存在时返回该设备信息
|
||||
# 更新设备状态为在线
|
||||
update_online_status_by_ip(client_ip=device_data.ip, online_status=1)
|
||||
# 返回信息
|
||||
return APIResponse(
|
||||
code=200,
|
||||
message=f"客户端IP {device_data.ip} 已存在",
|
||||
message=f"设备IP {device_data.ip} 已存在,返回已有设备信息",
|
||||
data=DeviceResponse(**existing_device)
|
||||
)
|
||||
|
||||
# 获取RTMP URL和WEBRTC URL配置
|
||||
rtmp_url = str(LIVE_CONFIG.get("rtmp_url", ""))
|
||||
webrtc_url = str(LIVE_CONFIG.get("webrtc_url", ""))
|
||||
|
||||
# 将设备详细信息(params)转换为JSON字符串
|
||||
device_params_json = json.dumps(device_data.params) if device_data.params else None
|
||||
|
||||
# 对JSON字符串进行MD5加密
|
||||
device_md5 = md5_encrypt(device_params_json) if device_params_json else ""
|
||||
|
||||
# 解析User-Agent获取设备类型
|
||||
from fastapi import Request
|
||||
request = Request(scope={"type": "http"})
|
||||
user_agent = request.headers.get("User-Agent", "").lower()
|
||||
|
||||
# 优先处理User-Agent为default的情况
|
||||
if user_agent == "default":
|
||||
# 检查params中是否存在os键
|
||||
if device_data.params and isinstance(device_data.params, dict) and "os" in device_data.params:
|
||||
device_type = device_data.params["os"]
|
||||
else:
|
||||
device_type = "unknown"
|
||||
device_type = device_data.params.get("os") if (
|
||||
device_data.params and isinstance(device_data.params, dict)) else "unknown"
|
||||
elif "windows" in user_agent:
|
||||
device_type = "windows"
|
||||
elif "android" in user_agent:
|
||||
@ -95,22 +144,16 @@ async def create_device(request: Request, device_data: DeviceCreateRequest):
|
||||
else:
|
||||
device_type = "unknown"
|
||||
|
||||
# 构建完整的WEBRTC URL
|
||||
full_webrtc_url = webrtc_url + device_md5
|
||||
device_params_json = json.dumps(device_data.params) if device_data.params else None
|
||||
|
||||
# SQL插入语句
|
||||
insert_query = """
|
||||
INSERT INTO devices
|
||||
(client_ip, hostname, rtmp_push_url, live_webrtc_url, detection_webrtc_url,
|
||||
device_online_status, device_type, alarm_count, params)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
(client_ip, hostname, device_online_status, device_type, alarm_count, params)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
cursor.execute(insert_query, (
|
||||
device_data.ip,
|
||||
device_data.hostname,
|
||||
rtmp_url + device_md5,
|
||||
full_webrtc_url, # 存储完整的WEBRTC URL
|
||||
"",
|
||||
1,
|
||||
device_type,
|
||||
0,
|
||||
@ -118,28 +161,22 @@ async def create_device(request: Request, device_data: DeviceCreateRequest):
|
||||
))
|
||||
conn.commit()
|
||||
|
||||
# 获取刚创建的设备信息
|
||||
device_id = cursor.lastrowid
|
||||
cursor.execute("SELECT * FROM devices WHERE id = %s", (device_id,))
|
||||
device = cursor.fetchone()
|
||||
new_device = cursor.fetchone()
|
||||
|
||||
# 设备创建成功后,在后台线程启动WEBRTC流处理
|
||||
threading.Thread(
|
||||
target=run_webrtc_processing,
|
||||
args=(device_data.ip, full_webrtc_url),
|
||||
daemon=True # 设为守护线程,主程序退出时自动结束
|
||||
).start()
|
||||
return APIResponse(
|
||||
code=200,
|
||||
message="设备创建成功,已开始处理WEBRTC流",
|
||||
data=DeviceResponse(**device)
|
||||
message="设备创建成功",
|
||||
data=DeviceResponse(**new_device)
|
||||
)
|
||||
|
||||
except MySQLError as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
raise Exception(f"创建设备失败:{str(e)}") from e
|
||||
except json.JSONDecodeError as e:
|
||||
raise Exception(f"设备信息JSON序列化失败:{str(e)}") from e
|
||||
raise Exception(f"设备详细信息JSON序列化失败:{str(e)}") from e
|
||||
except Exception as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
@ -148,139 +185,56 @@ async def create_device(request: Request, device_data: DeviceCreateRequest):
|
||||
db.close_connection(conn, cursor)
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 2. 获取设备列表
|
||||
# ------------------------------
|
||||
@router.get("/", response_model=APIResponse, summary="获取设备列表")
|
||||
@router.get("/", response_model=APIResponse, summary="获取设备列表(支持筛选分页)")
|
||||
async def get_device_list(
|
||||
page: int = Query(1, ge=1, description="页码"),
|
||||
page_size: int = Query(10, ge=1, le=100, description="每页条数"),
|
||||
device_type: str = Query(None, description="设备类型筛选"),
|
||||
online_status: int = Query(None, ge=0, le=1, description="在线状态筛选(1-在线、0-离线)")
|
||||
page: int = Query(1, ge=1, description="页码,默认第1页"),
|
||||
page_size: int = Query(10, ge=1, le=100, description="每页条数,1-100之间"),
|
||||
device_type: str = Query(None, description="按设备类型筛选"),
|
||||
online_status: int = Query(None, ge=0, le=1, description="按在线状态筛选")
|
||||
):
|
||||
# 原有代码保持不变
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
conn = db.get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
# 构建查询条件
|
||||
where_clause = []
|
||||
params = []
|
||||
|
||||
if device_type:
|
||||
where_clause.append("device_type = %s")
|
||||
params.append(device_type)
|
||||
|
||||
if online_status is not None:
|
||||
where_clause.append("device_online_status = %s")
|
||||
params.append(online_status)
|
||||
|
||||
# 总条数查询
|
||||
count_query = "SELECT COUNT(*) as total FROM devices"
|
||||
count_query = "SELECT COUNT(*) AS total FROM devices"
|
||||
if where_clause:
|
||||
count_query += " WHERE " + " AND ".join(where_clause)
|
||||
|
||||
cursor.execute(count_query, params)
|
||||
total = cursor.fetchone()["total"]
|
||||
|
||||
# 分页查询(SELECT * 会自动匹配表字段、响应模型已对齐)
|
||||
offset = (page - 1) * page_size
|
||||
query = "SELECT * FROM devices"
|
||||
list_query = "SELECT * FROM devices"
|
||||
if where_clause:
|
||||
query += " WHERE " + " AND ".join(where_clause)
|
||||
query += " ORDER BY id DESC LIMIT %s OFFSET %s"
|
||||
list_query += " WHERE " + " AND ".join(where_clause)
|
||||
list_query += " ORDER BY id DESC LIMIT %s OFFSET %s"
|
||||
params.extend([page_size, offset])
|
||||
|
||||
cursor.execute(query, params)
|
||||
devices = cursor.fetchall()
|
||||
|
||||
# 响应模型已更新为params字段、直接转换即可
|
||||
device_list = [DeviceResponse(**device) for device in devices]
|
||||
cursor.execute(list_query, params)
|
||||
device_list = cursor.fetchall()
|
||||
|
||||
return APIResponse(
|
||||
code=200,
|
||||
message="获取设备列表成功",
|
||||
data=DeviceListResponse(total=total, devices=device_list)
|
||||
data=DeviceListResponse(
|
||||
total=total,
|
||||
devices=[DeviceResponse(**device) for device in device_list]
|
||||
)
|
||||
)
|
||||
|
||||
except MySQLError as e:
|
||||
raise Exception(f"获取设备列表失败:{str(e)}") from e
|
||||
finally:
|
||||
db.close_connection(conn, cursor)
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 3. 获取单个设备详情
|
||||
# ------------------------------
|
||||
@router.get("/{device_id}", response_model=APIResponse, summary="获取设备详情")
|
||||
async def get_device_detail(
|
||||
device_id: int,
|
||||
current_user: UserResponse = Depends(get_current_user)
|
||||
):
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
conn = db.get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
# 查询设备信息(SELECT * 匹配表字段)
|
||||
query = "SELECT * FROM devices WHERE id = %s"
|
||||
cursor.execute(query, (device_id,))
|
||||
device = cursor.fetchone()
|
||||
|
||||
if not device:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"设备ID为 {device_id} 的设备不存在"
|
||||
)
|
||||
|
||||
# 响应模型已更新为params字段
|
||||
return APIResponse(
|
||||
code=200,
|
||||
message="获取设备详情成功",
|
||||
data=DeviceResponse(**device)
|
||||
)
|
||||
except MySQLError as e:
|
||||
raise Exception(f"获取设备详情失败:{str(e)}") from e
|
||||
finally:
|
||||
db.close_connection(conn, cursor)
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 4. 删除设备信息
|
||||
# ------------------------------
|
||||
@router.delete("/{device_id}", response_model=APIResponse, summary="删除设备信息")
|
||||
async def delete_device(
|
||||
device_id: int,
|
||||
current_user: UserResponse = Depends(get_current_user)
|
||||
):
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
conn = db.get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
# 检查设备是否存在
|
||||
cursor.execute("SELECT id FROM devices WHERE id = %s", (device_id,))
|
||||
if not cursor.fetchone():
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"设备ID为 {device_id} 的设备不存在"
|
||||
)
|
||||
|
||||
# 执行删除
|
||||
delete_query = "DELETE FROM devices WHERE id = %s"
|
||||
cursor.execute(delete_query, (device_id,))
|
||||
conn.commit()
|
||||
|
||||
return APIResponse(
|
||||
code=200,
|
||||
message=f"设备ID为 {device_id} 的设备已成功删除",
|
||||
data=None
|
||||
)
|
||||
except MySQLError as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
raise Exception(f"删除设备失败:{str(e)}") from e
|
||||
finally:
|
||||
db.close_connection(conn, cursor)
|
||||
|
Reference in New Issue
Block a user