diff --git a/rust/src/main.rs b/rust/src/main.rs index a19ef4a..8ec5b0b 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -429,27 +429,18 @@ fn safe_main() -> anyhow::Result<()> { match plant_to_pump { Some(plant) => { 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 { - 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); - } + if plant_config.mode == Mode::TargetMoisture { + 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); } + } - println!( "Trying to pump for {}s with pump {} now", plant_config.pump_time_s, plant @@ -463,48 +454,11 @@ fn safe_main() -> anyhow::Result<()> { state.active = true; for _ in 0..plant_config.pump_time_s { 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)?; } - 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 => { 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, -) { - 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( board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>, plant: usize, @@ -831,8 +747,13 @@ fn determine_state_timer_and_deadzone_for_plant( cur: DateTime, ) { 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 last_pump = board.last_pump_time(plant); match last_pump { Some(last_pump) => { @@ -842,20 +763,6 @@ fn determine_state_timer_and_deadzone_for_plant( state.next_pump = Some(europe_time); 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 => { println!( @@ -863,9 +770,20 @@ fn determine_state_timer_and_deadzone_for_plant( plant + 1 ); 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( @@ -887,7 +805,9 @@ fn determine_next_plant( ); } 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 => { determine_state_timer_and_deadzone_for_plant( @@ -941,7 +861,7 @@ fn update_plant_state( a: &sensor_to_string( &state.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), active: state.active,