-- Expand the single protocol-field catalog used by realtime telemetry,
-- history selection/export and alert rules. Vehicle mileage semantics remain
-- protocol-specific in the UI: JT808 is GPS mileage; GB32960/Yutong is meter mileage.
UPDATE vehicle_metric_definition
SET description='车辆累计行驶里程；JT808 为 GPS 里程，其余为仪表盘里程'
WHERE metric_key='total_mileage_km';

UPDATE vehicle_metric_protocol_mapping
SET source_field='yutong_mqtt.data.total_mileage_km'
WHERE metric_key='total_mileage_km' AND protocol='YUTONG_MQTT';

INSERT INTO vehicle_metric_definition
(metric_key,label,description,unit,category,value_type,protocols_json,searchable,chartable,alertable,enabled,sort_order) VALUES
('vehicle_status','车辆状态','GB/T 32960 整车状态码','','vehicle','numeric','["GB32960"]',1,0,1,1,90),
('charge_status','充电状态','GB/T 32960 充电状态码','','energy','numeric','["GB32960"]',1,0,1,1,100),
('total_voltage_v','总电压','动力电池总电压','V','energy','numeric','["GB32960"]',1,1,1,1,110),
('total_current_a','总电流','动力电池总电流','A','energy','numeric','["GB32960"]',1,1,1,1,120),
('insulation_kohm','绝缘电阻','整车绝缘电阻','kΩ','safety','numeric','["GB32960"]',1,1,1,1,130),
('fuel_cell_voltage_v','燃料电池电压','燃料电池系统输出电压','V','fuel-cell','numeric','["GB32960"]',1,1,1,1,140),
('fuel_cell_current_a','燃料电池电流','燃料电池系统输出电流','A','fuel-cell','numeric','["GB32960"]',1,1,1,1,150),
('hydrogen_consumption_kg_per_100km','百公里氢耗','燃料电池系统百公里氢气消耗量','kg/100km','fuel-cell','numeric','["GB32960"]',1,1,1,1,160),
('hydrogen_concentration_ppm','最高氢气浓度','燃料电池系统氢气浓度探针的最高读数','ppm','fuel-cell','numeric','["GB32960"]',1,1,1,1,170),
('hydrogen_pressure_mpa','最高氢气压力','燃料电池系统氢气压力探针的最高读数','MPa','fuel-cell','numeric','["GB32960"]',1,1,1,1,180),
('hydrogen_temperature_c','最高氢气温度','燃料电池系统氢气温度探针的最高读数','℃','fuel-cell','numeric','["GB32960"]',1,1,1,1,190),
('engine_speed_rpm','发动机转速','发动机曲轴转速','rpm','vehicle','numeric','["GB32960"]',1,1,1,1,200),
('max_cell_voltage_v','单体最高电压','动力电池单体电压最高值','V','energy','numeric','["GB32960"]',1,1,1,1,210),
('min_cell_voltage_v','单体最低电压','动力电池单体电压最低值','V','energy','numeric','["GB32960"]',1,1,1,1,220),
('max_battery_temperature_c','电池最高温度','动力电池温度探针最高值','℃','energy','numeric','["GB32960"]',1,1,1,1,230),
('min_battery_temperature_c','电池最低温度','动力电池温度探针最低值','℃','energy','numeric','["GB32960"]',1,1,1,1,240),
('gnss_satellite_count','GNSS 卫星数','JT/T 808 定位终端参与定位的卫星数量','颗','location','numeric','["JT808"]',1,1,1,1,250),
('network_signal_strength','通信信号强度','JT/T 808 终端无线通信信号强度','','quality','numeric','["JT808"]',1,1,1,1,260),
('fuel_l','油量','JT/T 808 终端上报的车辆油量','L','vehicle','numeric','["JT808"]',1,1,1,1,270)
ON DUPLICATE KEY UPDATE
label=VALUES(label),description=VALUES(description),unit=VALUES(unit),category=VALUES(category),
value_type=VALUES(value_type),protocols_json=VALUES(protocols_json),searchable=VALUES(searchable),
chartable=VALUES(chartable),alertable=VALUES(alertable),enabled=VALUES(enabled),sort_order=VALUES(sort_order);

INSERT INTO vehicle_metric_protocol_mapping(metric_key,protocol,source_field) VALUES
('vehicle_status','GB32960','gb32960.vehicle.vehicle_status'),
('charge_status','GB32960','gb32960.vehicle.charge_status'),
('total_voltage_v','GB32960','gb32960.vehicle.total_voltage_v'),
('total_current_a','GB32960','gb32960.vehicle.total_current_a'),
('insulation_kohm','GB32960','gb32960.vehicle.insulation_kohm'),
('fuel_cell_voltage_v','GB32960','gb32960.fuel_cell.fuel_cell_voltage_v'),
('fuel_cell_current_a','GB32960','gb32960.fuel_cell.fuel_cell_current_a'),
('hydrogen_consumption_kg_per_100km','GB32960','gb32960.fuel_cell.hydrogen_consumption_kg_per_100km'),
('hydrogen_concentration_ppm','GB32960','gb32960.fuel_cell.max_hydrogen_concentration_ppm'),
('hydrogen_pressure_mpa','GB32960','gb32960.fuel_cell.max_hydrogen_pressure_mpa'),
('hydrogen_temperature_c','GB32960','gb32960.fuel_cell.max_hydrogen_temperature_c'),
('engine_speed_rpm','GB32960','gb32960.engine.crank_speed_rpm'),
('max_cell_voltage_v','GB32960','gb32960.extreme.max_voltage_v'),
('min_cell_voltage_v','GB32960','gb32960.extreme.min_voltage_v'),
('max_battery_temperature_c','GB32960','gb32960.extreme.max_temp_c'),
('min_battery_temperature_c','GB32960','gb32960.extreme.min_temp_c'),
('gnss_satellite_count','JT808','jt808.location.additional.gnss_satellite_count'),
('network_signal_strength','JT808','jt808.location.additional.network_signal_strength'),
('fuel_l','JT808','jt808.location.additional.fuel_l')
ON DUPLICATE KEY UPDATE source_field=VALUES(source_field);
