add ability to override frequency per plant and adjust timezone, fix missing workhour for plants
This commit is contained in:
@@ -7,7 +7,7 @@ use crate::{
|
||||
in_time_range, plant_hal,
|
||||
};
|
||||
|
||||
const MOIST_SENSOR_MAX_FREQUENCY: f32 = 5500.; // 60kHz (500Hz margin)
|
||||
const MOIST_SENSOR_MAX_FREQUENCY: f32 = 6500.; // 60kHz (500Hz margin)
|
||||
const MOIST_SENSOR_MIN_FREQUENCY: f32 = 150.; // this is really really dry, think like cactus levels
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
@@ -87,23 +87,30 @@ pub struct PlantState {
|
||||
pub pump: PumpState,
|
||||
}
|
||||
|
||||
fn map_range_moisture(s: f32) -> Result<f32, MoistureSensorError> {
|
||||
if s < MOIST_SENSOR_MIN_FREQUENCY {
|
||||
fn map_range_moisture(
|
||||
s: f32,
|
||||
min_frequency: Option<f32>,
|
||||
max_frequency: Option<f32>,
|
||||
) -> Result<f32, MoistureSensorError> {
|
||||
// Use overrides if provided, otherwise fallback to defaults
|
||||
let min_freq = min_frequency.unwrap_or(MOIST_SENSOR_MIN_FREQUENCY);
|
||||
let max_freq = max_frequency.unwrap_or(MOIST_SENSOR_MAX_FREQUENCY);
|
||||
|
||||
if s < min_freq {
|
||||
return Err(MoistureSensorError::OpenLoop {
|
||||
hz: s,
|
||||
min: MOIST_SENSOR_MIN_FREQUENCY,
|
||||
min: min_freq,
|
||||
});
|
||||
}
|
||||
if s > MOIST_SENSOR_MAX_FREQUENCY {
|
||||
if s > max_freq {
|
||||
return Err(MoistureSensorError::ShortCircuit {
|
||||
hz: s,
|
||||
max: MOIST_SENSOR_MAX_FREQUENCY,
|
||||
max: max_freq,
|
||||
});
|
||||
}
|
||||
let moisture_percent = (s - MOIST_SENSOR_MIN_FREQUENCY) * 100.0
|
||||
/ (MOIST_SENSOR_MAX_FREQUENCY - MOIST_SENSOR_MIN_FREQUENCY);
|
||||
let moisture_percent = (s - min_freq) * 100.0 / (max_freq - min_freq);
|
||||
|
||||
return Ok(moisture_percent);
|
||||
Ok(moisture_percent)
|
||||
}
|
||||
|
||||
impl PlantState {
|
||||
@@ -114,7 +121,11 @@ impl PlantState {
|
||||
) -> Self {
|
||||
let sensor_a = if config.sensor_a {
|
||||
match board.measure_moisture_hz(plant_id, plant_hal::Sensor::A) {
|
||||
Ok(raw) => match map_range_moisture(raw) {
|
||||
Ok(raw) => match map_range_moisture(
|
||||
raw,
|
||||
config.moisture_sensor_min_frequency,
|
||||
config.moisture_sensor_max_frequency,
|
||||
) {
|
||||
Ok(moisture_percent) => MoistureSensorState::MoistureValue {
|
||||
raw_hz: raw,
|
||||
moisture_percent,
|
||||
@@ -128,9 +139,14 @@ impl PlantState {
|
||||
} else {
|
||||
MoistureSensorState::Disabled
|
||||
};
|
||||
|
||||
let sensor_b = if config.sensor_b {
|
||||
match board.measure_moisture_hz(plant_id, plant_hal::Sensor::B) {
|
||||
Ok(raw) => match map_range_moisture(raw) {
|
||||
Ok(raw) => match map_range_moisture(
|
||||
raw,
|
||||
config.moisture_sensor_min_frequency,
|
||||
config.moisture_sensor_max_frequency,
|
||||
) {
|
||||
Ok(moisture_percent) => MoistureSensorState::MoistureValue {
|
||||
raw_hz: raw,
|
||||
moisture_percent,
|
||||
@@ -144,6 +160,7 @@ impl PlantState {
|
||||
} else {
|
||||
MoistureSensorState::Disabled
|
||||
};
|
||||
|
||||
let previous_pump = board.last_pump_time(plant_id);
|
||||
let consecutive_pump_count = board.consecutive_pump_count(plant_id);
|
||||
let state = Self {
|
||||
@@ -210,7 +227,11 @@ impl PlantState {
|
||||
false
|
||||
} else {
|
||||
if moisture_percent < plant_conf.target_moisture {
|
||||
true
|
||||
in_time_range(
|
||||
current_time,
|
||||
plant_conf.pump_hour_start,
|
||||
plant_conf.pump_hour_end,
|
||||
)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
@@ -293,4 +314,4 @@ pub struct PlantInfo<'a> {
|
||||
last_pump: Option<DateTime<Tz>>,
|
||||
/// next time when pump should activate
|
||||
next_pump: Option<DateTime<Tz>>,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user