remove: eliminate MoistureSensorState::Disabled, simplify moisture sensor processing, refactor pump logic, and clean up redundant/unnecessary code

This commit is contained in:
2026-03-12 20:36:18 +01:00
parent 5c78495bd5
commit a4d764c4fe
10 changed files with 145 additions and 144 deletions

View File

@@ -29,8 +29,8 @@ use ::log::{error, info, warn};
use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::{format, vec};
use alloc::vec::Vec;
use alloc::{format, vec};
use chrono::{DateTime, Datelike, Timelike, Utc};
use chrono_tz::Tz::{self, UTC};
use core::sync::atomic::{AtomicBool, Ordering};
@@ -501,16 +501,17 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
info!("state of charg");
let is_day = board.board_hal.is_day();
let battery_state = board.board_hal.get_battery_monitor().get_state().await.unwrap_or(BatteryState::Unknown);
let battery_state = board
.board_hal
.get_battery_monitor()
.get_state()
.await
.unwrap_or(BatteryState::Unknown);
info!("Battery state is {battery_state:?}");
let state_of_charge = match &battery_state {
BatteryState::Unknown => {
0
}
BatteryState::Info(data) => {
data.state_of_charge
}
BatteryState::Unknown => 0,
BatteryState::Info(data) => data.state_of_charge,
};
let mut light_state = LightState {
@@ -529,22 +530,10 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
board.board_hal.get_config().night_lamp.night_lamp_hour_end,
);
if state_of_charge
< board
.board_hal
.get_config()
.night_lamp
.low_soc_cutoff
{
if state_of_charge < board.board_hal.get_config().night_lamp.low_soc_cutoff {
board.board_hal.get_esp().set_low_voltage_in_cycle();
info!("Set low voltage in cycle");
} else if state_of_charge
> board
.board_hal
.get_config()
.night_lamp
.low_soc_restore
{
} else if state_of_charge > board.board_hal.get_config().night_lamp.low_soc_restore {
board.board_hal.get_esp().clear_low_voltage_in_cycle();
info!("Clear low voltage in cycle");
}
@@ -652,17 +641,23 @@ pub async fn do_secure_pump(
plant_config: &PlantConfig,
dry_run: bool,
) -> FatResult<PumpResult> {
let mut current_collector = vec![0_u16; plant_config.pump_time_s.into()];
let mut flow_collector = vec![0_i16; plant_config.pump_time_s.into()];
const STARTUP_IGNORE_MS: u32 = 500;
const STARTUP_ABORT_CURRENT_MA: u16 = 1500;
let steps_in_50ms = plant_config.pump_time_s as usize * 20;
let mut current_collector = vec![0_u16; steps_in_50ms];
let mut flow_collector = vec![0_i16; steps_in_50ms];
let mut error = false;
let mut first_error = true;
let mut pump_time_s = 0;
let mut pump_time_ms: u32 = 0;
if !dry_run {
board.board_hal.get_tank_sensor()?.reset_flow_meter();
board.board_hal.get_tank_sensor()?.start_flow_meter();
board.board_hal.pump(plant_id, true).await?;
Timer::after_millis(10).await;
for step in 0..plant_config.pump_time_s as usize {
for step in 0..steps_in_50ms {
let flow_value = board.board_hal.get_tank_sensor()?.get_flow_meter_value();
flow_collector[step] = flow_value;
let flow_value_ml = flow_value as f32 * board.board_hal.get_config().tank.ml_per_pulse;
@@ -682,7 +677,20 @@ pub async fn do_secure_pump(
let current_ma = current.as_milliamperes() as u16;
current_collector[step] = current_ma;
let high_current = current_ma > plant_config.max_pump_current_ma;
if high_current && first_error {
if pump_time_ms < STARTUP_IGNORE_MS
&& high_current
&& current_ma > STARTUP_ABORT_CURRENT_MA
{
log(
LogMessage::PumpOverCurrent,
plant_id as u32 + 1,
current_ma as u32,
plant_config.max_pump_current_ma.to_string().as_str(),
step.to_string().as_str(),
)
.await;
error = true;
} else if high_current && first_error {
log(
LogMessage::PumpOverCurrent,
plant_id as u32 + 1,
@@ -732,12 +740,14 @@ pub async fn do_secure_pump(
error = true;
break;
} else {
//e.g., v3 without a sensor ends here, do not spam
error!("Error getting pump current: {err}");
}
}
}
Timer::after_millis(1000).await;
pump_time_s += 1;
//todo calcualte dynamically to offset time for stuff
Timer::after_millis(50).await;
pump_time_ms += 50;
}
}
board.board_hal.get_tank_sensor()?.stop_flow_meter();
@@ -751,14 +761,14 @@ pub async fn do_secure_pump(
min_current_ma: current_collector[0],
flow_value_ml,
flow_value_count: final_flow_value,
pump_time_s,
pump_time_s: (pump_time_ms / 1000) as u16,
error,
})
}
async fn update_charge_indicator(
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
) -> FatResult<()>{
) -> FatResult<()> {
//FIXME add config and code to allow power supply mode, in this case this is a nop
//we have mppt controller, ask it for charging current
let current = board.board_hal.get_mptt_current().await?;
@@ -766,10 +776,9 @@ async fn update_charge_indicator(
.board_hal
.set_charge_indicator(current.as_milliamperes() > 20_f64)
.await?;
Ok(())
Ok(())
}
async fn publish_tank_state(
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
tank_state: &TankState,
@@ -949,11 +958,7 @@ async fn publish_mppt_state(
async fn publish_battery_state(
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
) -> () {
let state = board
.board_hal
.get_battery_monitor()
.get_state()
.await;
let state = board.board_hal.get_battery_monitor().get_state().await;
let value = match state {
Ok(state) => {
let json = serde_json::to_string(&state).unwrap().to_owned();
@@ -986,7 +991,7 @@ async fn wait_infinity(
loop {
{
let mut board = BOARD_ACCESS.get().await.lock().await;
match update_charge_indicator(&mut board).await{
match update_charge_indicator(&mut board).await {
Ok(_) => {}
Err(error) => {
if !suppress_further_mppt_error {
@@ -1069,35 +1074,38 @@ async fn wait_infinity(
}
}
async fn handle_serial_config(board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'_>>, serial_config_receive: &AtomicBool, reboot_now: &AtomicBool) -> FatResult<()> {
async fn handle_serial_config(
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'_>>,
serial_config_receive: &AtomicBool,
reboot_now: &AtomicBool,
) -> FatResult<()> {
match board.board_hal.get_esp().read_serial_line().await {
Ok(serial_line) => {
match serial_line {
None => {
Ok(serial_line) => match serial_line {
None => Ok(()),
Some(line) => {
if serial_config_receive.load(Ordering::Relaxed) {
let ll = line.as_str();
let config: PlantControllerConfig = serde_json::from_str(ll)?;
board
.board_hal
.get_esp()
.save_config(Vec::from(ll.as_bytes()))
.await?;
board.board_hal.set_config(config);
serial_config_receive.store(false, Ordering::Relaxed);
info!("Config received, rebooting");
board.board_hal.get_esp().set_restart_to_conf(false);
reboot_now.store(true, Ordering::Relaxed);
Ok(())
} else {
if line == "automation:streamconfig" {
serial_config_receive.store(true, Ordering::Relaxed);
info!("streamconfig:recieving");
}
Ok(())
}
Some(line) => {
if serial_config_receive.load(Ordering::Relaxed) {
let ll = line.as_str();
let config: PlantControllerConfig = serde_json::from_str(ll)?;
board.board_hal.get_esp().save_config(Vec::from(ll.as_bytes())).await?;
board.board_hal.set_config(config);
serial_config_receive.store(false, Ordering::Relaxed);
info!("Config received, rebooting");
board.board_hal.get_esp().set_restart_to_conf(false);
reboot_now.store(true, Ordering::Relaxed);
Ok(())
} else {
if line == "automation:streamconfig" {
serial_config_receive.store(true, Ordering::Relaxed);
info!("streamconfig:recieving");
}
Ok(())
}
}
}
}
},
Err(_) => {
error!("Error reading serial line");
Ok(())