pub struct Feed {
config: FeedConfig,
pub execute_command_received: bool,
pub profile_id_requested: i32,
pub execution_blocked: bool,
lock_error_get_feedschedule_entry: bool,
pub last_ping_instant: Instant,
pub database_ping_interval: Duration,
pub lock_warn_inapplicable_command_signal_handler: bool,
pub lock_error_channel_receive_termination: bool,
}Expand description
Contains the configuration and the implementation for the feed control. Thread communication is as follows:
Fields§
§config: FeedConfigConfiguration data for Feed control
execute_command_received: boolCommunication from trait implementation: request to execute a certain feed profile has been received
profile_id_requested: i32Communication from trait implementation: id of feed profile to be executed requested externally
execution_blocked: boolInhibition flag for further execution of feed profiles. It is set whenever errors occur during feeding and can be reset by external command.
lock_error_get_feedschedule_entry: boolInhibition flag to avoid flooding the log file with repeated messages about failure to read feed schedule entry from the database
last_ping_instant: Instantrecording when the last database ping happened
database_ping_interval: Durationdatabase ping interval
lock_warn_inapplicable_command_signal_handler: boolInhibition flag to avoid flooding the log file with repeated messages about having received inapplicable command
lock_error_channel_receive_termination: boolInhibition flag to avoid flooding the log file with repeated messages about failure to receive termination signal via the channel
Implementations§
Source§impl Feed
impl Feed
Sourcepub fn new(config: FeedConfig, database_ping_interval: Duration) -> Feed
pub fn new(config: FeedConfig, database_ping_interval: Duration) -> Feed
Creates a new Feed control instance.
This constructor initializes the feed control module with its configuration and a database interface for managing feed-related data. It also sets initial states for command reception, execution blocking, and internal logging inhibition flags.
§Arguments
config- Configuration data for the feed control, loaded from a TOML file.database_ping_interval- ADurationinstance, providing the interval to ping the database.
§Returns
A new Feed struct, ready for operation within the application loop.
Sourcefn execute_feed(
&mut self,
profile_id: i32,
food_injection: &mut impl FoodInjectionTrait,
feed_channels: &mut FeedChannels,
sql_interface_feed: &mut Box<dyn DatabaseInterfaceFeedTrait + Sync + Send>,
) -> (Result<(), ()>, bool)
fn execute_feed( &mut self, profile_id: i32, food_injection: &mut impl FoodInjectionTrait, feed_channels: &mut FeedChannels, sql_interface_feed: &mut Box<dyn DatabaseInterfaceFeedTrait + Sync + Send>, ) -> (Result<(), ()>, bool)
Loads a specified feed profile from the database, executes it via food injection, and logs the event.
This private helper function orchestrates the process of executing a single feed profile.
It first attempts to retrieve the full Feedpattern from the database. If successful,
it then calls the food_injection trait method to actuate the feeder according to the
pattern. Finally, it records the completed feed event in the database.
The function also monitors for a Quit command from the signal handler during the
food injection process, indicating an early termination request.
§Arguments
profile_id- The unique identifier of the feed profile to be loaded and executed.food_injection- A mutable reference to an object implementing theFoodInjectionTrait, responsible for the physical actuation of the feeder.feed_channels- A mutable reference to the struct containing the channels.sql_interface_feed- A boxed trait object representing the interface to the SQL database for feed-specific operations. This allows for dependency injection and mock implementations during testing.
§Returns
A tuple (Result<(), ()>, bool) where:
- The first element is the overall result of the operation. It is
Ok(())only if the food injection and the database logging both complete without error. - The second element is a
bool,which istrueif aQuitcommand was received from the signal handler during theinject_foodprocess,falseotherwise. This value is independent of the success or failure of the operation itself.
§Errors
This function does not return a distinct error type. Instead, it returns Err(())
in the result tuple to signify failure. The function can fail if:
- It cannot read the specified feed pattern from the database.
- The
food_injection.inject_foodmethod reports an error. - It cannot retrieve the current timestamp from the database after injection.
- It fails to insert the feed event log into the database.
In all failure cases, a detailed error message is logged to the console.
Sourcefn select_feed_schedule_entry_for_execution(
&self,
feedschedule_entries: &[FeedScheduleEntry],
) -> Option<FeedScheduleEntry>
fn select_feed_schedule_entry_for_execution( &self, feedschedule_entries: &[FeedScheduleEntry], ) -> Option<FeedScheduleEntry>
Selects a specific feed schedule entry for execution based on the configured strategy.
This function is used when multiple FeedScheduleEntry items are found to be
in the past (i.e., overdue for execution). It applies the strategy_multiple_schedule_entries_in_past
from the FeedConfig to determine which single entry, if any, should be executed.
The strategy is defined as follows:
- Negative value: No entry will be selected for execution.
- Positive value (N): The N-th entry from the
feedschedule_entriesvector (0-indexed) will be selected.- If
Nis greater than or equal to the number of available entries, the last entry in the vector is selected.
- If
§Arguments
feedschedule_entries- A slice ofFeedScheduleEntrystructs, assumed to be ordered.
§Returns
An Option<FeedScheduleEntry>:
Some(FeedScheduleEntry): The selected feed schedule entry to be executed.None: If no entry is selected based on the strategy (e.g., negative strategy value, or an emptyfeedschedule_entriesvector).
Sourcefn execute_profile_update_database(
&mut self,
profile_id: i32,
food_injection: &mut impl FoodInjectionTrait,
feed_channels: &mut FeedChannels,
feed_schedule_entries: &mut Vec<FeedScheduleEntry>,
sql_interface_feed: &mut Box<dyn DatabaseInterfaceFeedTrait + Sync + Send>,
) -> bool
fn execute_profile_update_database( &mut self, profile_id: i32, food_injection: &mut impl FoodInjectionTrait, feed_channels: &mut FeedChannels, feed_schedule_entries: &mut Vec<FeedScheduleEntry>, sql_interface_feed: &mut Box<dyn DatabaseInterfaceFeedTrait + Sync + Send>, ) -> bool
Executes a specified feed profile and updates the feed schedule in the database.
This function is a core part of the feed control logic. It first calls execute_feed
to perform the actual food injection and log the event. Based on the outcome of
execute_feed, it updates the internal execution_blocked flag if an error occurs.
Finally, it triggers the update or deletion of the corresponding entries in the
feed schedule database.
§Arguments
profile_id- The numeric identifier of the feed profile to be executed.food_injection- A mutable reference to an object implementingFoodInjectionTrait, responsible for physical feeder actuation.feed_channels- A mutable reference to the structFoodInjectionTraitcontaining the channels.rx_feed_from_relay_manager- The receiver channel for receiving acknowledgments from the relay manager.rx_feed_from_signal_handler- The receiver channel for listening to signals (e.g.,Quit) from the signal handler.feed_schedule_entries- A mutable reference to the vector ofFeedScheduleEntrythat includes the entry(ies) just executed, which will be updated/deleted in the database.sql_interface_feed- A boxed trait object representing the interface to the SQL database for feed-specific operations. This allows for dependency injection and mock implementations during testing.
§Returns
A bool which is true if a Quit command was received from the signal handler
during the feed execution, indicating that the application should shut down; otherwise false.
Sourcepub fn execute(
&mut self,
mutex_device_scheduler_feed: Arc<Mutex<i32>>,
feed_channels: &mut FeedChannels,
food_injection: &mut impl FoodInjectionTrait,
sql_interface_feed: Box<dyn DatabaseInterfaceFeedTrait + Sync + Send>,
)
pub fn execute( &mut self, mutex_device_scheduler_feed: Arc<Mutex<i32>>, feed_channels: &mut FeedChannels, food_injection: &mut impl FoodInjectionTrait, sql_interface_feed: Box<dyn DatabaseInterfaceFeedTrait + Sync + Send>, )
Executes the main control loop for the feed module.
This function runs continuously, managing the automatic execution of feed profiles
based on a schedule, processing external commands, and ensuring graceful shutdown.
It periodically checks the database for overdue feed schedule entries
and executes selected profiles, while also handling Start, Stop, and Execute
commands received from external channels.
The loop breaks when a Quit command is received from the signal handler.
After breaking, it sends a confirmation back to the signal handler and then
waits for a Terminate command to ensure a complete shutdown.
§Arguments
mutex_device_scheduler_feed- AnArc<Mutex<i32>>used for coordinating access to device scheduling, preventing parallel actuation across different control modules. It holds a counter of the completed actuation.feed_channels- A mutable reference toFeedChannelsstruct containing all necessarympscsender and receiver channels for inter-thread communication (e.g., with the signal handler, relay manager, and messaging).food_injection- A mutable reference to an object implementing theFoodInjectionTrait, responsible for the physical dispensing of food.sql_interface_feed- A boxed trait object representing the interface to the SQL database for feed-specific operations. This allows for dependency injection and mock implementations during testing.
Trait Implementations§
Source§impl DatabasePingTrait for Feed
impl DatabasePingTrait for Feed
Source§fn get_ping_interval(&self) -> Duration
fn get_ping_interval(&self) -> Duration
Source§fn get_last_ping_instant(&self) -> Instant
fn get_last_ping_instant(&self) -> Instant
Source§fn update_last_ping_instant(&mut self)
fn update_last_ping_instant(&mut self)
Source§fn check_timing_and_ping_database(
&mut self,
database_interface: &mut (impl Pingable + ?Sized),
)
fn check_timing_and_ping_database( &mut self, database_interface: &mut (impl Pingable + ?Sized), )
Source§impl ProcessExternalRequestTrait for Feed
impl ProcessExternalRequestTrait for Feed
Source§fn process_external_request(
&mut self,
rx_from_signal_handler: &mut AquaReceiver<InternalCommand>,
rx_from_messaging_opt: Option<&mut AquaReceiver<InternalCommand>>,
) -> (bool, bool, bool)
fn process_external_request( &mut self, rx_from_signal_handler: &mut AquaReceiver<InternalCommand>, rx_from_messaging_opt: Option<&mut AquaReceiver<InternalCommand>>, ) -> (bool, bool, bool)
Checks for and processes new commands relevant to the Feed control module from external channels.
This is the specialized implementation of ProcessExternalRequestTrait for the Feed module.
It handles standard “Terminate” or “Quit” commands from the signal handler.
From the messaging channel, it specifically processes “Start,” “Stop,” “ResetAllErrors,”
and “Execute” commands, updating internal state variables based on these commands.
Inapplicable commands are ignored with warnings.
§Arguments
rx_from_signal_handler- A reference to the receiver end of the channel for commands originating from the signal handler.rx_from_messaging_opt- AnOptioncontaining a reference to the receiver end of the channel for commands from the messaging system. This channel is optional.
§Returns
A tuple (bool, bool, bool) indicating the status of specific commands received:
- The first
boolistrueif aQuitorTerminatecommand was received from the signal handler; otherwisefalse. - The second
boolistrueif aStartcommand was received from messaging; otherwisefalse. - The third
boolistrueif aStopcommand was received from messaging; otherwisefalse.
Source§impl WaitForTerminationTrait for Feed
impl WaitForTerminationTrait for Feed
Source§fn get_warn_lock_mut(&mut self) -> &mut bool
fn get_warn_lock_mut(&mut self) -> &mut bool
Method connects the default trait implementation with the specific implementation accessing the warn-lock.
Source§fn get_error_lock_mut(&mut self) -> &mut bool
fn get_error_lock_mut(&mut self) -> &mut bool
Method connects the default trait implementation with the specific implementation for accessing the error-lock.