AI实验室后端
This commit is contained in:
@@ -2,6 +2,9 @@ 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 *
|
||||
@@ -96,3 +99,94 @@ async def delete_sentinel_record(
|
||||
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"}
|
||||
return BaseResponse(
|
||||
data=[
|
||||
{
|
||||
"id": 1,
|
||||
"remark": "人员公示及岗位职责",
|
||||
"url": get_temp_url("sentinel", "promotional/promotional (2).jpg"),
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"remark": "入川动物监督检查工作流程图",
|
||||
"url": get_temp_url("sentinel", "promotional/promotional (1).jpg"),
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"remark": "四川省人民政府关于设立人川动物运输指定通道的通告",
|
||||
"url": get_temp_url("sentinel", "promotional/promotional (3).jpg"),
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
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 = ["BG2493625"]
|
||||
device_serials = ["BG2493625", "GH3713250", "GH3714496", "GH3714497"]
|
||||
|
||||
video_expire_time = 25 * 24 * 60 * 60 # 25 天
|
||||
res = []
|
||||
for device_serial in device_serials:
|
||||
live_key = f"ys7:live:{device_serial}"
|
||||
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,
|
||||
"protocol": 4, # 流播放协议,1-ezopen、2-hls、3-rtmp、4-flv,默认为1
|
||||
"expireTime": video_expire_time, # 25天
|
||||
"supportH265": 0,
|
||||
"quality": 2,
|
||||
}
|
||||
result = await http_client.post(url, data=payload)
|
||||
video_id = result["data"]["id"]
|
||||
video_url = result["data"]["url"]
|
||||
# 存到 Redis,自动序列化为 JSON,过期 25天
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user