From f71ca7ec6d40c48256d4ca061b85711635cd24d9 Mon Sep 17 00:00:00 2001 From: ju6ge Date: Sun, 10 May 2026 19:41:51 +0200 Subject: [PATCH] activate plant watering code again for some idiotic the most critical part of the code was disabled and commited that should not have happend! --- rust/src/main.rs | 161 ++++++++++++++++++++++++----------------------- 1 file changed, 82 insertions(+), 79 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index 92b21c2..9efde4f 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -26,7 +26,7 @@ use crate::{ config::BoardVersion::INITIAL, hal::{PlantHal, HAL, PLANT_COUNT}, }; -use ::log::{info, warn, error}; +use ::log::{error, info, warn}; use alloc::borrow::ToOwned; use alloc::string::{String, ToString}; use alloc::sync::Arc; @@ -122,8 +122,6 @@ pub struct PumpResult { pump_time_s: u16, } - - async fn safe_main(spawner: Spawner) -> FatResult<()> { info!("Startup Rust"); @@ -197,10 +195,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { info!("No wifi configured, starting initial config mode"); let esp = board.board_hal.get_esp(); - let ssid = esp.load_config().await + let ssid = esp + .load_config() + .await .map(|config| config.network.ap_ssid.to_string()) .unwrap_or_else(|_| String::from("PlantCtrl Emergency Mode")); - let device = esp.interface_ap.take().context("AP interface already taken")?; + let device = esp + .interface_ap + .take() + .context("AP interface already taken")?; let stack = network::wifi_ap(ssid, device, &esp.controller, &mut esp.rng, spawner).await?; let reboot_now = Arc::new(AtomicBool::new(false)); @@ -217,7 +220,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { info!("No wifi configured"); //the current sensors require this amount to stabilize, in the case of Wi-Fi this is already handled due to connect timings; Timer::after_millis(100).await; - network::NetworkMode::OFFLINE + network::NetworkMode::OFFLINE }; if matches!(network_mode, network::NetworkMode::OFFLINE) && to_config { @@ -225,14 +228,18 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { let res = { let esp = board.board_hal.get_esp(); - let ssid = esp.load_config().await + let ssid = esp + .load_config() + .await .map(|config| config.network.ap_ssid.to_string()) .unwrap_or_else(|_| String::from("PlantCtrl Emergency Mode")); let device = match esp.interface_ap.take() { Some(d) => d, None => { use crate::fat_error::FatError; - return Err(FatError::String { error: "AP interface already taken".to_string() }); + return Err(FatError::String { + error: "AP interface already taken".to_string(), + }); } }; network::wifi_ap(ssid, device, &esp.controller, &mut esp.rng, spawner).await @@ -302,7 +309,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { log(LogMessage::NormalRun, 0, 0, "", ""); } - let _dry_run = false; + let dry_run = false; let tank_state = determine_tank_state(&mut board).await; @@ -339,7 +346,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { } } - let mut _water_frozen = false; + let mut water_frozen = false; let water_temp: FatResult = match board.board_hal.get_tank_sensor() { Ok(sensor) => sensor.water_temperature_c().await, Err(e) => Err(e), @@ -347,7 +354,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { if let Ok(res) = water_temp { if res < WATER_FROZEN_THRESH { - _water_frozen = true; + water_frozen = true; } } info!("Water temp is {}", water_temp.as_ref().unwrap_or(&0.)); @@ -367,74 +374,70 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { publish_plant_states(&mut board, &timezone_time.clone(), &plantstate).await; - // let pump_required = plantstate - // .iter() - // .zip(&board.board_hal.get_config().plants) - // .any(|(it, conf)| it.needs_to_be_watered(conf, &timezone_time)) - // && !water_frozen; - // if pump_required { - // log(LogMessage::EnableMain, dry_run as u32, 0, "", ""); - // for (plant_id, (state, plant_config)) in plantstate - // .iter() - // .zip(&board.board_hal.get_config().plants.clone()) - // .enumerate() - // { - // if state.needs_to_be_watered(plant_config, &timezone_time) { - // let pump_count = board.board_hal.get_esp().consecutive_pump_count(plant_id) + 1; - // board - // .board_hal - // .get_esp() - // .store_consecutive_pump_count(plant_id, pump_count); - // - // let pump_ineffective = pump_count > plant_config.max_consecutive_pump_count as u32; - // if pump_ineffective { - // log( - // LogMessage::ConsecutivePumpCountLimit, - // pump_count, - // plant_config.max_consecutive_pump_count as u32, - // &(plant_id + 1).to_string(), - // "", - // ); - // board.board_hal.fault(plant_id, true).await?; - // } - // log( - // LogMessage::PumpPlant, - // (plant_id + 1) as u32, - // plant_config.pump_time_s as u32, - // &dry_run.to_string(), - // "", - // ); - // board - // .board_hal - // .get_esp() - // .store_last_pump_time(plant_id, cur); - // board.board_hal.get_esp().last_pump_time(plant_id); - // //state.active = true; - // - // pump_info(plant_id, true, pump_ineffective, 0, 0, 0, false).await; - // - // let result = do_secure_pump(plant_id, plant_config, dry_run).await?; - // board.board_hal.pump(plant_id, false).await?; - // pump_info( - // plant_id, - // false, - // pump_ineffective, - // result.median_current_ma, - // result.max_current_ma, - // result.min_current_ma, - // result.error, - // ) - // .await; - // } else if !state.pump_in_timeout(plant_config, &timezone_time) { - // // plant does not need to be watered and is not in timeout - // // -> reset consecutive pump count - // board - // .board_hal - // .get_esp() - // .store_consecutive_pump_count(plant_id, 0); - // } - // } - // } + let pump_required = plantstate + .iter() + .zip(&board.board_hal.get_config().plants) + .any(|(it, conf)| it.needs_to_be_watered(conf, &timezone_time)) + && !water_frozen; + if pump_required { + log(LogMessage::EnableMain, dry_run as u32, 0, "", ""); + for (plant_id, (state, plant_config)) in plantstate + .iter() + .zip(&board.board_hal.get_config().plants.clone()) + .enumerate() + { + if state.needs_to_be_watered(plant_config, &timezone_time) { + let pump_count = board.board_hal.get_esp().consecutive_pump_count(plant_id) + 1; + board + .board_hal + .get_esp() + .store_consecutive_pump_count(plant_id, pump_count); + let pump_ineffective = pump_count > plant_config.max_consecutive_pump_count as u32; + if pump_ineffective { + log( + LogMessage::ConsecutivePumpCountLimit, + pump_count, + plant_config.max_consecutive_pump_count as u32, + &(plant_id + 1).to_string(), + "", + ); + board.board_hal.fault(plant_id, true).await?; + } + log( + LogMessage::PumpPlant, + (plant_id + 1) as u32, + plant_config.pump_time_s as u32, + &dry_run.to_string(), + "", + ); + board + .board_hal + .get_esp() + .store_last_pump_time(plant_id, cur); + board.board_hal.get_esp().last_pump_time(plant_id); + pump_info(plant_id, true, pump_ineffective, 0, 0, 0, false).await; + let result = do_secure_pump(&mut board, plant_id, plant_config, dry_run).await?; + board.board_hal.pump(plant_id, false).await?; + pump_info( + plant_id, + false, + pump_ineffective, + result.median_current_ma, + result.max_current_ma, + result.min_current_ma, + result.error, + ) + .await; + } else if !state.pump_in_timeout(plant_config, &timezone_time) { + // plant does not need to be watered and is not in timeout + // -> reset consecutive pump count + board + .board_hal + .get_esp() + .store_consecutive_pump_count(plant_id, 0); + } + } + } info!("state of charg"); let is_day = board.board_hal.is_day();