pub struct RelayManager {
config: RelayManagerConfig,
lock_error_channel_send_refill: bool,
lock_error_channel_receive_refill: bool,
lock_error_channel_send_feed: bool,
lock_error_channel_receive_feed: bool,
lock_error_channel_send_ventilation: bool,
lock_error_channel_receive_ventilation: bool,
lock_error_channel_send_heating: bool,
lock_error_channel_receive_heating: bool,
lock_error_channel_send_balling: bool,
lock_error_channel_receive_balling: bool,
execution_config: ExecutionConfig,
}Expand description
Contains the configuration and the implementation of the relay manager. Thread communication of this component is as follows:
Fields§
§config: RelayManagerConfigconfiguration data for relay manager
lock_error_channel_send_refill: boolinhibition flag to avoid flooding the log file with repeated messages of failure to send to refill control via the channel
lock_error_channel_receive_refill: boolinhibition flag to avoid flooding the log file with repeated messages of failure to receive from refill control via the channel
lock_error_channel_send_feed: boolinhibition flag to avoid flooding the log file with repeated messages of failure to send to feed control via the channel
lock_error_channel_receive_feed: boolinhibition flag to avoid flooding the log file with repeated messages of failure to receive from feed control via the channel
lock_error_channel_send_ventilation: boolinhibition flag to avoid flooding the log file with repeated messages of failure to send to ventilation control via the channel
lock_error_channel_receive_ventilation: boolinhibition flag to avoid flooding the log file with repeated messages of failure to receive from ventilation control via the channel
lock_error_channel_send_heating: boolinhibition flag to avoid flooding the log file with repeated messages of failure to send to heating control via the channel
lock_error_channel_receive_heating: boolinhibition flag to avoid flooding the log file with repeated messages of failure to receive from heating control via the channel
lock_error_channel_send_balling: boolinhibition flag to avoid flooding the log file with repeated messages of failure to send to balling mineral dosing via the channel
lock_error_channel_receive_balling: boolinhibition flag to avoid flooding the log file with repeated messages of failure to receive from balling mineral dosing via the channel
execution_config: ExecutionConfiginformation about which threads have been started
Implementations§
Source§impl RelayManager
impl RelayManager
Sourcepub fn new(
config: RelayManagerConfig,
execution_config: ExecutionConfig,
) -> RelayManager
pub fn new( config: RelayManagerConfig, execution_config: ExecutionConfig, ) -> RelayManager
Creates a new RelayManager instance.
This constructor initializes the relay management module with its configuration.
It sets up various internal lock flags to false, which are used to prevent
repetitive error and warning messages from flooding the log file during operation,
especially concerning channel communication and actuation errors.
§Arguments
config- Configuration data for the relay manager, loaded from a TOML file.
§Returns
A new RelayManager struct, ready to handle relay actuation commands
and communicate with other modules.
Sourcefn actuate_with_retries(
&mut self,
actuator: &mut impl RelayActuationTrait,
command: &InternalCommand,
actuation_name: &str,
spin_sleeper: &mut SpinSleeper,
sleep_duration: Duration,
) -> Result<(), RelayError>
fn actuate_with_retries( &mut self, actuator: &mut impl RelayActuationTrait, command: &InternalCommand, actuation_name: &str, spin_sleeper: &mut SpinSleeper, sleep_duration: Duration, ) -> Result<(), RelayError>
Attempts to actuate a given command, with retries on failure.
This helper function encapsulates the retry logic for actuator commands. It
will attempt the actuation at least once, and if it fails, it will retry
up to the number of times specified by self.config.actuation_retries.
If a failure is due to an IncorrectChecksum, it will attempt to flush the
buffer before retrying.
§Arguments
actuator- A mutable reference to an object implementingRelayActuationTrait.command- TheInternalCommandto be executed.actuation_name- A descriptive name for the action (e.g., “refill pump”) for logging.spin_sleeper- ASpinSleeperto pause between retries.sleep_duration- TheDurationto wait between retry attempts.
§Returns
An empty Result (Ok(())) if the actuation succeeds, possibly after one or more retries.
§Errors
Returns Err(RelayError::ActuationFailed) if all attempts (the initial one plus all
retries) fail. This error is a wrapper that contains detailed context, including
- The name of the actuation that failed.
- The total number of attempts made.
- The specific error from the last attempt, providing the root cause of the final failure.
Sourcepub fn execute(
&mut self,
relay_manager_channels: &mut RelayManagerChannels,
actuator: &mut impl RelayActuationTrait,
)
pub fn execute( &mut self, relay_manager_channels: &mut RelayManagerChannels, actuator: &mut impl RelayActuationTrait, )
Executes the main control loop for the Relay Manager.
This function runs continuously, acting as the central dispatcher for actuation commands
received from various control modules (Refill, Heating, etc.). It uses a helper function,
process_actuation_request, to handle the logic for each module’s channel pair,
keeping the main loop clean and readable.
The loop forwards commands to the provided actuator and handles graceful shutdown
when a Quit command is received from the signal handler. It also manages to send
periodic heartbeats to the hardware if required.
§Arguments
relay_manager_channels- A mutable reference to the struct containing allmpscchannels necessary for communication with other control threads.actuator- A mutable reference to an object implementing theRelayActuationTrait, which handles the actual hardware interaction or simulation.
Sourcefn process_actuation_request(
&mut self,
channels: (&mut AquaReceiver<InternalCommand>, &mut AquaSender<bool>),
locks: (bool, bool),
actuation_name: &str,
actuator: &mut impl RelayActuationTrait,
spin_sleeper: &mut SpinSleeper,
sleep_duration_between_retries: Duration,
) -> (bool, bool)
fn process_actuation_request( &mut self, channels: (&mut AquaReceiver<InternalCommand>, &mut AquaSender<bool>), locks: (bool, bool), actuation_name: &str, actuator: &mut impl RelayActuationTrait, spin_sleeper: &mut SpinSleeper, sleep_duration_between_retries: Duration, ) -> (bool, bool)
Helper function to process an actuation request from a single channel pair.
This function encapsulates the logic for receiving a command, actuating it with retries,
and sending a confirmation response. It is used to de-duplicate the main execute loop.
It takes the current state of the log-suppression flags (lock_receive, lock_send)
by value and returns their updated state in a tuple. This pattern is used to work
around Rust’s borrowing rules, as the flags are fields on RelayManager and cannot
be mutably borrowed multiple times in the execute loop.
§Arguments
rx- The channel for receivingInternalCommands from a control module.tx- The channel for sending a boolean confirmation back to the control module.lock_receive- The current state of the receive-error lock flag.lock_send- The current state of the send error lock flag.actuation_name- A descriptive name for the action (e.g., “refill”) for logging.actuator- A mutable reference to the hardware actuator.spin_sleeper- ASpinSleeperto pause between retries.sleep_duration_between_retries- TheDurationto wait between retry attempts.
§Returns
A tuple (bool, bool) containing the updated state of the lock_receive and
lock_send flags, respectively.
Trait Implementations§
Source§impl ProcessExternalRequestTrait for RelayManager
impl ProcessExternalRequestTrait for RelayManager
Source§fn process_external_request(
&mut self,
rx_from_signal_handler: &mut AquaReceiver<InternalCommand>,
_: Option<&mut AquaReceiver<InternalCommand>>,
) -> (bool, bool, bool)
fn process_external_request( &mut self, rx_from_signal_handler: &mut AquaReceiver<InternalCommand>, _: Option<&mut AquaReceiver<InternalCommand>>, ) -> (bool, bool, bool)
Checks for and processes new commands relevant to the Relay Manager module from external channels.
This is the specialized implementation of ProcessExternalRequestTrait for RelayManager.
It delegates directly to process_external_request_without_messaging, indicating
that the RelayManager (which often interacts with hardware like Controllino)
only processes commands from the signal handler and ignores any input from a messaging channel.
§Arguments
rx_from_signal_handler- A reference to the receiver end of the channel for commands originating from the signal handler._- This parameter is ignored, as theRelayManagerdoes not process messages from a messaging channel.
§Returns
A tuple (bool, bool, bool) indicating the status of commands received:
- The first
boolistrueif aQuitcommand was received; otherwisefalse. - The second
boolis alwaysfalse(no “Start” commands processed). - The third
boolis alwaysfalse(no “Stop” commands processed).