From b065c8dd972df43516d49a57db228f1ad2ba6648 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Wed, 29 May 2024 00:59:08 +0200 Subject: [PATCH] Refactor the utils (remove them) --- src/app_state.rs | 16 ++++++++- src/main.rs | 1 - src/relay_loop.rs | 22 +++++++----- src/utils.rs | 83 ---------------------------------------------- src/ws/handlers.rs | 30 +++++++++-------- src/ws/mod.rs | 11 +++--- 6 files changed, 53 insertions(+), 110 deletions(-) delete mode 100644 src/utils.rs diff --git a/src/app_state.rs b/src/app_state.rs index 94f05eb..d53de02 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use std::time::{Duration, Instant}; -use actix::{Actor, Context, Handler, Message}; +use actix::{Actor, Addr, Context, Handler, Message}; use emgauwa_common::constants; use emgauwa_common::db::DbSchedule; use emgauwa_common::errors::EmgauwaError; @@ -84,6 +84,20 @@ impl AppState { pub fn notify_relay_change(&self) { self.relay_notifier.notify_one(); } + + pub async fn trigger_reload(app_state: &Addr) -> Result<(), EmgauwaError> { + app_state + .send(Reload {}) + .await + .map_err(EmgauwaError::from)? + } + + pub async fn get_this(app_state: &Addr) -> Result { + app_state + .send(GetThis {}) + .await + .map_err(EmgauwaError::from) + } } impl Actor for AppState { diff --git a/src/main.rs b/src/main.rs index f9d70eb..0c7eee0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,6 @@ mod drivers; mod errors; mod relay_loop; mod settings; -mod utils; mod ws; async fn create_this_controller( diff --git a/src/relay_loop.rs b/src/relay_loop.rs index 753dfba..d5f1676 100644 --- a/src/relay_loop.rs +++ b/src/relay_loop.rs @@ -10,10 +10,9 @@ use emgauwa_common::utils::printable_relay_states; use futures::pin_mut; use tokio::time; use tokio::time::timeout; -use utils::app_state_get_controller_notifier; use crate::app_state::AppState; -use crate::utils; +use crate::app_state; pub async fn run_relays_loop(app_state: Addr) { log::debug!("Spawned relays loop"); @@ -27,10 +26,12 @@ pub async fn run_relays_loop(app_state: Addr) { } async fn run_relays(app_state: &Addr) -> Result<(), EmgauwaError> { - let notifier = &*app_state_get_controller_notifier(app_state).await?; + let notifier = &*app_state + .send(app_state::GetControllerNotifier {}) + .await?; let mut last_weekday = emgauwa_common::utils::get_weekday(); - let mut this = utils::app_state_get_this(app_state).await?; + let mut this = AppState::get_this(app_state).await?; let mut duration_override = None; @@ -52,7 +53,7 @@ async fn run_relays(app_state: &Addr) -> Result<(), EmgauwaError> { if changed { log::debug!("Reloading controller in relay loop"); - this = utils::app_state_get_this(app_state).await?; + this = AppState::get_this(app_state).await?; } log::debug!( @@ -97,8 +98,13 @@ async fn calc_relay_states( || relay.check_pulsing(&now.instant).is_some(), ); }); - let relay_states = this.get_relay_states(); - utils::app_state_update_relay_states(app_state, relay_states.clone()).await + + app_state + .send(app_state::UpdateRelayStates { + relay_states: this.get_relay_states() + }) + .await + .map_err(EmgauwaError::from) } fn get_next_duration( @@ -136,7 +142,7 @@ async fn check_weekday( if current_weekday.ne(last_weekday) { log::debug!("Weekday changed"); *last_weekday = current_weekday; - utils::app_state_reload(app_state).await?; + AppState::trigger_reload(app_state).await?; *changed = true; } diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index 3ea7854..0000000 --- a/src/utils.rs +++ /dev/null @@ -1,83 +0,0 @@ -use std::sync::Arc; - -use actix::Addr; -use emgauwa_common::db::DbSchedule; -use emgauwa_common::errors::EmgauwaError; -use emgauwa_common::models::Controller; -use emgauwa_common::types::{RelayStates, Weekday}; -use tokio::sync::Notify; - -use crate::app_state; -use crate::app_state::AppState; - -pub async fn app_state_get_this(app_state: &Addr) -> Result { - app_state - .send(app_state::GetThis {}) - .await - .map_err(EmgauwaError::from) -} - -pub async fn app_state_get_relay_notifier( - app_state: &Addr, -) -> Result, EmgauwaError> { - app_state - .send(app_state::GetRelayNotifier {}) - .await - .map_err(EmgauwaError::from) -} - -pub async fn app_state_get_controller_notifier( - app_state: &Addr, -) -> Result, EmgauwaError> { - app_state - .send(app_state::GetControllerNotifier {}) - .await - .map_err(EmgauwaError::from) -} - -pub async fn app_state_reload(app_state: &Addr) -> Result<(), EmgauwaError> { - app_state - .send(app_state::Reload {}) - .await - .map_err(EmgauwaError::from)? -} - -pub async fn app_state_update_relay_states( - app_state: &Addr, - relay_states: RelayStates, -) -> Result<(), EmgauwaError> { - app_state - .send(app_state::UpdateRelayStates { relay_states }) - .await - .map_err(EmgauwaError::from) -} - -pub async fn app_state_relay_pulse( - app_state: &Addr, - relay_number: i64, - duration: Option, -) -> Result<(), EmgauwaError> { - app_state - .send(app_state::RelayPulse { - relay_number, - duration, - }) - .await - .map_err(EmgauwaError::from)? -} - -pub async fn app_state_relay_override_schedule( - app_state: &Addr, - relay_number: i64, - schedule: Option, - weekday: Weekday, -) -> Result<(), EmgauwaError> { - app_state - .send(app_state::RelayOverrideSchedule { - relay_number, - weekday, - schedule, - }) - .await - .map_err(EmgauwaError::from)? -} diff --git a/src/ws/handlers.rs b/src/ws/handlers.rs index 1ad709a..97caded 100644 --- a/src/ws/handlers.rs +++ b/src/ws/handlers.rs @@ -10,8 +10,7 @@ use emgauwa_common::models::{Controller, Relay}; use emgauwa_common::types::{ControllerWsAction, ScheduleUid}; use crate::app_state::AppState; -use crate::utils; -use crate::utils::app_state_get_this; +use crate::app_state; pub async fn handle_message( pool: Pool, @@ -53,7 +52,7 @@ pub async fn handle_action( app_state: &Addr, action: ControllerWsAction, ) -> Result<(), EmgauwaError> { - let this = app_state_get_this(app_state).await?; + let this = AppState::get_this(app_state).await?; match action { ControllerWsAction::Controller(controller) => { @@ -67,7 +66,7 @@ pub async fn handle_action( _ => return Ok(()), }; - utils::app_state_reload(app_state).await + AppState::trigger_reload(app_state).await } async fn handle_controller( @@ -154,13 +153,13 @@ async fn handle_relays( ); } - utils::app_state_relay_override_schedule( - app_state, - relay.r.number, - relay.override_schedule.clone(), - relay.override_schedule_weekday, - ) - .await?; + app_state + .send(app_state::RelayOverrideSchedule { + relay_number: relay.r.number, + schedule: relay.override_schedule.clone(), + weekday: relay.override_schedule_weekday, + }) + .await??; DbJunctionRelaySchedule::set_schedules(conn, &db_relay, schedules.iter().collect()).await?; } @@ -170,8 +169,13 @@ async fn handle_relays( async fn handle_relay_pulse( app_state: &Addr, - relay_num: i64, + relay_number: i64, duration: Option, ) -> Result<(), EmgauwaError> { - utils::app_state_relay_pulse(app_state, relay_num, duration).await + app_state + .send(app_state::RelayPulse { + relay_number, + duration, + }) + .await? } diff --git a/src/ws/mod.rs b/src/ws/mod.rs index bdb7e07..5ee6229 100644 --- a/src/ws/mod.rs +++ b/src/ws/mod.rs @@ -9,9 +9,9 @@ use sqlx::{Pool, Sqlite}; use tokio::time; use tokio_tungstenite::connect_async; use tokio_tungstenite::tungstenite::Message; +use crate::app_state; use crate::app_state::AppState; -use crate::utils::{app_state_get_relay_notifier, app_state_get_this}; use crate::ws::handlers::handle_message; pub async fn run_ws_loop(pool: Pool, app_state: Addr, url: String) { @@ -43,7 +43,7 @@ async fn run_websocket( let (mut write, read) = ws_stream.split(); - let ws_action = ControllerWsAction::Register(app_state_get_this(app_state).await?); + let ws_action = ControllerWsAction::Register(AppState::get_this(app_state).await?); let ws_action_json = serde_json::to_string(&ws_action)?; if let Err(err) = write.send(Message::text(ws_action_json)).await { @@ -73,11 +73,14 @@ async fn read_app_state( app_state: Addr, tx: futures_channel::mpsc::UnboundedSender, ) -> Result<(), EmgauwaError> { - let notifier = &*app_state_get_relay_notifier(&app_state).await?; + let notifier = &*app_state + .send(app_state::GetRelayNotifier {}) + .await?; + loop { notifier.notified().await; log::debug!("Relay change detected"); - let this = app_state_get_this(&app_state).await?; + let this = AppState::get_this(&app_state).await?; let relay_states = this.get_relay_states(); let ws_action = ControllerWsAction::RelayStates((this.c.uid, relay_states));