finish refactor of plant state logic

This commit is contained in:
2025-04-24 23:21:27 +02:00
parent e941a4973d
commit 519c8d2c52
4 changed files with 146 additions and 148 deletions

View File

@@ -4,7 +4,7 @@ use std::{
};
use anyhow::{bail, Result};
use chrono::{DateTime, Datelike, TimeDelta, Timelike, Utc};
use chrono::{DateTime, Datelike, Timelike};
use chrono_tz::{Europe::Berlin, Tz};
use esp_idf_hal::delay::Delay;
@@ -29,7 +29,7 @@ mod plant_state;
mod tank;
pub mod util;
use plant_state::{PlantInfo, PlantState};
use plant_state::PlantState;
use tank::*;
const TIME_ZONE: Tz = Berlin;
@@ -406,8 +406,21 @@ fn safe_main() -> anyhow::Result<()> {
}
};
let mut plantstate: [PlantState; PLANT_COUNT] =
let plantstate: [PlantState; PLANT_COUNT] =
core::array::from_fn(|i| PlantState::read_hardware_state(i, &mut board, &config.plants[i]));
for (plant_id, (plant_state, plant_conf)) in plantstate.iter().zip(&config.plants).enumerate() {
match serde_json::to_string(&plant_state.to_mqtt_info(plant_conf, &timezone_time)) {
Ok(state) => {
let plant_topic = format!("/plant{}", plant_id + 1);
let _ = board.mqtt_publish(&config, &plant_topic, state.as_bytes());
//reduce speed as else messages will be dropped
Delay::new_default().delay_ms(200);
}
Err(err) => {
println!("Error publishing plant state {}", err);
}
};
}
let pump_required = plantstate
.iter()
@@ -447,17 +460,16 @@ fn safe_main() -> anyhow::Result<()> {
//state.active = true;
if !dry_run {
board.pump(plant_id, true)?;
Delay::new_default().delay_ms(1000*plant_config.pump_time_s as u32);
Delay::new_default().delay_ms(1000 * plant_config.pump_time_s as u32);
board.pump(plant_id, false)?;
}
} else if !state.pump_in_timeout(&plant_config, &timezone_time){
} 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.store_consecutive_pump_count(plant_id, 0);
}
}
}
//update_plant_state(&mut plantstate, &mut board, &config);
let is_day = board.is_day();
let state_of_charge = board.state_charge_percent().unwrap_or(0);
@@ -622,40 +634,6 @@ fn main() {
}
}
fn time_to_string_utc(value_option: Option<DateTime<Utc>>) -> String {
let converted = value_option.and_then(|utc| Some(utc.with_timezone(&TIME_ZONE)));
return time_to_string(converted);
}
fn time_to_string(value_option: Option<DateTime<Tz>>) -> String {
match value_option {
Some(value) => {
let europe_time = value.with_timezone(&TIME_ZONE);
if europe_time.year() > 2023 {
return europe_time.to_rfc3339();
} else {
//initial value of 0 in rtc memory
return "N/A".to_owned();
}
}
None => return "N/A".to_owned(),
};
}
fn sensor_to_string(value: &Option<u8>, error: &Option<SensorError>, enabled: bool) -> String {
if enabled {
match error {
Some(error) => return format!("{:?}", error),
None => match value {
Some(v) => return v.to_string(),
None => return "Error".to_owned(),
},
}
} else {
return "disabled".to_owned();
};
}
fn to_string<T: Display>(value: Result<T>) -> String {
return match value {
Ok(v) => v.to_string(),
@@ -665,7 +643,7 @@ fn to_string<T: Display>(value: Result<T>) -> String {
};
}
fn in_time_range(cur: &DateTime<Tz>, start: u8, end: u8) -> bool {
pub fn in_time_range(cur: &DateTime<Tz>, start: u8, end: u8) -> bool {
let curhour = cur.hour() as u8;
//eg 10-14
if start < end {