diff --git a/rust/src/tank.rs b/rust/src/tank.rs index 05f0374..f99b6d0 100644 --- a/rust/src/tank.rs +++ b/rust/src/tank.rs @@ -1,4 +1,4 @@ -use crate::config::TankConfig; +use crate::config::{self, PlantControllerConfig, TankConfig}; const OPEN_TANK_VOLTAGE: f32 = 3.0; @@ -68,15 +68,59 @@ fn raw_voltage_to_tank_fill_percent( impl TankState { - pub fn left_ml(&self, config: &TankConfig) -> Result { + pub fn left_ml(&self, config: &TankConfig) -> Result { match self { - TankState::TankSensorDisabled => Err(TankError::SensorMissing), + TankState::TankSensorDisabled => Err(TankError::SensorDisabled), TankState::TankSensorPresent(raw_value_mv) => { - let tank_fill_percent = raw_voltage_to_tank_fill_percent(raw_value_mv, config); - todo!() + let tank_fill_percent = raw_voltage_to_tank_fill_percent(raw_value_mv, config)?; + //TODO(judge) move logging to more sensible place + //println!( + //"Tank sensor returned mv {} as {}% leaving {} ml useable", + //rv.raw, percent as u8, rv.left_ml + //); + Ok(config.tank_useable_ml as f32 * tank_fill_percent / 100.) } } } + pub fn enough_water(&self, config: &TankConfig) -> Result { + match self { + TankState::TankSensorDisabled => Err(TankError::SensorDisabled), + TankState::TankSensorPresent(raw_value_mv) => { + let tank_fill_percent = raw_voltage_to_tank_fill_percent(raw_value_mv, config)?; + if tank_fill_percent > config.tank_empty_percent { + //TODO(judge) move logging to more sensible place + //println!( + //"Enough water, current percent is {}, minimum empty level is {}", + //percent as u8, config.tank.tank_empty_percent + //); + Ok(true) + } else { + Ok(false) + } + }, + } + } + + pub fn warn_level(&self, config: &TankConfig) -> Result { + match self { + TankState::TankSensorDisabled => Err(TankError::SensorDisabled), + TankState::TankSensorPresent(raw_value_mv) => { + let tank_fill_percent = raw_voltage_to_tank_fill_percent(raw_value_mv, config)?; + if tank_fill_percent < config.tank_warn_percent { + //TODO(judge) move logging to more sensible place + //println!( + //"Low water, current percent is {}, minimum warn level is {}", + //percent as u8, config.tank.tank_warn_percent + //); + // TODO(judge) move board fault setting + // board.general_fault(true); + Ok(true) + } else { + Ok(false) + } + }, + } + } } #[derive(Serialize)] @@ -92,61 +136,11 @@ pub struct TankStateMQTT { pub fn determine_tank_state( board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>, config: &PlantControllerConfig, -) -> TankInfo { +) -> TankState { if config.tank.tank_sensor_enabled { - let mut rv: TankInfo = TankInfo { - ..Default::default() - }; - let success = board - .tank_sensor_percent() - .and_then(|raw| { - rv.raw = raw; - return map_range( - ( - config.tank.tank_empty_percent as f32, - config.tank.tank_full_percent as f32, - ), - raw as f32, - ); - }) - .and_then(|percent| { - rv.left_ml = ((percent * config.tank.tank_useable_ml as f32) / 100_f32) as u32; - println!( - "Tank sensor returned mv {} as {}% leaving {} ml useable", - rv.raw, percent as u8, rv.left_ml - ); - if config.tank.tank_warn_percent > percent as u8 { - board.general_fault(true); - println!( - "Low water, current percent is {}, minimum warn level is {}", - percent as u8, config.tank.tank_warn_percent - ); - rv.warn_level = true; - } - if config.tank.tank_empty_percent < percent as u8 { - println!( - "Enough water, current percent is {}, minimum empty level is {}", - percent as u8, config.tank.tank_empty_percent - ); - rv.enough_water = true; - } - return Ok(()); - }); - match success { - Err(err) => { - println!("Could not determine tank value due to {}", err); - board.general_fault(true); - rv.sensor_error = true; - } - Ok(_) => {} - } - return rv; + let raw_sensor_value_mv = board.tank_sensor_voltage(); + TankState::TankSensorPresent(raw_sensor_value_mv) + } else { + TankState::TankSensorDisabled } - return TankInfo { - warn_level: false, - enough_water: true, - left_ml: 1337, - sensor_error: false, - raw: 0, - }; }