diff --git a/src/config.rs b/src/config.rs index 1d49339..5057f4b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -56,6 +56,12 @@ pub struct ControllerSettings { pub ideal_soil_moisture_pct: u64, } +#[derive(Serialize, Deserialize, Clone)] +pub struct DataLoggingSettings { + pub enabled: bool, + pub frequency_mins: u64, +} + #[derive(Serialize, Deserialize)] pub struct Configuration { pub board_settings: BoardSettings, @@ -64,6 +70,7 @@ pub struct Configuration { pub thermistor_settings: ThermistorSettings, pub water_pump_settings: WaterPumpSettings, pub controller_settings: ControllerSettings, + pub data_logging_settings: DataLoggingSettings, } impl Configuration { @@ -116,6 +123,10 @@ impl Default for Configuration { max_water_window_hours: 24, ideal_soil_moisture_pct: 70, }, + data_logging_settings: DataLoggingSettings { + enabled: true, + frequency_mins: 60, + }, } } } diff --git a/src/data_logging.rs b/src/control/data_logging.rs similarity index 70% rename from src/data_logging.rs rename to src/control/data_logging.rs index 0dbcda3..e4ccd30 100644 --- a/src/data_logging.rs +++ b/src/control/data_logging.rs @@ -1,7 +1,10 @@ +use std::time::Duration; + use chrono::Utc; use serde::{Deserialize, Serialize}; use crate::{ + config::DataLoggingSettings, error::GenericResult, sensors, state::{lock_state, ProgramStateShared}, @@ -47,3 +50,21 @@ impl DataRecords { Ok(()) } } + +pub async fn data_logging_loop(program_state: ProgramStateShared) { + let DataLoggingSettings { + enabled, + frequency_mins, + } = lock_state(&program_state) + .map(|state| state.config.data_logging_settings.clone()) + .unwrap_or(DataLoggingSettings { + enabled: true, + frequency_mins: 60, + }); + loop { + if enabled { + let _ = DataRecords::push(program_state.clone()); + } + tokio::time::sleep(Duration::from_mins(frequency_mins)).await; + } +} diff --git a/src/control/mod.rs b/src/control/mod.rs index a672393..3cb0d7a 100644 --- a/src/control/mod.rs +++ b/src/control/mod.rs @@ -2,12 +2,13 @@ use tokio::join; use crate::{ control::{ - light::light_control_loop, soil::soil_moisture_control_loop, - temperature::temperature_control_loop, + data_logging::data_logging_loop, light::light_control_loop, + soil::soil_moisture_control_loop, temperature::temperature_control_loop, }, state::ProgramStateShared, }; +mod data_logging; mod light; mod soil; mod temperature; @@ -16,6 +17,7 @@ pub async fn control_thread(program_state: ProgramStateShared) { join!( light_control_loop(program_state.clone()), temperature_control_loop(program_state.clone()), - soil_moisture_control_loop(program_state.clone()) + soil_moisture_control_loop(program_state.clone()), + data_logging_loop(program_state.clone()) ); } diff --git a/src/main.rs b/src/main.rs index 71ac8de..288ad24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,6 @@ mod actuators; mod cli_mode; mod config; mod control; -mod data_logging; mod error; mod history; mod io;