mqtt via mcutie

This commit is contained in:
2025-09-29 01:00:11 +02:00
parent 3d18b0dbf6
commit cfe23c8a09
15 changed files with 482 additions and 482 deletions

View File

@@ -118,7 +118,11 @@ 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).await {
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 +143,11 @@ 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).await {
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 +272,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 +338,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>>,
}