Add handler in AppState to handle relay states
This commit is contained in:
		
							parent
							
								
									55617dbd7c
								
							
						
					
					
						commit
						27739e2b71
					
				
					 4 changed files with 43 additions and 9 deletions
				
			
		| 
						 | 
				
			
			@ -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<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 {
 | 
			
		||||
	type Result = Result<(), EmgauwaError>;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 <ControllerWs as Actor>::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(())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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(()),
 | 
			
		||||
		};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue