timer was fucked

This commit is contained in:
Empire Phoenix 2024-07-19 16:27:50 +02:00
parent 4d92e0c2a6
commit 863bc3eee2

View File

@ -429,6 +429,10 @@ fn safe_main() -> anyhow::Result<()> {
match plant_to_pump { match plant_to_pump {
Some(plant) => { Some(plant) => {
let state = &mut plantstate[plant]; let state = &mut plantstate[plant];
let plant_config = config.plants[plant];
if plant_config.mode == Mode::TargetMoisture {
state.consecutive_pump_count = board.consecutive_pump_count(plant) + 1; state.consecutive_pump_count = board.consecutive_pump_count(plant) + 1;
board.store_consecutive_pump_count(plant, state.consecutive_pump_count); board.store_consecutive_pump_count(plant, state.consecutive_pump_count);
if state.consecutive_pump_count > config.max_consecutive_pump_count.into() { if state.consecutive_pump_count > config.max_consecutive_pump_count.into() {
@ -436,20 +440,7 @@ fn safe_main() -> anyhow::Result<()> {
board.fault(plant, true); board.fault(plant, true);
} }
let plant_config = config.plants[plant];
if plant_config.sensor_p {
match map_range_moisture(
board.measure_moisture_hz(plant, plant_hal::Sensor::PUMP)? as f32,
) {
Ok(p) => state.p = Some(p),
Err(err) => {
board.fault(plant, true);
state.sensor_error_p = Some(err);
} }
}
}
println!( println!(
"Trying to pump for {}s with pump {} now", "Trying to pump for {}s with pump {} now",
plant_config.pump_time_s, plant plant_config.pump_time_s, plant
@ -463,48 +454,11 @@ fn safe_main() -> anyhow::Result<()> {
state.active = true; state.active = true;
for _ in 0..plant_config.pump_time_s { for _ in 0..plant_config.pump_time_s {
unsafe { vTaskDelay(CONFIG_FREERTOS_HZ) }; unsafe { vTaskDelay(CONFIG_FREERTOS_HZ) };
let p_live_topic = format!("/plant{} p live", plant + 1);
if plant_config.sensor_p {
let moist = map_range_moisture(
board.measure_moisture_hz(plant, plant_hal::Sensor::PUMP)? as f32,
);
if online_mode == OnlineMode::Online {
let _ = board.mqtt_publish(
&config,
&p_live_topic,
option_to_string(&moist.ok()).as_bytes(),
);
}
} else {
if online_mode == OnlineMode::Online {
let _ =
board.mqtt_publish(&config, &p_live_topic, "disabled".as_bytes());
}
}
} }
board.pump(plant, false)?; board.pump(plant, false)?;
} }
if plant_config.sensor_p {
match map_range_moisture(
board.measure_moisture_hz(plant, plant_hal::Sensor::PUMP)? as f32,
) {
Ok(p) => state.after_p = Some(p),
Err(err) => {
board.fault(plant, true);
state.sensor_error_p = Some(err);
}
}
if state.after_p.is_none()
|| state.p.is_none()
|| state.after_p.unwrap() < state.p.unwrap() + 5
{
state.pump_error = true;
board.fault(plant, true);
//mqtt sync pump error value
}
}
} }
None => { None => {
println!("Nothing to do"); println!("Nothing to do");
@ -784,44 +738,6 @@ fn determine_state_target_moisture_for_plant(
} }
} }
fn determine_state_timer_only_for_plant(
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
plant: usize,
state: &mut PlantState,
config: &Config,
tank_state: &TankState,
cur: DateTime<Tz>,
) {
let plant_config = &config.plants[plant];
let duration = TimeDelta::try_minutes(plant_config.pump_cooldown_min as i64).unwrap();
let last_pump = board.last_pump_time(plant);
match last_pump {
Some(last_pump) => {
let next_pump = last_pump + duration;
if next_pump > cur {
let europe_time = next_pump.with_timezone(&Berlin);
state.next_pump = Some(europe_time);
state.cooldown = true;
} else {
if tank_state.sensor_error && !config.tank_allow_pumping_if_sensor_error {
state.do_water = true;
} else if !tank_state.enough_water {
state.no_water = true;
}
}
}
None => {
println!(
"Could not restore last pump for plant {}, restoring",
plant + 1
);
board.store_last_pump_time(plant, DateTime::from_timestamp_millis(0).unwrap());
state.pump_error = true;
}
}
}
fn determine_state_timer_and_deadzone_for_plant( fn determine_state_timer_and_deadzone_for_plant(
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>, board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
plant: usize, plant: usize,
@ -831,8 +747,13 @@ fn determine_state_timer_and_deadzone_for_plant(
cur: DateTime<Tz>, cur: DateTime<Tz>,
) { ) {
let plant_config = &config.plants[plant]; let plant_config = &config.plants[plant];
if plant_config.mode == Mode::OFF {
return;
}
if !tank_state.enough_water {
state.no_water = true;
}
let duration = TimeDelta::try_minutes(plant_config.pump_cooldown_min as i64).unwrap(); let duration = TimeDelta::try_minutes(plant_config.pump_cooldown_min as i64).unwrap();
let last_pump = board.last_pump_time(plant); let last_pump = board.last_pump_time(plant);
match last_pump { match last_pump {
Some(last_pump) => { Some(last_pump) => {
@ -842,20 +763,6 @@ fn determine_state_timer_and_deadzone_for_plant(
state.next_pump = Some(europe_time); state.next_pump = Some(europe_time);
state.cooldown = true; state.cooldown = true;
} }
if !in_time_range(
&cur,
plant_config.pump_hour_start,
plant_config.pump_hour_end,
) {
state.out_of_work_hour = true;
}
if !state.cooldown && !state.out_of_work_hour {
if tank_state.sensor_error && !config.tank_allow_pumping_if_sensor_error {
state.do_water = true;
} else if !tank_state.enough_water {
state.no_water = true;
}
}
} }
None => { None => {
println!( println!(
@ -863,9 +770,20 @@ fn determine_state_timer_and_deadzone_for_plant(
plant + 1 plant + 1
); );
board.store_last_pump_time(plant, DateTime::from_timestamp_millis(0).unwrap()); board.store_last_pump_time(plant, DateTime::from_timestamp_millis(0).unwrap());
state.pump_error = true; state.do_water = true;
} }
} }
if !in_time_range(
&cur,
plant_config.pump_hour_start,
plant_config.pump_hour_end,
) {
state.out_of_work_hour = true;
}
if !state.no_water && !state.cooldown && !state.out_of_work_hour {
state.do_water = true;
}
} }
fn determine_next_plant( fn determine_next_plant(
@ -887,7 +805,9 @@ fn determine_next_plant(
); );
} }
config::Mode::TimerOnly => { config::Mode::TimerOnly => {
determine_state_timer_only_for_plant(board, plant, state, config, tank_state, cur); determine_state_timer_and_deadzone_for_plant(
board, plant, state, config, tank_state, cur,
);
} }
config::Mode::TimerAndDeadzone => { config::Mode::TimerAndDeadzone => {
determine_state_timer_and_deadzone_for_plant( determine_state_timer_and_deadzone_for_plant(
@ -941,7 +861,7 @@ fn update_plant_state(
a: &sensor_to_string( a: &sensor_to_string(
&state.a, &state.a,
&state.sensor_error_a, &state.sensor_error_a,
plant_config.mode != Mode::OFF, plant_config.mode == Mode::TargetMoisture,
), ),
b: &sensor_to_string(&state.b, &state.sensor_error_b, plant_config.sensor_b), b: &sensor_to_string(&state.b, &state.sensor_error_b, plant_config.sensor_b),
active: state.active, active: state.active,