finalize tank state logging
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{config::{PlantControllerConfig, TankConfig}, plant_hal::PlantCtrlBoard};
|
||||
use crate::{
|
||||
config::{PlantControllerConfig, TankConfig},
|
||||
plant_hal::PlantCtrlBoard,
|
||||
};
|
||||
|
||||
const OPEN_TANK_VOLTAGE: f32 = 3.0;
|
||||
pub const WATER_FROZEN_THRESH: f32 = 4.0;
|
||||
@@ -10,7 +13,7 @@ pub enum TankError {
|
||||
SensorDisabled,
|
||||
SensorMissing(f32),
|
||||
SensorValueError { value: f32, min: f32, max: f32 },
|
||||
BoardError(String)
|
||||
BoardError(String),
|
||||
}
|
||||
|
||||
pub enum TankState {
|
||||
@@ -35,18 +38,21 @@ fn raw_voltage_to_tank_fill_percent(
|
||||
config: &TankConfig,
|
||||
) -> Result<f32, TankError> {
|
||||
let divider_percent = raw_volatge_to_divider_percent(raw_value_mv)?;
|
||||
if divider_percent < config.tank_empty_percent.into() || divider_percent > config.tank_full_percent.into() {
|
||||
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.into(),
|
||||
max: config.tank_full_percent.into(),
|
||||
});
|
||||
}
|
||||
Ok((divider_percent - f32::from(config.tank_empty_percent)) * 100.
|
||||
/ f32::from(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),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
impl TankState {
|
||||
pub fn left_ml(&self, config: &TankConfig) -> Result<f32, TankError> {
|
||||
match self {
|
||||
@@ -69,10 +75,14 @@ impl TankState {
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_enabled(&self) -> bool {
|
||||
matches!(self, TankState::TankSensorDisabled)
|
||||
}
|
||||
|
||||
pub fn warn_level(&self, config: &TankConfig) -> Result<bool, TankError> {
|
||||
match self {
|
||||
TankState::TankSensorDisabled => Err(TankError::SensorDisabled),
|
||||
@@ -84,29 +94,37 @@ impl TankState {
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn got_error(&self, config: &TankConfig) -> Option<TankError> {
|
||||
match self {
|
||||
TankState::TankSensorPresent(raw_value_mv) => raw_voltage_to_tank_fill_percent(*raw_value_mv, config).err(),
|
||||
TankState::TankSensorPresent(raw_value_mv) => {
|
||||
raw_voltage_to_tank_fill_percent(*raw_value_mv, config).err()
|
||||
}
|
||||
TankState::TankSensorError(err) => Some(err.clone()),
|
||||
TankState::TankSensorDisabled => Some(TankError::SensorDisabled),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_mqtt_info(&self, config: &TankConfig, water_temp: Result<f32, anyhow::Error>) -> TankInfo {
|
||||
pub fn as_mqtt_info(
|
||||
&self,
|
||||
config: &TankConfig,
|
||||
water_temp: Result<f32, anyhow::Error>,
|
||||
) -> TankInfo {
|
||||
let mut tank_err: Option<TankError> = None;
|
||||
let left_ml = match self.left_ml(config) {
|
||||
Err(err) => { tank_err = Some(err); None },
|
||||
Err(err) => {
|
||||
tank_err = Some(err);
|
||||
None
|
||||
}
|
||||
Ok(left_ml) => Some(left_ml),
|
||||
};
|
||||
let enough_water = self.enough_water(config).unwrap_or(false); //NOTE: is this correct if there is an error assume not enough water?
|
||||
let warn_level = self.warn_level(config).unwrap_or(false); //NOTE: should no warn level be triggered if there is an error?
|
||||
let raw = match self {
|
||||
TankState::TankSensorDisabled
|
||||
| TankState::TankSensorError(_) => None,
|
||||
TankState::TankSensorDisabled | TankState::TankSensorError(_) => None,
|
||||
TankState::TankSensorPresent(raw_value_mv) => Some(*raw_value_mv),
|
||||
};
|
||||
|
||||
@@ -116,9 +134,11 @@ impl TankState {
|
||||
left_ml,
|
||||
sensor_error: tank_err,
|
||||
raw,
|
||||
water_frozen: water_temp.as_ref().is_ok_and(|temp| *temp < WATER_FROZEN_THRESH),
|
||||
water_frozen: water_temp
|
||||
.as_ref()
|
||||
.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())
|
||||
temp_sensor_error: water_temp.err().map(|err| err.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,9 +150,8 @@ pub fn determine_tank_state(
|
||||
if config.tank.tank_sensor_enabled {
|
||||
match board.tank_sensor_voltage() {
|
||||
Ok(raw_sensor_value_mv) => TankState::TankSensorPresent(raw_sensor_value_mv),
|
||||
Err(err) => TankState::TankSensorError(TankError::BoardError(err.to_string()))
|
||||
Err(err) => TankState::TankSensorError(TankError::BoardError(err.to_string())),
|
||||
}
|
||||
|
||||
} else {
|
||||
TankState::TankSensorDisabled
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user