it's alive

This commit is contained in:
2025-09-13 01:39:47 +02:00
parent 79087c9353
commit 9de85b6e37
19 changed files with 1567 additions and 1488 deletions

View File

@@ -3,7 +3,7 @@ use crate::{
hal::{Sensor, HAL},
in_time_range,
};
use alloc::string::String;
use alloc::string::{String, ToString};
use chrono::{DateTime, TimeDelta, Utc};
use chrono_tz::Tz;
use serde::{Deserialize, Serialize};
@@ -118,7 +118,7 @@ fn map_range_moisture(
impl PlantState {
pub async fn read_hardware_state(plant_id: usize, board: &mut HAL<'_>) -> Self {
let sensor_a = if board.board_hal.get_config().plants[plant_id].sensor_a {
match board.board_hal.measure_moisture_hz(plant_id, Sensor::A) {
match board.board_hal.measure_moisture_hz(plant_id, Sensor::A).await {
Ok(raw) => match map_range_moisture(
raw,
board.board_hal.get_config().plants[plant_id].moisture_sensor_min_frequency,
@@ -139,7 +139,7 @@ impl PlantState {
};
let sensor_b = if board.board_hal.get_config().plants[plant_id].sensor_b {
match board.board_hal.measure_moisture_hz(plant_id, Sensor::B) {
match board.board_hal.measure_moisture_hz(plant_id, Sensor::B).await {
Ok(raw) => match map_range_moisture(
raw,
board.board_hal.get_config().plants[plant_id].moisture_sensor_min_frequency,
@@ -264,50 +264,50 @@ impl PlantState {
PlantWateringMode::TimerOnly => !self.pump_in_timeout(plant_conf, current_time),
}
}
pub fn to_mqtt_info(
&self,
plant_conf: &PlantConfig,
current_time: &DateTime<Tz>,
) -> PlantInfo<'_> {
PlantInfo {
sensor_a: &self.sensor_a,
sensor_b: &self.sensor_b,
mode: plant_conf.mode,
do_water: self.needs_to_be_watered(plant_conf, current_time),
dry: if let Some(moisture_percent) = self.plant_moisture().0 {
moisture_percent < plant_conf.target_moisture
} else {
false
},
cooldown: self.pump_in_timeout(plant_conf, current_time),
out_of_work_hour: in_time_range(
current_time,
plant_conf.pump_hour_start,
plant_conf.pump_hour_end,
),
consecutive_pump_count: self.pump.consecutive_pump_count,
pump_error: self.pump.is_err(plant_conf),
last_pump: self
.pump
.previous_pump
.map(|t| t.with_timezone(&current_time.timezone())),
next_pump: if matches!(
plant_conf.mode,
PlantWateringMode::TimerOnly
| PlantWateringMode::TargetMoisture
| PlantWateringMode::MinMoisture
) {
self.pump.previous_pump.and_then(|last_pump| {
last_pump
.checked_add_signed(TimeDelta::minutes(plant_conf.pump_cooldown_min.into()))
.map(|t| t.with_timezone(&current_time.timezone()))
})
} else {
None
},
}
}
//
// pub fn to_mqtt_info(
// &self,
// plant_conf: &PlantConfig,
// current_time: &DateTime<Tz>,
// ) -> PlantInfo<'_> {
// PlantInfo {
// sensor_a: &self.sensor_a,
// sensor_b: &self.sensor_b,
// mode: plant_conf.mode,
// do_water: self.needs_to_be_watered(plant_conf, current_time),
// dry: if let Some(moisture_percent) = self.plant_moisture().0 {
// moisture_percent < plant_conf.target_moisture
// } else {
// false
// },
// cooldown: self.pump_in_timeout(plant_conf, current_time),
// out_of_work_hour: in_time_range(
// current_time,
// plant_conf.pump_hour_start,
// plant_conf.pump_hour_end,
// ),
// consecutive_pump_count: self.pump.consecutive_pump_count,
// pump_error: self.pump.is_err(plant_conf),
// last_pump: self
// .pump
// .previous_pump
// .map(|t| t.with_timezone(&current_time.timezone())),
// next_pump: if matches!(
// plant_conf.mode,
// PlantWateringMode::TimerOnly
// | PlantWateringMode::TargetMoisture
// | PlantWateringMode::MinMoisture
// ) {
// self.pump.previous_pump.and_then(|last_pump| {
// last_pump
// .checked_add_signed(TimeDelta::minutes(plant_conf.pump_cooldown_min.into()))
// .map(|t| t.with_timezone(&current_time.timezone()))
// })
// } else {
// None
// },
// }
// }
}
#[derive(Debug, PartialEq, Serialize)]
@@ -330,8 +330,8 @@ pub struct PlantInfo<'a> {
/// how often has the pump been watered without reaching target moisture
consecutive_pump_count: u32,
pump_error: Option<PumpError>,
/// last time when the pump was active
last_pump: Option<DateTime<Tz>>,
/// next time when pump should activate
next_pump: Option<DateTime<Tz>>,
// /// last time when the pump was active
// last_pump: Option<DateTime<Tz>>,
// /// next time when pump should activate
// next_pump: Option<DateTime<Tz>>,
}