WIP refactor plant_state

This commit is contained in:
2025-04-18 01:05:12 +02:00
parent cf31ce8d43
commit 2b5c1da484
7 changed files with 263 additions and 409 deletions

View File

@@ -1,8 +1,8 @@
//offer ota and config mode
use crate::{
determine_tank_state, get_version, log::LogMessage, map_range_moisture, plant_hal::PLANT_COUNT,
BOARD_ACCESS,
determine_tank_state, get_version, log::LogMessage, plant_hal::PLANT_COUNT,
plant_state::PlantState, util::LimitPrecision, BOARD_ACCESS,
};
use anyhow::bail;
use chrono::DateTime;
@@ -35,8 +35,8 @@ struct LoadData<'a> {
#[derive(Serialize, Debug)]
struct Moistures {
moisture_a: Vec<u8>,
moisture_b: Vec<u8>,
moisture_a: Vec<Option<f32>>,
moisture_b: Vec<Option<f32>>,
}
#[derive(Deserialize, Debug)]
@@ -81,33 +81,21 @@ fn get_live_moisture(
_request: &mut Request<&mut EspHttpConnection>,
) -> Result<Option<std::string::String>, anyhow::Error> {
let mut board = BOARD_ACCESS.lock().unwrap();
let config = board.get_config().unwrap();
let mut a: Vec<u8> = Vec::new();
let mut b: Vec<u8> = Vec::new();
for plant in 0..8 {
let a_hz = board.measure_moisture_hz(plant, crate::plant_hal::Sensor::A)?;
let b_hz = board.measure_moisture_hz(plant, crate::plant_hal::Sensor::B)?;
let a_pct = map_range_moisture(a_hz as f32);
match a_pct {
Ok(result) => {
a.push(result);
}
Err(_) => {
a.push(200);
}
}
let b_pct = map_range_moisture(b_hz as f32);
match b_pct {
Ok(result) => {
b.push(result);
}
Err(_) => {
b.push(200);
}
}
}
let plant_state = Vec::from_iter(
(0..PLANT_COUNT).map(|i| PlantState::read_hardware_state(i, &mut board, &config.plants[i])),
);
let a = Vec::from_iter(
plant_state
.iter()
.map(|s| s.sensor_a.moisture_percent().map(|f| f.to_precision(2))),
);
let b = Vec::from_iter(
plant_state
.iter()
.map(|s| s.sensor_b.moisture_percent().map(|f| f.to_precision(2))),
);
let data = Moistures {
moisture_a: a,