优化
This commit is contained in:
36
schema/device_action_schema.py
Normal file
36
schema/device_action_schema.py
Normal file
@ -0,0 +1,36 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional, List
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 请求模型(新增记录用,极简)
|
||||
# ------------------------------
|
||||
class DeviceActionCreate(BaseModel):
|
||||
"""设备操作记录创建模型(0=离线,1=上线)"""
|
||||
client_ip: str = Field(..., description="客户端IP")
|
||||
action: int = Field(..., ge=0, le=1, description="操作状态(0=离线,1=上线)")
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 响应模型(单条记录)
|
||||
# ------------------------------
|
||||
class DeviceActionResponse(BaseModel):
|
||||
"""设备操作记录响应模型(与自增表对齐)"""
|
||||
id: int = Field(..., description="自增主键ID")
|
||||
client_ip: Optional[str] = Field(None, description="客户端IP")
|
||||
action: Optional[int] = Field(None, description="操作状态(0=离线,1=上线)")
|
||||
created_at: datetime = Field(..., description="记录创建时间")
|
||||
updated_at: datetime = Field(..., description="记录更新时间")
|
||||
|
||||
# 支持从数据库结果直接转换
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 列表响应模型(仅含 total + device_actions)
|
||||
# ------------------------------
|
||||
class DeviceActionListResponse(BaseModel):
|
||||
"""设备操作记录列表(仅核心返回字段)"""
|
||||
total: int = Field(..., description="总记录数")
|
||||
device_actions: List[DeviceActionResponse] = Field(..., description="操作记录列表")
|
@ -1,4 +1,3 @@
|
||||
import hashlib
|
||||
from datetime import datetime
|
||||
from typing import Optional, List, Dict
|
||||
|
||||
@ -6,42 +5,31 @@ from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 请求模型(前端传参校验)
|
||||
# 请求模型
|
||||
# ------------------------------
|
||||
class DeviceCreateRequest(BaseModel):
|
||||
"""设备流信息创建请求模型"""
|
||||
"""设备流信息创建请求模型(与数据库表字段对齐)"""
|
||||
ip: Optional[str] = Field(..., max_length=100, description="设备IP地址")
|
||||
hostname: Optional[str] = Field(None, max_length=100, description="设备别名")
|
||||
params: Optional[Dict] = Field(None, description="设备详细信息")
|
||||
|
||||
|
||||
def md5_encrypt(text: str) -> str:
|
||||
"""对字符串进行MD5加密"""
|
||||
if not text:
|
||||
return ""
|
||||
md5_hash = hashlib.md5()
|
||||
md5_hash.update(text.encode('utf-8'))
|
||||
return md5_hash.hexdigest()
|
||||
params: Optional[Dict] = Field(None, description="设备详细信息(JSON格式)")
|
||||
|
||||
|
||||
# ------------------------------
|
||||
# 响应模型(后端返回设备数据)
|
||||
# 响应模型(后端返回数据)- 严格对齐数据库表字段
|
||||
# ------------------------------
|
||||
class DeviceResponse(BaseModel):
|
||||
"""设备流信息响应模型(字段与表结构完全对齐)"""
|
||||
id: int = Field(..., description="设备ID")
|
||||
"""设备流信息响应模型(与数据库表字段完全一致)"""
|
||||
id: int = Field(..., description="设备主键ID")
|
||||
client_ip: Optional[str] = Field(None, max_length=100, description="设备IP地址")
|
||||
hostname: Optional[str] = Field(None, max_length=100, description="设备别名")
|
||||
rtmp_push_url: Optional[str] = Field(None, description="需要推送的RTMP地址")
|
||||
live_webrtc_url: Optional[str] = Field(None, description="直播的Webrtc地址")
|
||||
detection_webrtc_url: Optional[str] = Field(None, description="检测的Webrtc地址")
|
||||
device_online_status: int = Field(..., description="设备在线状态(1-在线、0-离线)")
|
||||
device_type: Optional[str] = Field(None, description="设备类型")
|
||||
alarm_count: int = Field(..., description="报警次数")
|
||||
params: Optional[str] = Field(None, description="设备详细信息")
|
||||
params: Optional[str] = Field(None, description="设备详细信息(JSON字符串)")
|
||||
created_at: datetime = Field(..., description="记录创建时间")
|
||||
updated_at: datetime = Field(..., description="记录更新时间")
|
||||
|
||||
# 支持从数据库查询结果转换
|
||||
# 支持从数据库查询结果直接转换
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user