diff --git a/src/cli_mode.rs b/src/cli_mode.rs index ba7f942..445fc7f 100644 --- a/src/cli_mode.rs +++ b/src/cli_mode.rs @@ -4,10 +4,10 @@ use rustyline::{config::Configurer, error::ReadlineError, history::FileHistory}; use crate::{ actuators, - error::{lock_err, GenericResult}, + error::GenericResult, io::{self, get_input_voltage}, sensors, - state::ProgramStateShared, + state::{lock_state, ProgramStateShared}, }; struct LoopFlags { @@ -31,7 +31,7 @@ fn process_input(input: String, program_state: ProgramStateShared) -> GenericRes } fn command_pump(args: &[&str], program_state: ProgramStateShared) -> GenericResult<()> { - let mut program_state = program_state.lock().map_err(lock_err)?; + let mut program_state = lock_state(&program_state)?; let use_grams = args .get(2) @@ -59,7 +59,7 @@ fn command_temp(args: &[&str], program_state: ProgramStateShared) -> GenericResu .map(|arg| matches!(*arg, "loop")) .unwrap_or(false); loop { - let program_state = program_state.lock().map_err(lock_err)?; + let program_state = lock_state(&program_state)?; let temperature = sensors::get_temperature(&program_state.config)?; println!("Temperature: {}C", temperature); if !show_loop { @@ -77,7 +77,7 @@ fn command_soil(args: &[&str], program_state: ProgramStateShared) -> GenericResu .unwrap_or(false); loop { - let program_state = program_state.lock().map_err(lock_err)?; + let program_state = lock_state(&program_state)?; let humidity = sensors::get_soil_moisture(&program_state.config)?; println!("Soil humidity: {}", humidity); if !show_loop { @@ -90,7 +90,7 @@ fn command_soil(args: &[&str], program_state: ProgramStateShared) -> GenericResu } fn command_rel(args: &[&str], program_state: ProgramStateShared) -> GenericResult<()> { - let mut program_state = program_state.lock().map_err(lock_err)?; + let mut program_state = lock_state(&program_state)?; let pin = args .get(1) diff --git a/src/control/light.rs b/src/control/light.rs index be5f2c0..4bba31c 100644 --- a/src/control/light.rs +++ b/src/control/light.rs @@ -4,13 +4,13 @@ use chrono::{Local, Timelike}; use crate::{ actuators, - error::{lock_err, GenericResult}, - state::ProgramStateShared, + error::GenericResult, + state::{lock_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 mut program_state = lock_state(&program_state)?; let local = Local::now(); let hour = local.time().hour(); if hour as u64 <= program_state.config.controller_settings.sunlight_hours { diff --git a/src/control/soil.rs b/src/control/soil.rs index 04a527a..af95120 100644 --- a/src/control/soil.rs +++ b/src/control/soil.rs @@ -3,8 +3,8 @@ use std::time::Duration; use chrono::{DateTime, Utc}; use crate::{ - error::{lock_err, GenericResult}, - state::ProgramStateShared, + error::GenericResult, + state::{lock_state, ProgramStateShared}, }; pub async fn soil_moisture_control_loop(program_state: ProgramStateShared) { @@ -20,7 +20,7 @@ pub async fn soil_moisture_control_loop(program_state: ProgramStateShared) { } fn soil_moisture_control(program_state: ProgramStateShared) -> GenericResult<()> { - let program_state = program_state.lock().map_err(lock_err)?; + let program_state = lock_state(&program_state)?; let config = &program_state.config.controller_settings; let history = &program_state.history.watering_records; diff --git a/src/control/temperature.rs b/src/control/temperature.rs index e4d139e..36404d4 100644 --- a/src/control/temperature.rs +++ b/src/control/temperature.rs @@ -2,13 +2,13 @@ use std::time::Duration; use crate::{ actuators, - error::{lock_err, GenericResult}, + error::GenericResult, sensors, - state::ProgramStateShared, + state::{lock_state, ProgramStateShared}, }; fn temperature_control(program_state: ProgramStateShared) -> GenericResult<()> { - let mut program_state = program_state.lock().map_err(lock_err)?; + let mut program_state = lock_state(&program_state)?; let config = &program_state.config.controller_settings; let current_temperature = sensors::get_temperature(&program_state.config)?; diff --git a/src/data_logging.rs b/src/data_logging.rs index 634ee02..0dbcda3 100644 --- a/src/data_logging.rs +++ b/src/data_logging.rs @@ -2,9 +2,9 @@ use chrono::Utc; use serde::{Deserialize, Serialize}; use crate::{ - error::{lock_err, GenericResult}, + error::GenericResult, sensors, - state::ProgramStateShared, + state::{lock_state, ProgramStateShared}, }; #[derive(Serialize, Deserialize)] @@ -32,7 +32,7 @@ impl DataRecords { Ok(DataRecords { records: result }) } pub fn push(program_state: ProgramStateShared) -> GenericResult<()> { - let program_state = program_state.lock().map_err(lock_err)?; + let program_state = lock_state(&program_state)?; let config = &program_state.config; let record = DataRecord { timestamp: Utc::now().timestamp(), diff --git a/src/error.rs b/src/error.rs index 8f4e5aa..9fc7342 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,3 @@ use std::error::Error; pub type GenericResult = Result>; - -pub fn lock_err(e: T) -> String { - format!("{}", e) -} diff --git a/src/server.rs b/src/server.rs index 143f9a8..1620c43 100644 --- a/src/server.rs +++ b/src/server.rs @@ -10,10 +10,10 @@ use tower_http::cors::{Any, CorsLayer}; use crate::{ actuators, - error::{lock_err, GenericResult}, + error::GenericResult, io::RelaySwitchState, sensors, - state::ProgramStateShared, + state::{lock_state, ProgramStateShared}, }; pub async fn run_server(program_state: ProgramStateShared) { @@ -40,7 +40,7 @@ async fn pump_handler( State(program_state): State, ) -> impl IntoResponse { let exec = || -> GenericResult<()> { - let mut program_state = program_state.lock().map_err(lock_err)?; + let mut program_state = lock_state(&program_state)?; actuators::pump_water(quantity, &mut program_state)?; Ok(()) }; @@ -55,7 +55,7 @@ async fn switch_handler( State(program_state): State, ) -> impl IntoResponse { let exec = || -> GenericResult<()> { - let mut program_state = program_state.lock().map_err(lock_err)?; + let mut program_state = lock_state(&program_state)?; match device.as_str() { "lights" => actuators::switch_lights(state, &mut program_state)?, "fan" => actuators::switch_fan(state, &mut program_state)?, @@ -81,7 +81,7 @@ struct Info { async fn info_handler( State(program_state): State, ) -> Result, String> { - let mut program_state = program_state.lock().map_err(lock_err)?; + let mut program_state = lock_state(&program_state).map_err(|e| e.to_string())?; let temperature = sensors::get_temperature(&program_state.config).map_err(|e| e.to_string())?; let soil_moisture = sensors::get_soil_moisture(&program_state.config).map_err(|e| e.to_string())?; diff --git a/src/state.rs b/src/state.rs index 7c49982..bd60e35 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, MutexGuard}; use crate::{config::Configuration, error::GenericResult, history::History, io}; @@ -9,6 +9,16 @@ pub struct ProgramState { pub history: History, } +fn lock_err(e: T) -> String { + format!("{}", e) +} + +pub fn lock_state( + program_state: &ProgramStateShared, +) -> GenericResult> { + Ok(program_state.lock().map_err(lock_err)?) +} + pub fn init_state(config: Configuration) -> GenericResult { let relay = io::Relay::new(&config)?; let history = History::load().unwrap_or_default();