From 110cb50098cfa8134d21ae47f370206cac51d738 Mon Sep 17 00:00:00 2001 From: Empire Date: Thu, 20 Mar 2025 23:35:26 +0100 Subject: [PATCH] also add percent to tank mqtt --- rust/src/tank.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/rust/src/tank.rs b/rust/src/tank.rs index 152fc29..3ace7b9 100644 --- a/rust/src/tank.rs +++ b/rust/src/tank.rs @@ -86,16 +86,21 @@ impl TankState { pub fn warn_level(&self, config: &TankConfig) -> Result { match self { TankState::TankSensorDisabled => Err(TankError::SensorDisabled), - TankState::TankSensorError(err) => match err { - TankError::SensorValueError { value, min, max } => Ok(value < min), - _ => Err(err.clone()), - }, + TankState::TankSensorError(err) => Err(err.clone()), 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.into() { - Ok(true) - } else { - Ok(false) + let tank_fill_percent = raw_voltage_to_tank_fill_percent(*raw_value_mv, config); + match tank_fill_percent { + Ok(value) => { + if value < config.tank_warn_percent.into() { + Ok(true) + } else { + Ok(false) + } + } + Err(err) => match err { + TankError::SensorValueError { value, min, max: _ } => Ok(value < min), + _ => Err(err), + }, } } } @@ -131,6 +136,11 @@ impl TankState { TankState::TankSensorPresent(raw_value_mv) => Some(*raw_value_mv), }; + let percent = match raw { + Some(r) => raw_voltage_to_tank_fill_percent(r, config).ok(), + None => None, + }; + TankInfo { enough_water, warn_level, @@ -142,6 +152,7 @@ impl TankState { .is_ok_and(|temp| *temp < WATER_FROZEN_THRESH), water_temp: water_temp.as_ref().copied().ok(), temp_sensor_error: water_temp.err().map(|err| err.to_string()), + percent, } } } @@ -173,6 +184,8 @@ pub struct TankInfo { sensor_error: Option, /// raw water sensor value raw: Option, + /// percent value + percent: Option, /// water in tank might be frozen water_frozen: bool, /// water temperature