Files
2026-03-26 17:48:20 +08:00

230 lines
7.2 KiB
Python

from uuid import UUID
from fastapi import Depends, APIRouter
from config.httpClient import HttpClient
from config.minIO import get_temp_url
from config.redis import redis_client
from config.security import get_user_id_from_token
from db.postgres import get_dept_ids_by_user_id, get_dept_id_by_user_id
from db.postgres.sentinel import *
from models.BaseResponse import BaseResponse
# -------------------- 设备接口 --------------------
sentinel_router = APIRouter()
@sentinel_router.get("/record/list")
async def get_sentinel_record_list(
page: int = 1,
page_size: int = 10,
id: str | None = None,
license_plate: str | None = None,
vehicle_type: str | None = None,
is_inspected: int | None = None,
livestock_type: str = None,
livestock_source: str | None = None,
start_time: str | None = None,
end_time: str | None = None,
user_id: UUID = Depends(get_user_id_from_token),
):
if not user_id:
return {"error": "userId is required"}
dept_ids = get_dept_ids_by_user_id(user_id)
print(dept_ids) # 输出所有部门的 dept_id 列表
records, total = get_sentinel_record_list_db_page(
page,
page_size,
id,
license_plate,
vehicle_type,
is_inspected,
livestock_source,
livestock_type,
dept_ids,
start_time,
end_time,
)
return BaseResponse(data={"list": records, "total": total})
@sentinel_router.post("/record")
async def create_sentinel_record(
data: dict, user_id: UUID = Depends(get_user_id_from_token)
):
if not user_id:
return {"error": "userId is required"}
dept_id = get_dept_id_by_user_id(user_id)
new_id = insert_sentinel_record(data, dept_id)
return BaseResponse(data={"id": new_id})
@sentinel_router.put("/record/{id}")
async def update_sentinel_record(
id: str, data: dict, user_id: UUID = Depends(get_user_id_from_token)
):
if not user_id:
return {"error": "userId is required"}
count = update_sentinel_record_db(id, data)
if count == 0:
return BaseResponse(status=False, message="记录不存在", data=None)
return BaseResponse(data=True)
@sentinel_router.patch("/record/{id}")
async def patch_sentinel_record(
id: str, data: dict, user_id: UUID = Depends(get_user_id_from_token)
):
if not user_id:
return {"error": "userId is required"}
count = patch_sentinel_record_db(id, data)
if count == 0:
return BaseResponse(status=False, message="记录不存在", data=None)
return BaseResponse(data=True)
@sentinel_router.delete("/record/{id}")
async def delete_sentinel_record(
id: str, user_id: UUID = Depends(get_user_id_from_token)
):
if not user_id:
return {"error": "userId is required"}
deleted = delete_sentinel_record_db(id)
if deleted == 0:
return BaseResponse(status=False, message="记录不存在", data=None)
return BaseResponse(data=True)
@sentinel_router.get("/monitor/promotional/list")
async def get_sentinel_monitor_promotional_list(
user_id: UUID = Depends(get_user_id_from_token),
):
if not user_id:
return {"error": "userId is required"}
# 图片过期时间:7天
expiration_time = 60 * 60 * 24 * 7
return BaseResponse(
data=[
{
"id": 1,
"remark": "人员公示及岗位职责",
"url": get_temp_url(
"sentinel", "promotional/promotional (2).jpg", expiration_time
),
},
{
"id": 2,
"remark": "入川动物监督检查工作流程图",
"url": get_temp_url(
"sentinel", "promotional/promotional (1).jpg", expiration_time
),
},
{
"id": 3,
"remark": "四川省人民政府关于设立入川动物运输指定通道的通告",
"url": get_temp_url(
"sentinel", "promotional/promotional (3).jpg", expiration_time
),
},
{
"id": 4,
"remark": "四川省动物卫生监督检查站工作程序",
"url": get_temp_url(
"sentinel", "promotional/promotional (4).jpg", expiration_time
),
},
{
"id": 5,
"remark": "四川省动物卫生监督检查站无害化处理制度",
"url": get_temp_url(
"sentinel", "promotional/promotional (5).jpg", expiration_time
),
},
{
"id": 6,
"remark": "四川省动物卫生监督检查站工作人员行为规范",
"url": get_temp_url(
"sentinel", "promotional/promotional (6).jpg", expiration_time
),
},
]
)
http_client = HttpClient()
@sentinel_router.get("/monitor/list")
async def get_sentinel_monitor_list(
user_id: UUID = Depends(get_user_id_from_token),
):
if not user_id:
return {"error": "userId is required"}
# 尝试从 Redis 获取 accessToken
access_token = redis_client.get_value("ys7:access_token")
if not access_token:
url = "https://open.ys7.com/api/lapp/token/get"
payload = {
"appKey": "c85e53559223457f90f06cd215513c3d",
"appSecret": "9424419da5292707eff2007e9ae37f0d",
}
result = await http_client.post(url, data=payload)
access_token = result["data"]["accessToken"]
redis_client.set_value(
"ys7:access_token", access_token, expire=7 * 24 * 60 * 60 # 7天过期
)
url = "https://open.ys7.com/api/lapp/v2/live/address/get"
device_serials = {
# "GG9175589": [1, 2, 3, 4],
"GG9175589": [1, 3],
"GG9175555": [1, 2],
}
video_expire_time = 25 * 24 * 60 * 60 # 25 天
res = []
for device_serial, channels in device_serials.items():
for channelNo in channels:
live_key = f"ys7:live:{device_serial}:{channelNo}"
cached_live = redis_client.get_value(live_key)
if cached_live:
video_id = cached_live.get("id")
video_url = cached_live.get("url")
else:
payload = {
"accessToken": access_token,
"deviceSerial": device_serial,
"channelNo": channelNo,
"protocol": 4,
"expireTime": video_expire_time,
"supportH265": 0,
"quality": 2,
}
result = await http_client.post(url, data=payload)
video_data = result.get("data")
if not video_data:
continue # 或者记录异常日志
video_id = video_data["id"]
video_url = video_data["url"]
redis_client.set_value(
live_key,
{"id": video_id, "url": video_url},
expire=video_expire_time,
)
res.append({"id": video_id, "url": video_url})
return BaseResponse(data=res)