From 34e8aee41b65ad38abb8be6d60ecb231195deaa2 Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao Date: Thu, 2 May 2024 20:40:17 +0200 Subject: [PATCH] wip control --- src/control.rs | 81 -------------------------------------- src/control/light.rs | 31 +++++++++++++++ src/control/mod.rs | 21 ++++++++++ src/control/soil.rs | 14 +++++++ src/control/temperature.rs | 38 ++++++++++++++++++ 5 files changed, 104 insertions(+), 81 deletions(-) delete mode 100644 src/control.rs create mode 100644 src/control/light.rs create mode 100644 src/control/mod.rs create mode 100644 src/control/soil.rs create mode 100644 src/control/temperature.rs diff --git a/src/control.rs b/src/control.rs deleted file mode 100644 index 196dcb8..0000000 --- a/src/control.rs +++ /dev/null @@ -1,81 +0,0 @@ -use std::time::Duration; - -use chrono::{Local, Timelike}; -use tokio::join; - -use crate::{ - actuators, - error::{lock_err, GenericResult}, - sensors, - state::ProgramStateShared, -}; - -async fn temperature_control(program_state: ProgramStateShared) -> GenericResult<()> { - let mut program_state = program_state.lock().map_err(lock_err)?; - let config = &program_state.config.controller_settings; - - let current_temperature = sensors::get_temperature(&program_state.config)?; - if current_temperature > config.temperature_set_point_upper { - actuators::switch_fan(crate::io::RelaySwitchState::On, &mut program_state)?; - } else if current_temperature < config.temperature_set_point_lower { - actuators::switch_fan(crate::io::RelaySwitchState::Off, &mut program_state)?; - } - Ok(()) -} - -async fn temperature_control_loop(program_state: ProgramStateShared) { - let loop_duration = program_state - .lock() - .map(|program_state| { - program_state - .config - .controller_settings - .temperature_loop_mins - }) - .unwrap_or(1); - - loop { - let _ = temperature_control(program_state.clone()).await; - tokio::time::sleep(Duration::from_mins(loop_duration)).await; - } -} -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 { - tokio::time::sleep(Duration::from_hours(loop_duration)).await; - } -} - -async fn light_control(program_state: ProgramStateShared) -> GenericResult<()> { - let program_state = program_state.clone(); - let mut program_state = program_state.lock().map_err(lock_err)?; - let local = Local::now(); - let hour = local.time().hour(); - const HOURS_ON: u32 = 24; - if hour <= HOURS_ON { - actuators::switch_lights(crate::io::RelaySwitchState::On, &mut program_state)?; - } else { - actuators::switch_lights(crate::io::RelaySwitchState::Off, &mut program_state)?; - } - - Ok(()) -} - -async fn light_control_loop(program_state: ProgramStateShared) { - loop { - let _ = light_control(program_state.clone()).await; - tokio::time::sleep(Duration::from_hours(1)).await; - } -} - -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()) - ); -} diff --git a/src/control/light.rs b/src/control/light.rs new file mode 100644 index 0000000..5fb2c52 --- /dev/null +++ b/src/control/light.rs @@ -0,0 +1,31 @@ +use std::time::Duration; + +use chrono::{Local, Timelike}; + +use crate::{ + actuators, + error::{lock_err, GenericResult}, + state::ProgramStateShared, +}; + +fn light_control(program_state: ProgramStateShared) -> GenericResult<()> { + let program_state = program_state.clone(); + let mut program_state = program_state.lock().map_err(lock_err)?; + let local = Local::now(); + let hour = local.time().hour(); + const HOURS_ON: u32 = 24; + if hour <= HOURS_ON { + actuators::switch_lights(crate::io::RelaySwitchState::On, &mut program_state)?; + } else { + actuators::switch_lights(crate::io::RelaySwitchState::Off, &mut program_state)?; + } + + Ok(()) +} + +pub async fn light_control_loop(program_state: ProgramStateShared) { + loop { + let _ = light_control(program_state.clone()); + tokio::time::sleep(Duration::from_hours(1)).await; + } +} diff --git a/src/control/mod.rs b/src/control/mod.rs new file mode 100644 index 0000000..a672393 --- /dev/null +++ b/src/control/mod.rs @@ -0,0 +1,21 @@ +use tokio::join; + +use crate::{ + control::{ + light::light_control_loop, soil::soil_moisture_control_loop, + temperature::temperature_control_loop, + }, + state::ProgramStateShared, +}; + +mod light; +mod soil; +mod temperature; + +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()) + ); +} diff --git a/src/control/soil.rs b/src/control/soil.rs new file mode 100644 index 0000000..c4b7a87 --- /dev/null +++ b/src/control/soil.rs @@ -0,0 +1,14 @@ +use std::time::Duration; + +use crate::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 { + tokio::time::sleep(Duration::from_hours(loop_duration)).await; + } +} diff --git a/src/control/temperature.rs b/src/control/temperature.rs new file mode 100644 index 0000000..e4d139e --- /dev/null +++ b/src/control/temperature.rs @@ -0,0 +1,38 @@ +use std::time::Duration; + +use crate::{ + actuators, + error::{lock_err, GenericResult}, + sensors, + state::ProgramStateShared, +}; + +fn temperature_control(program_state: ProgramStateShared) -> GenericResult<()> { + let mut program_state = program_state.lock().map_err(lock_err)?; + let config = &program_state.config.controller_settings; + + let current_temperature = sensors::get_temperature(&program_state.config)?; + if current_temperature > config.temperature_set_point_upper { + actuators::switch_fan(crate::io::RelaySwitchState::On, &mut program_state)?; + } else if current_temperature < config.temperature_set_point_lower { + actuators::switch_fan(crate::io::RelaySwitchState::Off, &mut program_state)?; + } + Ok(()) +} + +pub async fn temperature_control_loop(program_state: ProgramStateShared) { + let loop_duration = program_state + .lock() + .map(|program_state| { + program_state + .config + .controller_settings + .temperature_loop_mins + }) + .unwrap_or(1); + + loop { + let _ = temperature_control(program_state.clone()); + tokio::time::sleep(Duration::from_mins(loop_duration)).await; + } +}