Compare commits

...

2 Commits

Author SHA1 Message Date
Nareshkumar Rao 5819fcc7f9 wip 7 months ago
Nareshkumar Rao 32d47b5907 wip finish data logging 7 months ago
  1. 1
      growpi.service
  2. 11
      src/config.rs
  3. 21
      src/control/data_logging.rs
  4. 8
      src/control/mod.rs
  5. 14
      src/main.rs

1
growpi.service

@ -7,6 +7,7 @@ Type=simple
Restart=always
RestartSec=10
ExecStart=/opt/growpi/growpi
WorkingDirectory=/opt/growpi
[Install]
WantedBy=mutli-user.target

11
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,
},
}
}
}

21
src/data_logging.rs → 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;
}
}

8
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())
);
}

14
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;
@ -20,11 +19,16 @@ mod state;
fn load_config() -> config::Configuration {
let config = Configuration::from_file(std::path::Path::new("./growpi.toml"));
if let Err(config) = &config {
println!("Could not load config: {}", config);
match config {
Ok(config) => config,
Err(_) => {
let config = Configuration::default();
config
.save_to_file(std::path::Path::new("./growpi.toml"))
.expect("Could not create default config in ./growpi.toml");
config
}
}
config.unwrap_or_default()
}
#[tokio::main(flavor = "current_thread")]

Loading…
Cancel
Save