Add handler in AppState to handle relay states

This commit is contained in:
Tobias Reisinger 2024-04-25 17:21:54 +02:00
parent 55617dbd7c
commit 27739e2b71
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
4 changed files with 43 additions and 9 deletions

View file

@ -3,7 +3,7 @@ use std::collections::HashMap;
use actix::{Actor, Context, Handler, Message, Recipient}; use actix::{Actor, Context, Handler, Message, Recipient};
use emgauwa_lib::errors::EmgauwaError; use emgauwa_lib::errors::EmgauwaError;
use emgauwa_lib::models::Controller; use emgauwa_lib::models::Controller;
use emgauwa_lib::types::{ControllerUid, ControllerWsAction}; use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RelayStates};
use futures::executor::block_on; use futures::executor::block_on;
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
@ -20,6 +20,13 @@ pub struct ConnectController {
pub controller: Controller, pub controller: Controller,
} }
#[derive(Message)]
#[rtype(result = "()")]
pub struct UpdateRelayStates {
pub controller_uid: ControllerUid,
pub relay_states: RelayStates,
}
#[derive(Message)] #[derive(Message)]
#[rtype(result = "Result<(), EmgauwaError>")] #[rtype(result = "Result<(), EmgauwaError>")]
pub struct Action { pub struct Action {
@ -80,6 +87,16 @@ impl Handler<ConnectController> for AppState {
} }
} }
impl Handler<UpdateRelayStates> 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<Action> for AppState { impl Handler<Action> for AppState {
type Result = Result<(), EmgauwaError>; type Result = Result<(), EmgauwaError>;

View file

@ -1,15 +1,14 @@
use std::hash::{Hash, Hasher};
use actix::{Actor, AsyncContext}; use actix::{Actor, AsyncContext};
use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule}; use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
use emgauwa_lib::models::{Controller, FromDbModel}; use emgauwa_lib::models::{Controller, FromDbModel};
use emgauwa_lib::types::{ControllerUid, RelayStates}; use emgauwa_lib::types::{ControllerUid, RelayStates};
use emgauwa_lib::utils;
use futures::executor::block_on; use futures::executor::block_on;
use sqlx::pool::PoolConnection; use sqlx::pool::PoolConnection;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::app_state::ConnectController; use crate::app_state::{ConnectController, UpdateRelayStates};
use crate::handlers::v1::ws::controllers::ControllerWs; use crate::handlers::v1::ws::controllers::ControllerWs;
impl ControllerWs { impl ControllerWs {
@ -87,15 +86,18 @@ impl ControllerWs {
pub fn handle_relay_states( pub fn handle_relay_states(
&mut self, &mut self,
ctx: &mut <ControllerWs as Actor>::Context,
controller_uid: ControllerUid, controller_uid: ControllerUid,
relay_states: RelayStates, relay_states: RelayStates,
) -> Result<(), EmgauwaError> { ) -> Result<(), EmgauwaError> {
log::debug!( log::debug!(
"Received relay states: {:?} for {}", "Received relay states: {} for {}",
relay_states, utils::printable_relay_states(&relay_states),
controller_uid controller_uid
); );
block_on(self.app_state.send(UpdateRelayStates {
controller_uid,
relay_states,
}))?;
Ok(()) Ok(())
} }
} }

View file

@ -55,7 +55,7 @@ impl ControllerWs {
let action_res = match action { let action_res = match action {
ControllerWsAction::Register(controller) => self.handle_register(conn, ctx, controller), ControllerWsAction::Register(controller) => self.handle_register(conn, ctx, controller),
ControllerWsAction::RelayStates((controller_uid, relay_states)) => { ControllerWsAction::RelayStates((controller_uid, relay_states)) => {
self.handle_relay_states(ctx, controller_uid, relay_states) self.handle_relay_states(controller_uid, relay_states)
} }
_ => Ok(()), _ => Ok(()),
}; };

View file

@ -8,7 +8,7 @@ use simple_logger::SimpleLogger;
use crate::errors::EmgauwaError; use crate::errors::EmgauwaError;
use crate::settings::Permissions; use crate::settings::Permissions;
use crate::types::Weekday; use crate::types::{RelayStates, Weekday};
pub fn init_logging(level: &str) -> Result<(), EmgauwaError> { pub fn init_logging(level: &str) -> Result<(), EmgauwaError> {
@ -100,3 +100,18 @@ pub fn get_weekday() -> Weekday {
.number_from_monday() .number_from_monday()
- 1) as Weekday - 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
}