Browse Source

wip finish data logging

main
Nareshkumar Rao 7 months ago
parent
commit
32d47b5907
  1. 11
      src/config.rs
  2. 21
      src/control/data_logging.rs
  3. 8
      src/control/mod.rs
  4. 1
      src/main.rs

11
src/config.rs

@ -56,6 +56,12 @@ pub struct ControllerSettings {
pub ideal_soil_moisture_pct: u64, pub ideal_soil_moisture_pct: u64,
} }
#[derive(Serialize, Deserialize, Clone)]
pub struct DataLoggingSettings {
pub enabled: bool,
pub frequency_mins: u64,
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Configuration { pub struct Configuration {
pub board_settings: BoardSettings, pub board_settings: BoardSettings,
@ -64,6 +70,7 @@ pub struct Configuration {
pub thermistor_settings: ThermistorSettings, pub thermistor_settings: ThermistorSettings,
pub water_pump_settings: WaterPumpSettings, pub water_pump_settings: WaterPumpSettings,
pub controller_settings: ControllerSettings, pub controller_settings: ControllerSettings,
pub data_logging_settings: DataLoggingSettings,
} }
impl Configuration { impl Configuration {
@ -116,6 +123,10 @@ impl Default for Configuration {
max_water_window_hours: 24, max_water_window_hours: 24,
ideal_soil_moisture_pct: 70, ideal_soil_moisture_pct: 70,
}, },
data_logging_settings: DataLoggingSettings {
enabled: true,
frequency_mins: 60,
},
} }
} }
} }

21
src/data_logging.rs → src/control/data_logging.rs

@ -1,7 +1,10 @@
use std::time::Duration;
use chrono::Utc; use chrono::Utc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
config::DataLoggingSettings,
error::GenericResult, error::GenericResult,
sensors, sensors,
state::{lock_state, ProgramStateShared}, state::{lock_state, ProgramStateShared},
@ -47,3 +50,21 @@ impl DataRecords {
Ok(()) 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;
}
}

8
src/control/mod.rs

@ -2,12 +2,13 @@ use tokio::join;
use crate::{ use crate::{
control::{ 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, state::ProgramStateShared,
}; };
mod data_logging;
mod light; mod light;
mod soil; mod soil;
mod temperature; mod temperature;
@ -16,6 +17,7 @@ pub async fn control_thread(program_state: ProgramStateShared) {
join!( join!(
light_control_loop(program_state.clone()), light_control_loop(program_state.clone()),
temperature_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())
); );
} }

1
src/main.rs

@ -10,7 +10,6 @@ mod actuators;
mod cli_mode; mod cli_mode;
mod config; mod config;
mod control; mod control;
mod data_logging;
mod error; mod error;
mod history; mod history;
mod io; mod io;

Loading…
Cancel
Save