From 55b8ecf2a624b22a7a7c6b958d9e46e6688fb80f Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 16 Jan 2026 12:31:46 +0100 Subject: [PATCH] not exactly nice.. --- satrs-example/src/acs/mgm.rs | 59 +++++++++++--------------------- satrs-example/src/eps/mod.rs | 65 ++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 42 deletions(-) diff --git a/satrs-example/src/acs/mgm.rs b/satrs-example/src/acs/mgm.rs index 1b47f1a..b779219 100644 --- a/satrs-example/src/acs/mgm.rs +++ b/satrs-example/src/acs/mgm.rs @@ -22,6 +22,7 @@ use satrs::mode::{ use satrs::request::{GenericMessage, MessageMetadata}; use satrs_example::config::components::NO_SENDER; +use crate::eps::PowerSwitchHelper; use crate::spi::SpiInterface; use serde::{Deserialize, Serialize}; @@ -150,16 +151,13 @@ impl Default for ModeHelpers { /// Example MGM device handler strongly based on the LIS3MDL MEMS device. #[allow(clippy::too_many_arguments)] -pub struct MgmHandlerLis3Mdl< - ComInterface: SpiInterface, - SwitchHelper: PowerSwitchInfo + PowerSwitcherCommandSender, -> { +pub struct MgmHandlerLis3Mdl { id: ComponentId, dev_str: &'static str, mode_node: ModeRequestHandlerMpscBounded, tc_rx: mpsc::Receiver, tm_tx: mpsc::SyncSender, - switch_helper: SwitchHelper, + switch_helper: PowerSwitchHelper, pub com_interface: ComInterface, shared_mgm_set: Arc>, //hk_helper: PusHkHelper, @@ -168,18 +166,14 @@ pub struct MgmHandlerLis3Mdl< stamp_helper: TimestampHelper, } -impl< - ComInterface: SpiInterface, - SwitchHelper: PowerSwitchInfo + PowerSwitcherCommandSender, - > MgmHandlerLis3Mdl -{ +impl MgmHandlerLis3Mdl { pub fn new( id: ComponentId, dev_str: &'static str, mode_node: ModeRequestHandlerMpscBounded, tc_rx: mpsc::Receiver, tm_tx: mpsc::SyncSender, - switch_helper: SwitchHelper, + switch_helper: PowerSwitchHelper, com_interface: ComInterface, shared_mgm_set: Arc>, ) -> Self { @@ -356,10 +350,7 @@ impl< self.mode_helpers.transition_state = TransitionState::PowerSwitching; } if self.mode_helpers.transition_state == TransitionState::PowerSwitching - && self - .switch_helper - .is_switch_on(SwitchId::Mgm0) - .expect("switch info error") + && self.switch_helper.is_switch_on(SwitchId::Mgm0) { self.mode_helpers.transition_state = TransitionState::Done; } @@ -373,21 +364,13 @@ impl< } } -impl< - ComInterface: SpiInterface, - SwitchHelper: PowerSwitchInfo + PowerSwitcherCommandSender, - > ModeProvider for MgmHandlerLis3Mdl -{ +impl ModeProvider for MgmHandlerLis3Mdl { fn mode_and_submode(&self) -> ModeAndSubmode { self.mode_helpers.current } } -impl< - ComInterface: SpiInterface, - SwitchHelper: PowerSwitchInfo + PowerSwitcherCommandSender, - > ModeRequestHandler for MgmHandlerLis3Mdl -{ +impl ModeRequestHandler for MgmHandlerLis3Mdl { type Error = ModeError; fn start_transition( @@ -472,21 +455,13 @@ impl< } } -impl< - ComInterface: SpiInterface, - SwitchHelper: PowerSwitchInfo + PowerSwitcherCommandSender, - > ModeNode for MgmHandlerLis3Mdl -{ +impl ModeNode for MgmHandlerLis3Mdl { fn id(&self) -> satrs::ComponentId { self.id as u32 } } -impl< - ComInterface: SpiInterface, - SwitchHelper: PowerSwitchInfo + PowerSwitcherCommandSender, - > ModeChild for MgmHandlerLis3Mdl -{ +impl ModeChild for MgmHandlerLis3Mdl { type Sender = mpsc::SyncSender>; fn add_mode_parent(&mut self, id: satrs::ComponentId, reply_sender: Self::Sender) { @@ -501,7 +476,7 @@ mod tests { sync::{mpsc, Arc}, }; - use models::ComponentId; + use models::{pcdu::SwitchRequest, ComponentId}; use satrs::{ mode::{ModeReply, ModeRequest}, mode_tree::ModeParent, @@ -511,7 +486,7 @@ mod tests { }; use satrs_minisim::acs::lis3mdl::MgmLis3RawValues; - use crate::eps::TestSwitchHelper; + use crate::eps::{pcdu::SharedSwitchSet, TestSwitchHelper}; use super::*; @@ -541,9 +516,11 @@ mod tests { pub mode_request_tx: mpsc::SyncSender>, pub mode_reply_rx_to_pus: mpsc::Receiver>, pub mode_reply_rx_to_parent: mpsc::Receiver>, + pub shared_switch_set: SharedSwitchSet, pub tc_tx: mpsc::SyncSender, pub tm_rx: mpsc::Receiver, - pub handler: MgmHandlerLis3Mdl, + pub switch_rx: mpsc::Receiver>, + pub handler: MgmHandlerLis3Mdl, } #[derive(Default)] @@ -595,14 +572,16 @@ mod tests { let (tc_tx, tc_rx) = mpsc::sync_channel(10); let (hk_reply_tx, _hk_reply_rx) = mpsc::sync_channel(10); let (_tm_tx, tm_rx) = mpsc::sync_channel(10); + let (switcher_tx, switcher_rx) = mpsc::sync_channel(10); let shared_mgm_set = Arc::default(); + let shared_switch_set = SharedSwitchSet::default(); let mut handler = MgmHandlerLis3Mdl::new( ComponentId::AcsMgm0, "TEST_MGM", mode_node, tc_rx, hk_reply_tx, - TestSwitchHelper::default(), + PowerSwitchHelper::new(switcher_tx, shared_switch_set.clone()), TestSpiInterface::default(), shared_mgm_set, ); @@ -612,6 +591,8 @@ mod tests { mode_request_tx: request_tx, mode_reply_rx_to_pus: reply_rx_to_ground, mode_reply_rx_to_parent: reply_rx_to_parent, + shared_switch_set, + switcher_rx, handler, tm_rx, tc_tx, diff --git a/satrs-example/src/eps/mod.rs b/satrs-example/src/eps/mod.rs index 1b3fe59..9170092 100644 --- a/satrs-example/src/eps/mod.rs +++ b/satrs-example/src/eps/mod.rs @@ -1,9 +1,8 @@ use derive_new::new; -use models::pcdu::{SwitchId, SwitchRequest, SwitchStateBinary}; +use models::pcdu::{SwitchId, SwitchRequest, SwitchState, SwitchStateBinary}; use std::{cell::RefCell, collections::VecDeque, sync::mpsc, time::Duration}; use satrs::{ - power::{PowerSwitchInfo, PowerSwitcherCommandSender, SwitchState}, queue::GenericSendError, request::{GenericMessage, MessageMetadata}, }; @@ -36,10 +35,65 @@ pub enum SwitchInfoError { SwitchSetInvalid, } +impl PowerSwitchHelper { + pub fn send_switch_on_cmd( + &self, + requestor_info: satrs::request::MessageMetadata, + switch_id: SwitchId, + ) -> Result<(), GenericSendError> { + self.switcher_tx.send(GenericMessage::new( + requestor_info, + SwitchRequest::new(switch_id, SwitchStateBinary::On), + ))?; + Ok(()) + } + + pub fn send_switch_off_cmd( + &self, + requestor_info: satrs::request::MessageMetadata, + switch_id: SwitchId, + ) -> Result<(), GenericSendError> { + self.switcher_tx.send(GenericMessage::new( + requestor_info, + SwitchRequest::new(switch_id, SwitchStateBinary::Off), + ))?; + Ok(()) + } + + pub fn switch_state(&self, switch_id: SwitchId) -> Result { + let switch_set = self + .shared_switch_set + .lock() + .expect("failed to lock switch set"); + if !switch_set.valid { + return Err(SwitchInfoError::SwitchSetInvalid); + } + + if let Some(state) = switch_set.switch_map.get(&switch_id) { + return Ok(*state); + } + Err(SwitchInfoError::SwitchIdNotInMap(switch_id)) + } + + fn switch_delay_ms(&self) -> Duration { + // Here, we could set device specific switch delays theoretically. Set it to this value + // for now. + Duration::from_millis(1000) + } + + pub fn is_switch_on(&self, switch_id: SwitchId) -> bool { + if let Ok(state) = self.switch_state(switch_id) { + state == SwitchState::On + } else { + false + } + } +} +/* impl PowerSwitchInfo for PowerSwitchHelper { type Error = SwitchInfoError; - fn switch_state(&self, switch_id: SwitchId) -> Result { + fn switch_state(&self, switch_id: SwitchId) -> Result { let switch_set = self .shared_switch_set .lock() @@ -60,7 +114,9 @@ impl PowerSwitchInfo for PowerSwitchHelper { Duration::from_millis(1000) } } +*/ +/* impl PowerSwitcherCommandSender for PowerSwitchHelper { type Error = SwitchCommandingError; @@ -88,6 +144,7 @@ impl PowerSwitcherCommandSender for PowerSwitchHelper { Ok(()) } } +*/ #[allow(dead_code)] #[derive(new)] @@ -121,6 +178,7 @@ impl Default for TestSwitchHelper { } } +/* impl PowerSwitchInfo for TestSwitchHelper { type Error = SwitchInfoError; @@ -185,6 +243,7 @@ impl PowerSwitcherCommandSender for TestSwitchHelper { Ok(()) } } +*/ #[allow(dead_code)] impl TestSwitchHelper {