diff --git a/src/models.py b/src/models.py index e7464a1..03aa85a 100644 --- a/src/models.py +++ b/src/models.py @@ -259,6 +259,67 @@ async def _create_tables(pool: aiomysql.Pool): ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 """) + # 9. 线圈参数表 + await cur.execute(""" + CREATE TABLE IF NOT EXISTS `tb_coil_info` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `coil_num` VARCHAR(45) DEFAULT '' COMMENT '线圈编号', + `name` VARCHAR(100) DEFAULT '' COMMENT '名称', + `induct` FLOAT DEFAULT 0 COMMENT '电感量', + `shape` VARCHAR(20) DEFAULT '' COMMENT '形状(矩形、圆形等)', + `length` FLOAT DEFAULT 0 COMMENT '长 cm(矩形有效)', + `width` FLOAT DEFAULT 0 COMMENT '宽 cm(矩形有效)', + `radius` FLOAT DEFAULT 0 COMMENT '半径 cm(圆形有效)', + `turns` INT DEFAULT 0 COMMENT '圈数', + `resistance` FLOAT DEFAULT 0 COMMENT '电阻 欧姆', + `material` VARCHAR(50) DEFAULT '' COMMENT '材质', + `remark` VARCHAR(500) DEFAULT '' COMMENT '备注', + `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP, + `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 + """) + + # 10. 模拟车辆参数表 + await cur.execute(""" + CREATE TABLE IF NOT EXISTS `tb_simulate_car` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `simulate_num` VARCHAR(45) DEFAULT '' COMMENT '模拟编号', + `name` VARCHAR(100) DEFAULT '' COMMENT '名称', + `shape` VARCHAR(20) DEFAULT '' COMMENT '形状(矩形、圆形等)', + `length` FLOAT DEFAULT 0 COMMENT '长 cm(矩形有效)', + `width` FLOAT DEFAULT 0 COMMENT '宽 cm(矩形有效)', + `radius` FLOAT DEFAULT 0 COMMENT '半径 cm(圆形有效)', + `material` VARCHAR(50) DEFAULT '' COMMENT '材质(铁板、合金等)', + `remark` VARCHAR(500) DEFAULT '' COMMENT '备注', + `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP, + `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 + """) + + # V2.1.0 迁移:tb_fixture_param 增加线圈/模拟车辆关联 + for col, col_def in [ + ("coil_id", "INT DEFAULT NULL COMMENT 'FK → tb_coil_info.id'"), + ("simulate_car_id", "INT DEFAULT NULL COMMENT 'FK → tb_simulate_car.id'"), + ]: + try: + await cur.execute( + f"ALTER TABLE `tb_fixture_param` ADD COLUMN `{col}` {col_def}" + ) + except Exception: + pass + + # V2.1.0 迁移:tb_state_tst 增加线圈/模拟车辆关联 + for col, col_def in [ + ("coil_id", "INT DEFAULT NULL COMMENT 'FK → tb_coil_info.id'"), + ("simulate_car_id", "INT DEFAULT NULL COMMENT 'FK → tb_simulate_car.id'"), + ]: + try: + await cur.execute( + f"ALTER TABLE `tb_state_tst` ADD COLUMN `{col}` {col_def}" + ) + except Exception: + pass + logger.info("数据库表初始化完成") @@ -376,6 +437,21 @@ async def upsert_dnt(serial: str, ip: str, port: int, mac: str, return cur.lastrowid +async def get_fixture_coil_car_ids(dnt_id: int) -> tuple[int | None, int | None]: + """从 tb_fixture_param 获取当前线圈和模拟车辆关联 ID""" + pool = await get_pool() + async with pool.acquire() as conn: + async with conn.cursor(aiomysql.DictCursor) as cur: + await cur.execute( + "SELECT coil_id, simulate_car_id FROM tb_fixture_param WHERE dnt_id=%s", + (dnt_id,), + ) + row = await cur.fetchone() + if row: + return row.get("coil_id"), row.get("simulate_car_id") + return None, None + + async def insert_test_result(dnt_id: int, dpg430_addr: int, pcnum: str, serialnum: int, sub_type: int, str_type: str, iffinish: str, fault_info: str, relay_out: str, @@ -385,6 +461,7 @@ async def insert_test_result(dnt_id: int, dpg430_addr: int, pcnum: str, test_mode: int = 0, data_source: str = "B2", relay_code: int = 0): """插入测试结果到 tb_state_tst""" + coil_id, simulate_car_id = await get_fixture_coil_car_ids(dnt_id) pool = await get_pool() async with pool.acquire() as conn: async with conn.cursor() as cur: @@ -394,13 +471,13 @@ async def insert_test_result(dnt_id: int, dpg430_addr: int, pcnum: str, test_mode, data_source, iffinish, fault_info, relay_out, ppvalue, idle_freq, enter_freq, exit_freq, enter_dist, exit_dist, enter_speed, exit_speed, - relay_code) - VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", + relay_code, coil_id, simulate_car_id) + VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", (dnt_id, dpg430_addr, pcnum, serialnum, sub_type, str_type, test_mode, data_source, iffinish, fault_info, relay_out, ppvalue, idle_freq, enter_freq, exit_freq, enter_dist, exit_dist, enter_speed, exit_speed, - relay_code), + relay_code, coil_id, simulate_car_id), ) @@ -410,6 +487,7 @@ async def insert_wave_data(dnt_id: int, dpg430_addr: int, near_dist: int, far_dist: int, enter_dist: int, leave_dist: int): """插入 0xB4 波动测试上报数据到 tb_state_tst""" + coil_id, simulate_car_id = await get_fixture_coil_car_ids(dnt_id) pool = await get_pool() async with pool.acquire() as conn: async with conn.cursor() as cur: @@ -418,12 +496,14 @@ async def insert_wave_data(dnt_id: int, dpg430_addr: int, (dnt_id, dpg430_addr, test_mode, data_source, relay_out, relay_code, remain_count, work_freq, curr_dist, speed, - near_dist, far_dist, b4_enter_dist, b4_leave_dist) - VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", + near_dist, far_dist, b4_enter_dist, b4_leave_dist, + coil_id, simulate_car_id) + VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", (dnt_id, dpg430_addr, 1, "B4", "", relay_code, remain_count, work_freq, curr_dist, speed, - near_dist, far_dist, enter_dist, leave_dist), + near_dist, far_dist, enter_dist, leave_dist, + coil_id, simulate_car_id), ) logger.info( f"B4波动数据已存储 dnt_id={dnt_id} relay=0x{relay_code:02X} "