From b5a8d6a71d032ad2573e1dd5cb8843433bf710ce Mon Sep 17 00:00:00 2001 From: caoqianming Date: Tue, 24 Mar 2026 15:03:26 +0800 Subject: [PATCH] fix(engine): correct fault equipment ID lookup and all_roles data structure Co-Authored-By: Claude Sonnet 4.6 --- src/control/engine.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/control/engine.rs b/src/control/engine.rs index 65b8b21..bb24868 100644 --- a/src/control/engine.rs +++ b/src/control/engine.rs @@ -63,7 +63,7 @@ async fn tick_unit( // kind -> role -> EquipmentRolePoint (first equipment per kind wins) let mut kind_roles: HashMap> = HashMap::new(); // all role maps for fault/comm scanning across all equipment - let mut all_roles: Vec> = Vec::new(); + let mut all_roles: Vec<(Uuid, HashMap)> = Vec::new(); for equip in &equipment_list { match crate::service::get_equipment_role_points(&state.pool, equip.id).await { @@ -84,7 +84,7 @@ async fn tick_unit( kind_roles.insert(kind.clone(), role_map.clone()); } } - all_roles.push(role_map); + all_roles.push((equip.id, role_map)); } Err(e) => { tracing::warn!( @@ -102,7 +102,7 @@ async fn tick_unit( .await; // ── Communication check ────────────────────────────────── - let any_bad_quality = all_roles.iter().flat_map(|r| r.values()).any(|rp| { + let any_bad_quality = all_roles.iter().flat_map(|(_, r)| r.values()).any(|rp| { monitor_guard .get(&rp.point_id) .map(|m| m.quality != PointQuality::Good) @@ -122,7 +122,7 @@ async fn tick_unit( } // ── Fault check ────────────────────────────────────────── - let any_flt = all_roles.iter().any(|roles| { + let any_flt = all_roles.iter().any(|(_, roles)| { roles .get("flt") .and_then(|rp| monitor_guard.get(&rp.point_id)) @@ -135,18 +135,16 @@ async fn tick_unit( if any_flt && !runtime.fault_locked { // Find which equipment triggered the fault - let flt_eq_id = equipment_list + let flt_eq_id = all_roles .iter() - .find(|_e| { - all_roles.iter().any(|roles| { - roles - .get("flt") - .and_then(|rp| monitor_guard.get(&rp.point_id)) - .map(|m| monitor_value_as_bool(m)) - .unwrap_or(false) - }) + .find(|(_, roles)| { + roles + .get("flt") + .and_then(|rp| monitor_guard.get(&rp.point_id)) + .map(|m| monitor_value_as_bool(m)) + .unwrap_or(false) }) - .map(|e| e.id) + .map(|(eq_id, _)| *eq_id) .unwrap_or(Uuid::nil()); runtime.fault_locked = true;