Nareshkumar Rao 7 months ago
parent
commit
fc937ad46c
  1. 6
      growpi.toml
  2. 10
      src/config.rs
  3. 42
      src/control/soil.rs

6
growpi.toml

@ -35,3 +35,9 @@ temperature_set_point_lower = 28.0
temperature_loop_mins = 60
sunlight_hours = 24
soil_loop_hours = 12
watering_frequency_hours = 30
watering_amount_grams = 200
[data_logging_settings]
enabled = true
frequency_mins = 60

10
src/config.rs

@ -51,9 +51,8 @@ pub struct ControllerSettings {
pub temperature_loop_mins: u64,
pub sunlight_hours: u64,
pub soil_loop_hours: u64,
pub max_water_over_window_grams: u64,
pub max_water_window_hours: u64,
pub ideal_soil_moisture_pct: u64,
pub watering_frequency_hours: u64,
pub watering_amount_grams: u64,
}
#[derive(Serialize, Deserialize, Clone)]
@ -119,9 +118,8 @@ impl Default for Configuration {
temperature_loop_mins: 60,
soil_loop_hours: 12,
sunlight_hours: 24,
max_water_over_window_grams: 1000,
max_water_window_hours: 24,
ideal_soil_moisture_pct: 70,
watering_frequency_hours: 30,
watering_amount_grams: 200,
},
data_logging_settings: DataLoggingSettings {
enabled: true,

42
src/control/soil.rs

@ -1,42 +1,34 @@
use std::time::Duration;
use chrono::{DateTime, Utc};
use crate::{
actuators,
error::GenericResult,
state::{lock_state, ProgramStateShared},
};
pub async fn soil_moisture_control_loop(program_state: ProgramStateShared) {
let loop_duration = program_state
.lock()
.map(|program_state| program_state.config.controller_settings.soil_loop_hours)
.unwrap_or(1);
loop {
let _ = soil_moisture_control(program_state.clone());
tokio::time::sleep(Duration::from_hours(loop_duration)).await;
let watering_frequency_hours = program_state
.lock()
.map(|program_state| {
program_state
.config
.controller_settings
.watering_frequency_hours
})
.unwrap_or(72);
tokio::time::sleep(Duration::from_hours(watering_frequency_hours)).await;
}
}
fn soil_moisture_control(program_state: ProgramStateShared) -> GenericResult<()> {
let program_state = lock_state(&program_state)?;
let mut program_state = lock_state(&program_state)?;
let config = &program_state.config.controller_settings;
let history = &program_state.history.watering_records;
let water_amount_over_window: u64 = history
.iter()
.filter(|record| {
if let Some(time) = DateTime::from_timestamp(record.time, 0) {
let delta = Utc::now() - time;
let delta = delta.num_hours();
delta <= config.max_water_window_hours.try_into().unwrap_or(24)
} else {
false
}
})
.map(|record| record.amount)
.sum();
let watering_amount = config.watering_amount_grams;
actuators::pump_water(
watering_amount.try_into().unwrap_or(100),
&mut program_state,
)?;
Ok(())
}

Loading…
Cancel
Save