From b993f2b0372235e344dc74cc4abc1a4200517cb0 Mon Sep 17 00:00:00 2001 From: ju6ge Date: Sat, 15 Mar 2025 01:13:48 +0100 Subject: [PATCH] use proper error handling --- rust/src/main.rs | 49 ++++++++++++++++++++++++++++++++---------------- rust/src/tank.rs | 6 ++++++ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index 7293148..db1e76f 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -611,7 +611,7 @@ fn determine_state_target_moisture_for_plant( plant: usize, state: &mut PlantState, config: &PlantControllerConfig, - tank_state: &TankInfo, + tank_state: &TankState, cur: DateTime, ) { let plant_config = &config.plants[plant]; @@ -657,10 +657,17 @@ fn determine_state_target_moisture_for_plant( if a_low || b_low { state.dry = true; - if tank_state.sensor_error && !config.tank.tank_allow_pumping_if_sensor_error { - //ignore is ok - } else if !tank_state.enough_water { - state.no_water = true; + match tank_state.enough_water(&config.tank) { + Err(_tank_err) => { + if !config.tank.tank_allow_pumping_if_sensor_error { + // ignore is ok + // wtf does this meen, shouldn't something happen if the configuration specifies + // that no water should flow if there was an error? + } + }, + Ok(enough_water) => { + state.no_water = !enough_water + }, } } let duration = TimeDelta::try_minutes(plant_config.pump_cooldown_min as i64).unwrap(); @@ -701,7 +708,7 @@ fn determine_state_timer_only_for_plant( plant: usize, state: &mut PlantState, config: &PlantControllerConfig, - tank_state: &TankInfo, + tank_state: &TankState, cur: DateTime, ) { let plant_config = &config.plants[plant]; @@ -716,10 +723,15 @@ fn determine_state_timer_only_for_plant( state.next_pump = Some(europe_time); state.cooldown = true; } else { - if tank_state.sensor_error && !config.tank.tank_allow_pumping_if_sensor_error { - state.do_water = true; - } else if !tank_state.enough_water { - state.no_water = true; + match tank_state.enough_water(&config.tank) { + Err(_tank_err) => { + if !config.tank.tank_allow_pumping_if_sensor_error { + state.do_water = true; + } + }, + Ok(enough_water) => { + state.no_water = !enough_water; + }, } } } @@ -739,7 +751,7 @@ fn determine_state_timer_and_deadzone_for_plant( plant: usize, state: &mut PlantState, config: &PlantControllerConfig, - tank_state: &TankInfo, + tank_state: &TankState, cur: DateTime, ) { let plant_config = &config.plants[plant]; @@ -762,10 +774,15 @@ fn determine_state_timer_and_deadzone_for_plant( state.out_of_work_hour = true; } if !state.cooldown && !state.out_of_work_hour { - if tank_state.sensor_error && !config.tank.tank_allow_pumping_if_sensor_error { - state.do_water = true; - } else if !tank_state.enough_water { - state.no_water = true; + match tank_state.enough_water(&config.tank) { + Err(_tank_err) => { + if !config.tank.tank_allow_pumping_if_sensor_error { + state.do_water = true; + } + }, + Ok(enough_water) => { + state.no_water = !enough_water; + }, } } } @@ -783,7 +800,7 @@ fn determine_state_timer_and_deadzone_for_plant( fn determine_plant_state( plantstate: &mut [PlantState; PLANT_COUNT], cur: DateTime, - tank_state: &TankInfo, + tank_state: &TankState, config: &PlantControllerConfig, board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>, ) { diff --git a/rust/src/tank.rs b/rust/src/tank.rs index c09baa4..3a7e223 100644 --- a/rust/src/tank.rs +++ b/rust/src/tank.rs @@ -123,6 +123,12 @@ impl TankState { } } + pub fn got_error(&self, config: &TankConfig) -> Option { + match self { + TankState::TankSensorPresent(raw_value_mv) => raw_voltage_to_tank_fill_percent(*raw_value_mv, config).err(), + TankState::TankSensorDisabled => Some(TankError::SensorDisabled), + } + } }