重构:区域列表改为直接查 IoT 数据库

去掉 httpx 调 IoT HTTP 接口的方式,改为通过 IOT_DATABASE_URL
直接查询 IoT MySQL 的 ops_area 表,更简单可靠。
This commit is contained in:
2026-03-23 16:53:45 +08:00
parent 7235a20e25
commit 5b83e38f25

View File

@@ -1,15 +1,15 @@
"""
区域列表 API
代理查询 IoT 平台的区域数据,供前端摄像头页面选择区域使用。
带 5 分钟缓存,减少跨服务查询。
直接查询 IoT 平台 MySQL 数据库的区域,供前端摄像头页面选择区域使用。
带 5 分钟内存缓存,减少跨查询。
"""
import os
import time
from typing import List, Dict, Any
from fastapi import APIRouter
from sqlalchemy import create_engine, text
from app.config import settings
from app.yudao_compat import YudaoResponse
@@ -21,69 +21,58 @@ router = APIRouter(prefix="/api/area", tags=["区域管理"])
_area_list_cache: Dict[str, Any] = {"data": [], "expire": 0}
_CACHE_TTL = 300 # 5 分钟
# IoT 数据库连接(懒初始化)
_iot_engine = None
async def _fetch_area_list_from_iot() -> List[Dict]:
"""从 IoT 平台获取区域列表"""
import httpx
base_url = (
os.getenv("IOT_PLATFORM_URL", "")
or settings.work_order.base_url
)
if not base_url:
logger.warning("未配置 IoT 平台地址,无法查询区域列表")
def _get_iot_engine():
global _iot_engine
if _iot_engine is None:
url = settings.iot_db.url
if not url:
return None
_iot_engine = create_engine(url, pool_size=2, pool_recycle=3600)
return _iot_engine
def _fetch_area_list_from_db() -> List[Dict]:
"""直接查询 IoT MySQL 区域表"""
engine = _get_iot_engine()
if not engine:
logger.warning("未配置 IOT_DATABASE_URL无法查询区域列表")
return []
# 复用 notify_dispatch 中的 token 缓存
from app.services.notify_dispatch import _iot_token_cache
now = time.time()
if not _iot_token_cache["token"] or now > _iot_token_cache["expire"]:
async with httpx.AsyncClient(timeout=5) as client:
login_resp = await client.post(
f"{base_url}/admin-api/system/auth/login",
json={"username": "admin", "password": "admin123", "tenantName": "默认"},
headers={"tenant-id": "1"},
)
login_data = login_resp.json().get("data", {})
_iot_token_cache["token"] = login_data.get("accessToken", "")
_iot_token_cache["expire"] = now + 1200
token = _iot_token_cache["token"]
if not token:
return []
async with httpx.AsyncClient(timeout=10) as client:
resp = await client.get(
f"{base_url}/admin-api/ops/area/list",
headers={"tenant-id": "1", "Authorization": f"Bearer {token}"},
)
data = resp.json()
if data.get("code") == 0 and data.get("data"):
return data["data"]
return []
with engine.connect() as conn:
# ops_area 是芋道 IoT 平台的区域表
result = conn.execute(text(
"SELECT id, area_name, parent_id FROM ops_area "
"WHERE deleted = 0 ORDER BY sort, id"
))
return [
{"id": row[0], "areaName": row[1], "parentId": row[2]}
for row in result
]
@router.get("/list")
async def get_area_list():
"""
获取区域列表( IoT 平台代理查询,带 5 分钟缓存)
获取区域列表(直接查 IoT 数据库,带 5 分钟缓存)
返回: [{id, areaName, parentId, ...}]
返回: [{id, areaName, parentId}]
"""
now = time.time()
if _area_list_cache["data"] and now < _area_list_cache["expire"]:
return YudaoResponse.success(_area_list_cache["data"])
try:
areas = await _fetch_area_list_from_iot()
areas = _fetch_area_list_from_db()
if areas:
_area_list_cache["data"] = areas
_area_list_cache["expire"] = now + _CACHE_TTL
return YudaoResponse.success(areas)
except Exception as e:
logger.error(f"获取区域列表失败: {e}", exc_info=True)
# 有旧缓存则返回旧数据
if _area_list_cache["data"]:
return YudaoResponse.success(_area_list_cache["data"])
return YudaoResponse.error(500, f"获取区域列表失败: {e}")