Refactor plant state handling and moisture interpretation

- Replaced `read_hardware_state` with `interpret_raw_values` for better abstraction and clarity.
- Enhanced error handling by introducing `NoMessage` and `NotExpectedMessage` states.
- Updated moisture sensor logic to differentiate expected and unexpected messages.
- Renamed and refactored enum fields for consistency (`raw_hz` to `hz`).
- Minor imports and formatting optimizations.
This commit is contained in:
2026-04-16 23:58:23 +02:00
parent 6a71ac4234
commit 2493507304
4 changed files with 51 additions and 64 deletions

View File

@@ -10,14 +10,16 @@ const MOIST_SENSOR_MIN_FREQUENCY: f32 = 150.; // this is really, really dry, thi
#[derive(Debug, PartialEq, Serialize)]
pub enum MoistureSensorError {
NoMessage,
MissingMessage,
NotExpectedMessage { hz: f32 },
ShortCircuit { hz: f32, max: f32 },
OpenLoop { hz: f32, min: f32 },
}
#[derive(Debug, PartialEq, Serialize)]
pub enum MoistureSensorState {
MoistureValue { raw_hz: f32, moisture_percent: f32 },
MoistureValue { hz: f32, moisture_percent: f32 },
NoMessage,
SensorError(MoistureSensorError),
}
@@ -31,7 +33,7 @@ impl MoistureSensorState {
pub fn moisture_percent(&self) -> Option<f32> {
if let MoistureSensorState::MoistureValue {
raw_hz: _,
hz: _,
moisture_percent,
} = self
{
@@ -112,64 +114,47 @@ fn map_range_moisture(
}
impl PlantState {
pub async fn read_hardware_state(
pub async fn interpret_raw_values(
moistures: Moistures,
plant_id: usize,
board: &mut HAL<'_>,
) -> Self {
let sensor_a = {
//if board.board_hal.get_config().plants[plant_id].sensor_a {
let raw = moistures.sensor_a_hz[plant_id];
match raw {
None => MoistureSensorState::SensorError(MoistureSensorError::NoMessage),
Some(raw) => {
match map_range_moisture(
raw,
board.board_hal.get_config().plants[plant_id]
.moisture_sensor_min_frequency
.map(|a| a as f32),
board.board_hal.get_config().plants[plant_id]
.moisture_sensor_max_frequency
.map(|b| b as f32),
) {
Ok(moisture_percent) => MoistureSensorState::MoistureValue {
raw_hz: raw,
moisture_percent,
},
Err(err) => MoistureSensorState::SensorError(err),
}
}
}
}; // else {
// MoistureSensorState::Disabled
//};
let min = board.board_hal.get_config().plants[plant_id].moisture_sensor_min_frequency;
let max = board.board_hal.get_config().plants[plant_id].moisture_sensor_max_frequency;
let sensor_b = {
//if board.board_hal.get_config().plants[plant_id].sensor_b {
let raw = moistures.sensor_b_hz[plant_id];
let raw_to_value = |raw: Option<f32>, expected: bool| -> MoistureSensorState {
match raw {
None => MoistureSensorState::SensorError(MoistureSensorError::NoMessage),
None => {
if expected {
MoistureSensorState::SensorError(MoistureSensorError::MissingMessage)
} else {
MoistureSensorState::NoMessage
}
}
Some(raw) => {
match map_range_moisture(
raw,
board.board_hal.get_config().plants[plant_id]
.moisture_sensor_min_frequency
.map(|a| a as f32),
board.board_hal.get_config().plants[plant_id]
.moisture_sensor_max_frequency
.map(|b| b as f32),
) {
Ok(moisture_percent) => MoistureSensorState::MoistureValue {
raw_hz: raw,
moisture_percent,
},
Err(err) => MoistureSensorState::SensorError(err),
if expected {
match map_range_moisture(raw, min.map(|a| a as f32), max.map(|b| b as f32))
{
Ok(moisture_percent) => MoistureSensorState::MoistureValue {
hz: raw,
moisture_percent,
},
Err(err) => MoistureSensorState::SensorError(err),
}
} else {
MoistureSensorState::SensorError(MoistureSensorError::NotExpectedMessage {
hz: raw,
})
}
}
}
}; // else {
// MoistureSensorState::Disabled
//};
};
let expected_a = board.board_hal.get_config().plants[plant_id].sensor_a;
let expected_b = board.board_hal.get_config().plants[plant_id].sensor_b;
let sensor_a = { raw_to_value(moistures.sensor_a_hz[plant_id], expected_a) };
let sensor_b = { raw_to_value(moistures.sensor_b_hz[plant_id], expected_b) };
let previous_pump = board.board_hal.get_esp().last_pump_time(plant_id);
let consecutive_pump_count = board.board_hal.get_esp().consecutive_pump_count(plant_id);