fix: 设备型号名称改为从 db tb_vechicle_base_test 动态查询,修复新增型号显示 Unknown 的问题
- handlers.py: B2 数据硬编码 dev_model_map {1:PD132,2:DLD110} → await get_dev_type_name()
- models.py: B4 波动数据硬编码 map → 同上
- models.py: 新增 get_dev_type_name() 带内存缓存,首次加载后缓存 type_num→dev_name
- models.py: 新增 refresh_dev_type_names() 供工装配置页新增型号后刷新缓存
This commit is contained in:
@@ -33,6 +33,7 @@ from src.models import (
|
|||||||
insert_device_log,
|
insert_device_log,
|
||||||
update_device_status,
|
update_device_status,
|
||||||
get_all_device_serials,
|
get_all_device_serials,
|
||||||
|
get_dev_type_name,
|
||||||
)
|
)
|
||||||
from src.dg430 import (
|
from src.dg430 import (
|
||||||
parse_b2_status,
|
parse_b2_status,
|
||||||
@@ -293,8 +294,7 @@ async def parse_loop():
|
|||||||
fault_info = decode_fault_info(status.fault)
|
fault_info = decode_fault_info(status.fault)
|
||||||
relay_info = decode_relay_info(status.relay_out)
|
relay_info = decode_relay_info(status.relay_out)
|
||||||
|
|
||||||
dev_model_map = {1: "PD132", 2: "DLD110"}
|
str_type = await get_dev_type_name(status.dev_model)
|
||||||
str_type = dev_model_map.get(status.dev_model, f"Unknown({status.dev_model})")
|
|
||||||
|
|
||||||
await insert_test_result(
|
await insert_test_result(
|
||||||
dnt_id=dnt_id,
|
dnt_id=dnt_id,
|
||||||
|
|||||||
@@ -498,6 +498,42 @@ async def get_fixture_dev_type(dnt_id: int) -> int:
|
|||||||
return row[0] if row else 0
|
return row[0] if row else 0
|
||||||
|
|
||||||
|
|
||||||
|
# ─── 设备型号名称缓存 ──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
_dev_type_name_cache: dict[int, str] = {}
|
||||||
|
_cache_loaded = False
|
||||||
|
|
||||||
|
|
||||||
|
async def _load_dev_type_names():
|
||||||
|
"""从 tb_vechicle_base_test 加载 type_num → dev_name 映射"""
|
||||||
|
global _dev_type_name_cache, _cache_loaded
|
||||||
|
pool = await get_pool()
|
||||||
|
async with pool.acquire() as conn:
|
||||||
|
async with conn.cursor() as cur:
|
||||||
|
await cur.execute(
|
||||||
|
"SELECT type_num, dev_name FROM tb_vechicle_base_test"
|
||||||
|
)
|
||||||
|
rows = await cur.fetchall()
|
||||||
|
_dev_type_name_cache = {row[0]: row[1] for row in rows if row[1]}
|
||||||
|
_cache_loaded = True
|
||||||
|
logger.debug(f"设备型号名称缓存已加载: {_dev_type_name_cache}")
|
||||||
|
|
||||||
|
|
||||||
|
async def get_dev_type_name(dev_type: int) -> str:
|
||||||
|
"""根据设备型号编码获取名称(从 tb_vechicle_base_test 查询,带内存缓存)"""
|
||||||
|
global _cache_loaded
|
||||||
|
if not _cache_loaded:
|
||||||
|
await _load_dev_type_names()
|
||||||
|
return _dev_type_name_cache.get(dev_type, f"Unknown({dev_type})")
|
||||||
|
|
||||||
|
|
||||||
|
async def refresh_dev_type_names():
|
||||||
|
"""刷新型号名称缓存(工装配置页新增型号后调用)"""
|
||||||
|
global _cache_loaded
|
||||||
|
_cache_loaded = False
|
||||||
|
await _load_dev_type_names()
|
||||||
|
|
||||||
|
|
||||||
async def insert_test_result(dnt_id: int, dpg430_addr: int, pcnum: str,
|
async def insert_test_result(dnt_id: int, dpg430_addr: int, pcnum: str,
|
||||||
serialnum: int, sub_type: int, str_type: str,
|
serialnum: int, sub_type: int, str_type: str,
|
||||||
iffinish: str, fault_info: str, relay_out: str,
|
iffinish: str, fault_info: str, relay_out: str,
|
||||||
@@ -536,8 +572,7 @@ async def insert_wave_data(dnt_id: int, dpg430_addr: int,
|
|||||||
"""插入 0xB4 波动测试上报数据到 tb_state_tst"""
|
"""插入 0xB4 波动测试上报数据到 tb_state_tst"""
|
||||||
coil_id, simulate_car_id = await get_fixture_coil_car_ids(dnt_id)
|
coil_id, simulate_car_id = await get_fixture_coil_car_ids(dnt_id)
|
||||||
dev_type = await get_fixture_dev_type(dnt_id)
|
dev_type = await get_fixture_dev_type(dnt_id)
|
||||||
dev_model_map = {1: "PD132", 2: "DLD110"}
|
str_type = await get_dev_type_name(dev_type) if dev_type else ""
|
||||||
str_type = dev_model_map.get(dev_type, f"Unknown({dev_type})") if dev_type else ""
|
|
||||||
pool = await get_pool()
|
pool = await get_pool()
|
||||||
async with pool.acquire() as conn:
|
async with pool.acquire() as conn:
|
||||||
async with conn.cursor() as cur:
|
async with conn.cursor() as cur:
|
||||||
|
|||||||
Reference in New Issue
Block a user