Nareshkumar Rao
7 months ago
5 changed files with 104 additions and 81 deletions
@ -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()) |
|
||||
); |
|
||||
} |
|
@ -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; |
||||
|
} |
||||
|
} |
@ -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()) |
||||
|
); |
||||
|
} |
@ -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; |
||||
|
} |
||||
|
} |
@ -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; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue