Expand description
Contains the main functionality for the Balling mineral dosing Implements the main control logic for automated Balling mineral dosing.
This module contains the Balling struct, which runs as a dedicated thread to manage
the scheduled dosing of up to four different mineral solutions. It is responsible for
maintaining a precise dosing schedule, responding to external commands, and ensuring
that dosing operations are performed safely and reliably.
§Key Components
-
BallingStruct: The central state machine for the dosing subsystem. It holds the configuration and the internal state for each pump, most importantly thecountdown_dosing_pumpXfields which track the time until the next scheduled dose. -
new()Constructor: A complex and critical initialization function. It performs several pre-flight checks:- Validates that all configured dosing intervals are logical and non-zero.
- Ensures that every pump marked as
activein the configuration has a corresponding entry in the database. - Calculates the initial countdown for each pump by querying the database for the time of its last dosing event.
-
execute()Method: The main thread loop. It continuously performs the following actions:- Periodically decrements the countdown timers for each active pump.
- When a countdown reaches zero, it requests a
schedule_checkto ensure dosing is permitted at the current time. - If permitted, it calls
execute_dosingto perform the physical action. - Resets the pump’s countdown to its configured interval.
- Listens for and processes external commands like
Start,Stop, andExecute(pump_id).
-
execute_dosing()Method: A private helper that orchestrates a single dosing event. It retrieves pump parameters from the database, calculates the required pump run time, invokes theMineralInjectionTraitto actuate the pump, and logs the completed event back to the database.
§Design and Architecture
The Balling module is designed as a robust, decoupled, and testable component.
-
Time-Based Scheduling: The core scheduling logic is based on simple countdown timers. This is a robust and low-overhead way to manage recurring events without relying on a complex external scheduler.
-
Dependency Injection: The module relies on a trait for its core dependency:
MineralInjectionTrait: An abstraction for the physical act of dispensing a mineral solution. This allows for mock implementations in tests.
-
Concurrency Control: It uses an
Arc<Mutex<i32>>to coordinate with other device-actuating modules (likeFeed). This mutex acts as semaphore, ensuring that only one major physical action occurs at a time across the entire system, preventing conflicts. -
State Management: The
dosing_inhibitedflag allows external commands (Start/Stop) to temporarily pause all scheduled dosing without stopping the thread or losing the current countdown states.
Structs§
- Balling
- Contains the configuration and the implementation for the Balling dosing control. Thread communication of this component is as follows: