save compiling state during refactor
This commit is contained in:
@@ -8,18 +8,19 @@ use crate::{
|
||||
plant_hal::{self, PLANT_COUNT},
|
||||
};
|
||||
|
||||
const MOIST_SENSOR_MAX_FREQUENCY: u32 = 5500; // 60kHz (500Hz margin)
|
||||
const MOIST_SENSOR_MIN_FREQUENCY: u32 = 150; // this is really really dry, think like cactus levels
|
||||
const MOIST_SENSOR_MAX_FREQUENCY: f32 = 5500.; // 60kHz (500Hz margin)
|
||||
const MOIST_SENSOR_MIN_FREQUENCY: f32 = 150.; // this is really really dry, think like cactus levels
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
pub enum HumiditySensorError {
|
||||
ShortCircuit { hz: f32, max: f32 },
|
||||
OpenLoop { hz: f32, min: f32 },
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
pub enum HumiditySensorState {
|
||||
Disabled,
|
||||
HumidityValue { raw_hz: u32, moisture_percent: f32 },
|
||||
HumidityValue { raw_hz: f32, moisture_percent: f32 },
|
||||
SensorError(HumiditySensorError),
|
||||
BoardError(String),
|
||||
}
|
||||
@@ -35,7 +36,7 @@ impl HumiditySensorState {
|
||||
moisture_percent,
|
||||
} = self
|
||||
{
|
||||
Some(moisture_percent)
|
||||
Some(*moisture_percent)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -44,7 +45,7 @@ impl HumiditySensorState {
|
||||
|
||||
impl HumiditySensorState {}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
pub enum PumpError {
|
||||
PumpNotWorking {
|
||||
failed_attempts: usize,
|
||||
@@ -52,6 +53,7 @@ pub enum PumpError {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct PumpState {
|
||||
consecutive_pump_count: u32,
|
||||
previous_pump: Option<DateTime<Utc>>,
|
||||
@@ -74,7 +76,7 @@ pub struct PlantState {
|
||||
|
||||
fn map_range_moisture(s: f32) -> Result<f32, HumiditySensorError> {
|
||||
if s < MOIST_SENSOR_MIN_FREQUENCY {
|
||||
return Err(HumiditySensorError::OpenCircuit {
|
||||
return Err(HumiditySensorError::OpenLoop {
|
||||
hz: s,
|
||||
min: MOIST_SENSOR_MIN_FREQUENCY,
|
||||
});
|
||||
@@ -85,7 +87,7 @@ fn map_range_moisture(s: f32) -> Result<f32, HumiditySensorError> {
|
||||
max: MOIST_SENSOR_MAX_FREQUENCY,
|
||||
});
|
||||
}
|
||||
let moisture_percent = (s - MOIST_SENSOR_MIN_FREQUENCY) * 100
|
||||
let moisture_percent = (s - MOIST_SENSOR_MIN_FREQUENCY) * 100.0
|
||||
/ (MOIST_SENSOR_MAX_FREQUENCY - MOIST_SENSOR_MIN_FREQUENCY);
|
||||
|
||||
return Ok(moisture_percent);
|
||||
@@ -165,7 +167,7 @@ impl PlantState {
|
||||
PlantWateringMode::TargetMoisture => {
|
||||
let moisture_percent = match (
|
||||
self.sensor_a.moisture_percent(),
|
||||
&self.sensor_b.moisture_percent(),
|
||||
self.sensor_b.moisture_percent(),
|
||||
) {
|
||||
(Some(moisture_a), Some(moisture_b)) => (moisture_a + moisture_b) / 2.,
|
||||
(Some(moisture_percent), _) => moisture_percent,
|
||||
@@ -196,51 +198,51 @@ impl PlantState {
|
||||
}
|
||||
}
|
||||
|
||||
fn update_plant_state(
|
||||
plantstate: &mut [PlantInfo; PLANT_COUNT],
|
||||
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
|
||||
config: &PlantControllerConfig,
|
||||
) {
|
||||
for plant in 0..PLANT_COUNT {
|
||||
let state = &plantstate[plant];
|
||||
let plant_config = &config.plants[plant];
|
||||
|
||||
let mode = format!("{:?}", plant_config.mode);
|
||||
|
||||
let plant_dto = PlantStateMQTT {
|
||||
a: &sensor_to_string(
|
||||
&state.a,
|
||||
&state.sensor_error_a,
|
||||
plant_config.mode != PlantWateringMode::OFF,
|
||||
),
|
||||
a_raw: &state.a_raw.unwrap_or(0).to_string(),
|
||||
b: &sensor_to_string(&state.b, &state.sensor_error_b, plant_config.sensor_b),
|
||||
b_raw: &state.b_raw.unwrap_or(0).to_string(),
|
||||
active: state.active,
|
||||
mode: &mode,
|
||||
last_pump: &time_to_string_utc(board.last_pump_time(plant)),
|
||||
next_pump: &time_to_string(state.next_pump),
|
||||
consecutive_pump_count: state.consecutive_pump_count,
|
||||
cooldown: state.cooldown,
|
||||
dry: state.dry,
|
||||
not_effective: state.not_effective,
|
||||
out_of_work_hour: state.out_of_work_hour,
|
||||
pump_error: state.pump_error,
|
||||
};
|
||||
|
||||
match serde_json::to_string(&plant_dto) {
|
||||
Ok(state) => {
|
||||
let plant_topic = format!("/plant{}", plant + 1);
|
||||
let _ = board.mqtt_publish(&config, &plant_topic, state.as_bytes());
|
||||
//reduce speed as else messages will be dropped
|
||||
Delay::new_default().delay_ms(200);
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error publishing lightstate {}", err);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
//fn update_plant_state(
|
||||
// plantstate: &mut [PlantInfo; PLANT_COUNT],
|
||||
// board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
|
||||
// config: &PlantControllerConfig,
|
||||
//) {
|
||||
// for plant in 0..PLANT_COUNT {
|
||||
// let state = &plantstate[plant];
|
||||
// let plant_config = &config.plants[plant];
|
||||
//
|
||||
// let mode = format!("{:?}", plant_config.mode);
|
||||
//
|
||||
// let plant_dto = PlantStateMQTT {
|
||||
// a: &sensor_to_string(
|
||||
// &state.a,
|
||||
// &state.sensor_error_a,
|
||||
// plant_config.mode != PlantWateringMode::OFF,
|
||||
// ),
|
||||
// a_raw: &state.a_raw.unwrap_or(0).to_string(),
|
||||
// b: &sensor_to_string(&state.b, &state.sensor_error_b, plant_config.sensor_b),
|
||||
// b_raw: &state.b_raw.unwrap_or(0).to_string(),
|
||||
// active: state.active,
|
||||
// mode: &mode,
|
||||
// last_pump: &time_to_string_utc(board.last_pump_time(plant)),
|
||||
// next_pump: &time_to_string(state.next_pump),
|
||||
// consecutive_pump_count: state.consecutive_pump_count,
|
||||
// cooldown: state.cooldown,
|
||||
// dry: state.dry,
|
||||
// not_effective: state.not_effective,
|
||||
// out_of_work_hour: state.out_of_work_hour,
|
||||
// pump_error: state.pump_error,
|
||||
// };
|
||||
//
|
||||
// match serde_json::to_string(&plant_dto) {
|
||||
// Ok(state) => {
|
||||
// let plant_topic = format!("/plant{}", plant + 1);
|
||||
// let _ = board.mqtt_publish(&config, &plant_topic, state.as_bytes());
|
||||
// //reduce speed as else messages will be dropped
|
||||
// Delay::new_default().delay_ms(200);
|
||||
// }
|
||||
// Err(err) => {
|
||||
// println!("Error publishing lightstate {}", err);
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
//}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
/// State of a single plant to be tracked
|
||||
@@ -250,7 +252,7 @@ pub struct PlantInfo {
|
||||
/// state of humidity sensor on bank b
|
||||
sensor_b: HumiditySensorState,
|
||||
/// configured plant watering mode
|
||||
mode: config::PlantWateringMode,
|
||||
mode: PlantWateringMode,
|
||||
/// plant needs to be watered
|
||||
do_water: bool,
|
||||
/// is plant considerd to be dry according to settings
|
||||
|
||||
Reference in New Issue
Block a user