From 48b0777d9460bf66435c457c060fa2f2713a2d0b Mon Sep 17 00:00:00 2001 From: ju6ge Date: Fri, 14 Mar 2025 23:39:46 +0100 Subject: [PATCH] fix tank.rs errors after intial implementation --- rust/src/plant_hal.rs | 2 +- rust/src/tank.rs | 41 ++++++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/rust/src/plant_hal.rs b/rust/src/plant_hal.rs index a65c91d..7bf736e 100644 --- a/rust/src/plant_hal.rs +++ b/rust/src/plant_hal.rs @@ -472,7 +472,7 @@ impl PlantCtrlBoard<'_> { } /// return median tank sensor value in milli volt - pub fn tank_sensor_voltage(&mut self) -> Result { + pub fn tank_sensor_voltage(&mut self) -> Result { let delay = Delay::new_default(); self.tank_power.set_high()?; //let stabilize diff --git a/rust/src/tank.rs b/rust/src/tank.rs index f99b6d0..c09baa4 100644 --- a/rust/src/tank.rs +++ b/rust/src/tank.rs @@ -1,4 +1,4 @@ -use crate::config::{self, PlantControllerConfig, TankConfig}; +use crate::{config::{self, PlantControllerConfig, TankConfig}, plant_hal::PlantCtrlBoard}; const OPEN_TANK_VOLTAGE: f32 = 3.0; @@ -24,14 +24,15 @@ pub enum TankError { SensorDisabled, SensorMissing(f32), SensorValueError { value: f32, min: f32, max: f32 }, + BoardError(anyhow::Error) } pub enum TankState { - TankSensorPresent(u16), + TankSensorPresent(f32), TankSensorDisabled, } -fn raw_volatge_to_divider_percent(raw_value_mv: u16) -> Result { +fn raw_volatge_to_divider_percent(raw_value_mv: f32) -> Result { if raw_value_mv > OPEN_TANK_VOLTAGE { return Err(TankError::SensorMissing(raw_value_mv)); } @@ -51,19 +52,19 @@ fn raw_volatge_to_divider_percent(raw_value_mv: u16) -> Result { } fn raw_voltage_to_tank_fill_percent( - raw_value_mv: u16, + raw_value_mv: f32, config: &TankConfig, ) -> Result { let divider_percent = raw_volatge_to_divider_percent(raw_value_mv)?; - if s < config.tank_empty_percent || s > config.tank_full_percent { + if divider_percent < config.tank_empty_percent.into() || divider_percent > config.tank_full_percent.into() { return Err(TankError::SensorValueError { value: divider_percent, - min: config.tank_empty_percent, - max: config.tank_full_percent, + min: config.tank_empty_percent.into(), + max: config.tank_full_percent.into(), }); } - Ok((divider_percent - config.tank_empty_percent) * 100 - / (config.tank_full_percent - config.tank_empty_percent)) + Ok((divider_percent - f32::from(config.tank_empty_percent)) * 100. + / f32::from(config.tank_full_percent - config.tank_empty_percent)) } @@ -72,7 +73,7 @@ impl TankState { 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)?; + 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", @@ -86,8 +87,8 @@ impl TankState { 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 { + let tank_fill_percent = raw_voltage_to_tank_fill_percent(*raw_value_mv, config)?; + if tank_fill_percent > config.tank_empty_percent.into() { //TODO(judge) move logging to more sensible place //println!( //"Enough water, current percent is {}, minimum empty level is {}", @@ -105,8 +106,8 @@ impl TankState { 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 { + let tank_fill_percent = raw_voltage_to_tank_fill_percent(*raw_value_mv, config)?; + if tank_fill_percent < config.tank_warn_percent.into() { //TODO(judge) move logging to more sensible place //println!( //"Low water, current percent is {}, minimum warn level is {}", @@ -121,9 +122,11 @@ impl TankState { }, } } + + } -#[derive(Serialize)] +#[derive(serde::Serialize)] pub struct TankStateMQTT { enough_water: bool, warn_level: bool, @@ -136,11 +139,11 @@ pub struct TankStateMQTT { pub fn determine_tank_state( board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>, config: &PlantControllerConfig, -) -> TankState { +) -> Result { if config.tank.tank_sensor_enabled { - let raw_sensor_value_mv = board.tank_sensor_voltage(); - TankState::TankSensorPresent(raw_sensor_value_mv) + let raw_sensor_value_mv = board.tank_sensor_voltage().map_err(|err| TankError::BoardError(err))?; + Ok(TankState::TankSensorPresent(raw_sensor_value_mv)) } else { - TankState::TankSensorDisabled + Ok(TankState::TankSensorDisabled) } }