diff --git a/emgauwa-core/src/app_state.rs b/emgauwa-core/src/app_state.rs index 2441c01..feacab9 100644 --- a/emgauwa-core/src/app_state.rs +++ b/emgauwa-core/src/app_state.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use actix::{Actor, Context, Handler, Message, Recipient}; use emgauwa_lib::errors::EmgauwaError; use emgauwa_lib::models::Controller; -use emgauwa_lib::types::{ControllerUid, ControllerWsAction}; +use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RelayStates}; use futures::executor::block_on; use sqlx::{Pool, Sqlite}; @@ -20,6 +20,13 @@ pub struct ConnectController { pub controller: Controller, } +#[derive(Message)] +#[rtype(result = "()")] +pub struct UpdateRelayStates { + pub controller_uid: ControllerUid, + pub relay_states: RelayStates, +} + #[derive(Message)] #[rtype(result = "Result<(), EmgauwaError>")] pub struct Action { @@ -80,6 +87,16 @@ impl Handler for AppState { } } +impl Handler for AppState { + type Result = (); + + fn handle(&mut self, msg: UpdateRelayStates, _ctx: &mut Self::Context) -> Self::Result { + if let Some((controller, _)) = self.connected_controllers.get_mut(&msg.controller_uid) { + controller.apply_relay_states(&msg.relay_states); + } + } +} + impl Handler for AppState { type Result = Result<(), EmgauwaError>; diff --git a/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs b/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs index abdbbe2..335f7b1 100644 --- a/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs +++ b/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs @@ -1,15 +1,14 @@ -use std::hash::{Hash, Hasher}; - use actix::{Actor, AsyncContext}; use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule}; use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; use emgauwa_lib::models::{Controller, FromDbModel}; use emgauwa_lib::types::{ControllerUid, RelayStates}; +use emgauwa_lib::utils; use futures::executor::block_on; use sqlx::pool::PoolConnection; use sqlx::Sqlite; -use crate::app_state::ConnectController; +use crate::app_state::{ConnectController, UpdateRelayStates}; use crate::handlers::v1::ws::controllers::ControllerWs; impl ControllerWs { @@ -87,15 +86,18 @@ impl ControllerWs { pub fn handle_relay_states( &mut self, - ctx: &mut ::Context, controller_uid: ControllerUid, relay_states: RelayStates, ) -> Result<(), EmgauwaError> { log::debug!( - "Received relay states: {:?} for {}", - relay_states, + "Received relay states: {} for {}", + utils::printable_relay_states(&relay_states), controller_uid ); + block_on(self.app_state.send(UpdateRelayStates { + controller_uid, + relay_states, + }))?; Ok(()) } } diff --git a/emgauwa-core/src/handlers/v1/ws/controllers/mod.rs b/emgauwa-core/src/handlers/v1/ws/controllers/mod.rs index 58d8ab6..fc95439 100644 --- a/emgauwa-core/src/handlers/v1/ws/controllers/mod.rs +++ b/emgauwa-core/src/handlers/v1/ws/controllers/mod.rs @@ -55,7 +55,7 @@ impl ControllerWs { let action_res = match action { ControllerWsAction::Register(controller) => self.handle_register(conn, ctx, controller), ControllerWsAction::RelayStates((controller_uid, relay_states)) => { - self.handle_relay_states(ctx, controller_uid, relay_states) + self.handle_relay_states(controller_uid, relay_states) } _ => Ok(()), }; diff --git a/emgauwa-lib/src/utils.rs b/emgauwa-lib/src/utils.rs index 0ce1079..776259b 100644 --- a/emgauwa-lib/src/utils.rs +++ b/emgauwa-lib/src/utils.rs @@ -8,7 +8,7 @@ use simple_logger::SimpleLogger; use crate::errors::EmgauwaError; use crate::settings::Permissions; -use crate::types::Weekday; +use crate::types::{RelayStates, Weekday}; pub fn init_logging(level: &str) -> Result<(), EmgauwaError> { @@ -100,3 +100,18 @@ pub fn get_weekday() -> Weekday { .number_from_monday() - 1) as Weekday } + +pub fn printable_relay_states(relay_states: &RelayStates) -> String { + let mut relay_debug = String::new(); + relay_states.iter().for_each(|state| { + relay_debug.push_str(&format!( + "{}", + match state { + Some(true) => "+", + Some(false) => "-", + None => "?", + } + )); + }); + relay_debug +}