Refactor the utils (remove them)
This commit is contained in:
		
							parent
							
								
									bb982c4444
								
							
						
					
					
						commit
						b065c8dd97
					
				
					 6 changed files with 53 additions and 110 deletions
				
			
		| 
						 | 
					@ -1,7 +1,7 @@
 | 
				
			||||||
use std::sync::Arc;
 | 
					use std::sync::Arc;
 | 
				
			||||||
use std::time::{Duration, Instant};
 | 
					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::constants;
 | 
				
			||||||
use emgauwa_common::db::DbSchedule;
 | 
					use emgauwa_common::db::DbSchedule;
 | 
				
			||||||
use emgauwa_common::errors::EmgauwaError;
 | 
					use emgauwa_common::errors::EmgauwaError;
 | 
				
			||||||
| 
						 | 
					@ -84,6 +84,20 @@ impl AppState {
 | 
				
			||||||
	pub fn notify_relay_change(&self) {
 | 
						pub fn notify_relay_change(&self) {
 | 
				
			||||||
		self.relay_notifier.notify_one();
 | 
							self.relay_notifier.notify_one();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn trigger_reload(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
 | 
				
			||||||
 | 
							app_state
 | 
				
			||||||
 | 
								.send(Reload {})
 | 
				
			||||||
 | 
								.await
 | 
				
			||||||
 | 
								.map_err(EmgauwaError::from)?
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn get_this(app_state: &Addr<AppState>) -> Result<Controller, EmgauwaError> {
 | 
				
			||||||
 | 
							app_state
 | 
				
			||||||
 | 
								.send(GetThis {})
 | 
				
			||||||
 | 
								.await
 | 
				
			||||||
 | 
								.map_err(EmgauwaError::from)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Actor for AppState {
 | 
					impl Actor for AppState {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,7 +18,6 @@ mod drivers;
 | 
				
			||||||
mod errors;
 | 
					mod errors;
 | 
				
			||||||
mod relay_loop;
 | 
					mod relay_loop;
 | 
				
			||||||
mod settings;
 | 
					mod settings;
 | 
				
			||||||
mod utils;
 | 
					 | 
				
			||||||
mod ws;
 | 
					mod ws;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn create_this_controller(
 | 
					async fn create_this_controller(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,10 +10,9 @@ use emgauwa_common::utils::printable_relay_states;
 | 
				
			||||||
use futures::pin_mut;
 | 
					use futures::pin_mut;
 | 
				
			||||||
use tokio::time;
 | 
					use tokio::time;
 | 
				
			||||||
use tokio::time::timeout;
 | 
					use tokio::time::timeout;
 | 
				
			||||||
use utils::app_state_get_controller_notifier;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::app_state::AppState;
 | 
					use crate::app_state::AppState;
 | 
				
			||||||
use crate::utils;
 | 
					use crate::app_state;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub async fn run_relays_loop(app_state: Addr<AppState>) {
 | 
					pub async fn run_relays_loop(app_state: Addr<AppState>) {
 | 
				
			||||||
	log::debug!("Spawned relays loop");
 | 
						log::debug!("Spawned relays loop");
 | 
				
			||||||
| 
						 | 
					@ -27,10 +26,12 @@ pub async fn run_relays_loop(app_state: Addr<AppState>) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
 | 
					async fn run_relays(app_state: &Addr<AppState>) -> 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 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;
 | 
						let mut duration_override = None;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -52,7 +53,7 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if changed {
 | 
							if changed {
 | 
				
			||||||
			log::debug!("Reloading controller in relay loop");
 | 
								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!(
 | 
							log::debug!(
 | 
				
			||||||
| 
						 | 
					@ -97,8 +98,13 @@ async fn calc_relay_states(
 | 
				
			||||||
					|| relay.check_pulsing(&now.instant).is_some(),
 | 
										|| 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(
 | 
					fn get_next_duration(
 | 
				
			||||||
| 
						 | 
					@ -136,7 +142,7 @@ async fn check_weekday(
 | 
				
			||||||
	if current_weekday.ne(last_weekday) {
 | 
						if current_weekday.ne(last_weekday) {
 | 
				
			||||||
		log::debug!("Weekday changed");
 | 
							log::debug!("Weekday changed");
 | 
				
			||||||
		*last_weekday = current_weekday;
 | 
							*last_weekday = current_weekday;
 | 
				
			||||||
		utils::app_state_reload(app_state).await?;
 | 
							AppState::trigger_reload(app_state).await?;
 | 
				
			||||||
		*changed = true;
 | 
							*changed = true;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										83
									
								
								src/utils.rs
									
										
									
									
									
								
							
							
						
						
									
										83
									
								
								src/utils.rs
									
										
									
									
									
								
							| 
						 | 
					@ -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<AppState>) -> Result<Controller, EmgauwaError> {
 | 
					 | 
				
			||||||
	app_state
 | 
					 | 
				
			||||||
		.send(app_state::GetThis {})
 | 
					 | 
				
			||||||
		.await
 | 
					 | 
				
			||||||
		.map_err(EmgauwaError::from)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub async fn app_state_get_relay_notifier(
 | 
					 | 
				
			||||||
	app_state: &Addr<AppState>,
 | 
					 | 
				
			||||||
) -> Result<Arc<Notify>, EmgauwaError> {
 | 
					 | 
				
			||||||
	app_state
 | 
					 | 
				
			||||||
		.send(app_state::GetRelayNotifier {})
 | 
					 | 
				
			||||||
		.await
 | 
					 | 
				
			||||||
		.map_err(EmgauwaError::from)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub async fn app_state_get_controller_notifier(
 | 
					 | 
				
			||||||
	app_state: &Addr<AppState>,
 | 
					 | 
				
			||||||
) -> Result<Arc<Notify>, EmgauwaError> {
 | 
					 | 
				
			||||||
	app_state
 | 
					 | 
				
			||||||
		.send(app_state::GetControllerNotifier {})
 | 
					 | 
				
			||||||
		.await
 | 
					 | 
				
			||||||
		.map_err(EmgauwaError::from)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub async fn app_state_reload(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
 | 
					 | 
				
			||||||
	app_state
 | 
					 | 
				
			||||||
		.send(app_state::Reload {})
 | 
					 | 
				
			||||||
		.await
 | 
					 | 
				
			||||||
		.map_err(EmgauwaError::from)?
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub async fn app_state_update_relay_states(
 | 
					 | 
				
			||||||
	app_state: &Addr<AppState>,
 | 
					 | 
				
			||||||
	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<AppState>,
 | 
					 | 
				
			||||||
	relay_number: i64,
 | 
					 | 
				
			||||||
	duration: Option<u32>,
 | 
					 | 
				
			||||||
) -> 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<AppState>,
 | 
					 | 
				
			||||||
	relay_number: i64,
 | 
					 | 
				
			||||||
	schedule: Option<DbSchedule>,
 | 
					 | 
				
			||||||
	weekday: Weekday,
 | 
					 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					 | 
				
			||||||
	app_state
 | 
					 | 
				
			||||||
		.send(app_state::RelayOverrideSchedule {
 | 
					 | 
				
			||||||
			relay_number,
 | 
					 | 
				
			||||||
			weekday,
 | 
					 | 
				
			||||||
			schedule,
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
		.await
 | 
					 | 
				
			||||||
		.map_err(EmgauwaError::from)?
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -10,8 +10,7 @@ use emgauwa_common::models::{Controller, Relay};
 | 
				
			||||||
use emgauwa_common::types::{ControllerWsAction, ScheduleUid};
 | 
					use emgauwa_common::types::{ControllerWsAction, ScheduleUid};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::app_state::AppState;
 | 
					use crate::app_state::AppState;
 | 
				
			||||||
use crate::utils;
 | 
					use crate::app_state;
 | 
				
			||||||
use crate::utils::app_state_get_this;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub async fn handle_message(
 | 
					pub async fn handle_message(
 | 
				
			||||||
	pool: Pool<Sqlite>,
 | 
						pool: Pool<Sqlite>,
 | 
				
			||||||
| 
						 | 
					@ -53,7 +52,7 @@ pub async fn handle_action(
 | 
				
			||||||
	app_state: &Addr<AppState>,
 | 
						app_state: &Addr<AppState>,
 | 
				
			||||||
	action: ControllerWsAction,
 | 
						action: ControllerWsAction,
 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					) -> Result<(), EmgauwaError> {
 | 
				
			||||||
	let this = app_state_get_this(app_state).await?;
 | 
						let this = AppState::get_this(app_state).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	match action {
 | 
						match action {
 | 
				
			||||||
		ControllerWsAction::Controller(controller) => {
 | 
							ControllerWsAction::Controller(controller) => {
 | 
				
			||||||
| 
						 | 
					@ -67,7 +66,7 @@ pub async fn handle_action(
 | 
				
			||||||
		_ => return Ok(()),
 | 
							_ => return Ok(()),
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	utils::app_state_reload(app_state).await
 | 
						AppState::trigger_reload(app_state).await
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn handle_controller(
 | 
					async fn handle_controller(
 | 
				
			||||||
| 
						 | 
					@ -154,13 +153,13 @@ async fn handle_relays(
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		utils::app_state_relay_override_schedule(
 | 
							app_state
 | 
				
			||||||
			app_state,
 | 
								.send(app_state::RelayOverrideSchedule {
 | 
				
			||||||
			relay.r.number,
 | 
									relay_number: relay.r.number,
 | 
				
			||||||
			relay.override_schedule.clone(),
 | 
									schedule: relay.override_schedule.clone(),
 | 
				
			||||||
			relay.override_schedule_weekday,
 | 
									weekday: relay.override_schedule_weekday,
 | 
				
			||||||
		)
 | 
								})
 | 
				
			||||||
		.await?;
 | 
								.await??;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		DbJunctionRelaySchedule::set_schedules(conn, &db_relay, schedules.iter().collect()).await?;
 | 
							DbJunctionRelaySchedule::set_schedules(conn, &db_relay, schedules.iter().collect()).await?;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -170,8 +169,13 @@ async fn handle_relays(
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn handle_relay_pulse(
 | 
					async fn handle_relay_pulse(
 | 
				
			||||||
	app_state: &Addr<AppState>,
 | 
						app_state: &Addr<AppState>,
 | 
				
			||||||
	relay_num: i64,
 | 
						relay_number: i64,
 | 
				
			||||||
	duration: Option<u32>,
 | 
						duration: Option<u32>,
 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					) -> Result<(), EmgauwaError> {
 | 
				
			||||||
	utils::app_state_relay_pulse(app_state, relay_num, duration).await
 | 
						app_state
 | 
				
			||||||
 | 
							.send(app_state::RelayPulse {
 | 
				
			||||||
 | 
								relay_number,
 | 
				
			||||||
 | 
								duration,
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
							.await?
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,9 +9,9 @@ use sqlx::{Pool, Sqlite};
 | 
				
			||||||
use tokio::time;
 | 
					use tokio::time;
 | 
				
			||||||
use tokio_tungstenite::connect_async;
 | 
					use tokio_tungstenite::connect_async;
 | 
				
			||||||
use tokio_tungstenite::tungstenite::Message;
 | 
					use tokio_tungstenite::tungstenite::Message;
 | 
				
			||||||
 | 
					use crate::app_state;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::app_state::AppState;
 | 
					use crate::app_state::AppState;
 | 
				
			||||||
use crate::utils::{app_state_get_relay_notifier, app_state_get_this};
 | 
					 | 
				
			||||||
use crate::ws::handlers::handle_message;
 | 
					use crate::ws::handlers::handle_message;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub async fn run_ws_loop(pool: Pool<Sqlite>, app_state: Addr<AppState>, url: String) {
 | 
					pub async fn run_ws_loop(pool: Pool<Sqlite>, app_state: Addr<AppState>, url: String) {
 | 
				
			||||||
| 
						 | 
					@ -43,7 +43,7 @@ async fn run_websocket(
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			let (mut write, read) = ws_stream.split();
 | 
								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)?;
 | 
								let ws_action_json = serde_json::to_string(&ws_action)?;
 | 
				
			||||||
			if let Err(err) = write.send(Message::text(ws_action_json)).await {
 | 
								if let Err(err) = write.send(Message::text(ws_action_json)).await {
 | 
				
			||||||
| 
						 | 
					@ -73,11 +73,14 @@ async fn read_app_state(
 | 
				
			||||||
	app_state: Addr<AppState>,
 | 
						app_state: Addr<AppState>,
 | 
				
			||||||
	tx: futures_channel::mpsc::UnboundedSender<Message>,
 | 
						tx: futures_channel::mpsc::UnboundedSender<Message>,
 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					) -> Result<(), EmgauwaError> {
 | 
				
			||||||
	let notifier = &*app_state_get_relay_notifier(&app_state).await?;
 | 
						let notifier = &*app_state
 | 
				
			||||||
 | 
							.send(app_state::GetRelayNotifier {})
 | 
				
			||||||
 | 
							.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	loop {
 | 
						loop {
 | 
				
			||||||
		notifier.notified().await;
 | 
							notifier.notified().await;
 | 
				
			||||||
		log::debug!("Relay change detected");
 | 
							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 relay_states = this.get_relay_states();
 | 
				
			||||||
		let ws_action = ControllerWsAction::RelayStates((this.c.uid, relay_states));
 | 
							let ws_action = ControllerWsAction::RelayStates((this.c.uid, relay_states));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue