fix palntstate not correctly calculated

This commit is contained in:
2024-03-09 15:21:52 +01:00
parent 869a581242
commit b4ad668620
2 changed files with 28 additions and 36 deletions

View File

@@ -61,11 +61,12 @@ struct LightState {
is_day: bool,
}
#[derive(Copy, Clone, Debug, PartialEq, Default)]
#[derive(Clone, Copy, Debug, PartialEq, Default)]
struct PlantState {
a: Option<u8>,
b: Option<u8>,
p: Option<u8>,
consecutive_pump_count: u32,
after_p: Option<u8>,
do_water: bool,
frozen: bool,
@@ -368,7 +369,6 @@ fn safe_main() -> anyhow::Result<()> {
..Default::default()
}; PLANT_COUNT];
let plant_to_pump = determine_next_plant(
online_mode,
&mut plantstate,
europe_time,
&tank_state,
@@ -386,9 +386,14 @@ fn safe_main() -> anyhow::Result<()> {
let mut did_pump = false;
match plant_to_pump {
Some(plant) => {
let mut state = plantstate[plant];
let consecutive_pump_count = board.consecutive_pump_count(plant) + 1;
board.store_consecutive_pump_count(plant, consecutive_pump_count);
let state = &mut plantstate[plant];
state.consecutive_pump_count = board.consecutive_pump_count(plant) + 1;
board.store_consecutive_pump_count(plant, state.consecutive_pump_count);
if state.consecutive_pump_count > config.max_consecutive_pump_count.into() {
state.not_effective = true;
board.fault(plant, true);
}
let plant_config = config.plants[plant];
if plant_config.sensor_p {
@@ -884,7 +889,6 @@ fn determine_state_target_moisture_for_plant(
}
fn determine_next_plant(
online_mode: OnlineMode,
plantstate: &mut [PlantState; PLANT_COUNT],
cur: DateTime<Tz>,
tank_state: &TankState,
@@ -954,12 +958,8 @@ fn determine_next_plant(
{
board.fault(plant, true);
}
if state.do_water {
if board.consecutive_pump_count(plant) > config.max_consecutive_pump_count.into() {
state.not_effective = true;
board.fault(plant, true);
}
} else {
if !state.dry {
state.consecutive_pump_count = 0;
board.store_consecutive_pump_count(plant, 0);
}
println!("Plant {} state is {:?}", plant, state);
@@ -1032,7 +1032,11 @@ fn update_plant_state(
);
}
}
let _ = board.mqtt_publish(
&config,
format!("/plant{}/active", plant + 1).as_str(),
state.active.to_string().as_bytes(),
);
let _ = board.mqtt_publish(
&config,
format!("/plant{}/Sensor A", plant + 1).as_str(),
@@ -1116,6 +1120,13 @@ fn update_plant_state(
format!("/plant{}/Out of Work Hour", plant + 1).as_str(),
state.out_of_work_hour.to_string().as_bytes(),
);
let _ = board.mqtt_publish(
&config,
format!("/plant{}/consecutive pump count", plant + 1).as_str(),
state.consecutive_pump_count.to_string().as_bytes(),
);
}
}