Add handler for relay states
This commit is contained in:
		
							parent
							
								
									82f2d49dc6
								
							
						
					
					
						commit
						55617dbd7c
					
				
					 8 changed files with 53 additions and 20 deletions
				
			
		| 
						 | 
				
			
			@ -3,6 +3,7 @@ use std::sync::Arc;
 | 
			
		|||
use actix::{Actor, Context, Handler, Message};
 | 
			
		||||
use emgauwa_lib::errors::EmgauwaError;
 | 
			
		||||
use emgauwa_lib::models::Controller;
 | 
			
		||||
use emgauwa_lib::types::RelayStates;
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
use tokio::sync::Notify;
 | 
			
		||||
| 
						 | 
				
			
			@ -13,8 +14,8 @@ pub struct Reload {}
 | 
			
		|||
 | 
			
		||||
#[derive(Message)]
 | 
			
		||||
#[rtype(result = "()")]
 | 
			
		||||
pub struct UpdateRelaysOn {
 | 
			
		||||
	pub relays_are_on: Vec<Option<bool>>,
 | 
			
		||||
pub struct UpdateRelayStates {
 | 
			
		||||
	pub relay_states: RelayStates,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Message)]
 | 
			
		||||
| 
						 | 
				
			
			@ -74,17 +75,11 @@ impl Handler<Reload> for AppState {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Handler<UpdateRelaysOn> for AppState {
 | 
			
		||||
impl Handler<UpdateRelayStates> for AppState {
 | 
			
		||||
	type Result = ();
 | 
			
		||||
 | 
			
		||||
	fn handle(&mut self, msg: UpdateRelaysOn, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
		self.this
 | 
			
		||||
			.relays
 | 
			
		||||
			.iter_mut()
 | 
			
		||||
			.zip(msg.relays_are_on.iter())
 | 
			
		||||
			.for_each(|(relay, is_on)| {
 | 
			
		||||
				relay.is_on = *is_on;
 | 
			
		||||
			});
 | 
			
		||||
	fn handle(&mut self, msg: UpdateRelayStates, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
		self.this.apply_relay_states(&msg.relay_states);
 | 
			
		||||
 | 
			
		||||
		self.notify_relay_change();
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,6 +5,7 @@ use chrono::Local;
 | 
			
		|||
use emgauwa_lib::constants::RELAYS_RETRY_TIMEOUT;
 | 
			
		||||
use emgauwa_lib::errors::EmgauwaError;
 | 
			
		||||
use emgauwa_lib::models::Controller;
 | 
			
		||||
use emgauwa_lib::types::RelayStates;
 | 
			
		||||
use futures::pin_mut;
 | 
			
		||||
use tokio::time;
 | 
			
		||||
use tokio::time::timeout;
 | 
			
		||||
| 
						 | 
				
			
			@ -30,7 +31,7 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
 | 
			
		|||
 | 
			
		||||
	let mut last_weekday = emgauwa_lib::utils::get_weekday();
 | 
			
		||||
	let mut this = utils::app_state_get_this(app_state).await?;
 | 
			
		||||
	let mut relay_states: Vec<Option<bool>> = Vec::new();
 | 
			
		||||
	let mut relay_states: RelayStates = Vec::new();
 | 
			
		||||
	init_relay_states(&mut relay_states, &this);
 | 
			
		||||
 | 
			
		||||
	loop {
 | 
			
		||||
| 
						 | 
				
			
			@ -80,7 +81,7 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn init_relay_states(relay_states: &mut Vec<Option<bool>>, this: &Controller) {
 | 
			
		||||
fn init_relay_states(relay_states: &mut RelayStates, this: &Controller) {
 | 
			
		||||
	relay_states.clear();
 | 
			
		||||
	for _ in 0..this.c.relay_count {
 | 
			
		||||
		relay_states.push(None);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,7 @@ use std::sync::Arc;
 | 
			
		|||
use actix::Addr;
 | 
			
		||||
use emgauwa_lib::errors::EmgauwaError;
 | 
			
		||||
use emgauwa_lib::models::Controller;
 | 
			
		||||
use emgauwa_lib::types::RelayStates;
 | 
			
		||||
use tokio::sync::Notify;
 | 
			
		||||
 | 
			
		||||
use crate::app_state;
 | 
			
		||||
| 
						 | 
				
			
			@ -42,11 +43,11 @@ pub async fn app_state_reload(app_state: &Addr<AppState>) -> Result<(), EmgauwaE
 | 
			
		|||
 | 
			
		||||
pub async fn app_state_update_relays_on(
 | 
			
		||||
	app_state: &Addr<AppState>,
 | 
			
		||||
	relay_states: Vec<Option<bool>>,
 | 
			
		||||
	relay_states: RelayStates,
 | 
			
		||||
) -> Result<(), EmgauwaError> {
 | 
			
		||||
	app_state
 | 
			
		||||
		.send(app_state::UpdateRelaysOn {
 | 
			
		||||
			relays_are_on: relay_states,
 | 
			
		||||
		.send(app_state::UpdateRelayStates {
 | 
			
		||||
			relay_states: relay_states,
 | 
			
		||||
		})
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(EmgauwaError::from)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -78,7 +78,9 @@ async fn read_app_state(
 | 
			
		|||
	loop {
 | 
			
		||||
		notifier.notified().await;
 | 
			
		||||
		log::debug!("Relay change detected");
 | 
			
		||||
		let ws_action = ControllerWsAction::Register(app_state_get_this(&app_state).await?);
 | 
			
		||||
		let this = app_state_get_this(&app_state).await?;
 | 
			
		||||
		let relay_states = this.get_relay_states();
 | 
			
		||||
		let ws_action = ControllerWsAction::RelayStates((this.c.uid, relay_states));
 | 
			
		||||
 | 
			
		||||
		let ws_action_json = serde_json::to_string(&ws_action)?;
 | 
			
		||||
		tx.unbounded_send(Message::text(ws_action_json))
 | 
			
		||||
| 
						 | 
				
			
			@ -122,9 +124,6 @@ async fn handle_message(
 | 
			
		|||
				log::error!("Error deserializing action: {:?}", e);
 | 
			
		||||
			}
 | 
			
		||||
		},
 | 
			
		||||
		Message::Ping(_) => {
 | 
			
		||||
			log::debug!("Received ping");
 | 
			
		||||
		}
 | 
			
		||||
		_ => {}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue