Add AppState to Controller and split up models
This commit is contained in:
		
							parent
							
								
									8dc9072fe8
								
							
						
					
					
						commit
						83c1f033d5
					
				
					 11 changed files with 261 additions and 150 deletions
				
			
		| 
						 | 
				
			
			@ -7,6 +7,8 @@ authors = ["Tobias Reisinger <tobias@msrg.cc>"]
 | 
			
		|||
[dependencies]
 | 
			
		||||
emgauwa-lib = { path = "../emgauwa-lib" }
 | 
			
		||||
 | 
			
		||||
actix = "0.13"
 | 
			
		||||
 | 
			
		||||
tokio = { version = "1.34", features = ["io-std", "macros", "rt-multi-thread"] }
 | 
			
		||||
tokio-tungstenite = "0.20"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										48
									
								
								emgauwa-controller/src/app_state.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								emgauwa-controller/src/app_state.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,48 @@
 | 
			
		|||
use actix::{Actor, Context, Handler, Message};
 | 
			
		||||
use emgauwa_lib::errors::EmgauwaError;
 | 
			
		||||
use emgauwa_lib::models::Controller;
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
#[derive(Message)]
 | 
			
		||||
#[rtype(result = "Result<(), EmgauwaError>")]
 | 
			
		||||
pub struct Reload {}
 | 
			
		||||
 | 
			
		||||
#[derive(Message)]
 | 
			
		||||
#[rtype(result = "Controller")]
 | 
			
		||||
pub struct GetThis {}
 | 
			
		||||
 | 
			
		||||
pub struct AppState {
 | 
			
		||||
	pub pool: Pool<Sqlite>,
 | 
			
		||||
	pub this: Controller,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl AppState {
 | 
			
		||||
	pub fn new(pool: Pool<Sqlite>, this: Controller) -> AppState {
 | 
			
		||||
		AppState { pool, this }
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Actor for AppState {
 | 
			
		||||
	type Context = Context<Self>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Handler<Reload> for AppState {
 | 
			
		||||
	type Result = Result<(), EmgauwaError>;
 | 
			
		||||
 | 
			
		||||
	fn handle(&mut self, _msg: Reload, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
		let mut pool_conn = block_on(self.pool.acquire())?;
 | 
			
		||||
 | 
			
		||||
		self.this.reload(&mut pool_conn)?;
 | 
			
		||||
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Handler<GetThis> for AppState {
 | 
			
		||||
	type Result = Controller;
 | 
			
		||||
 | 
			
		||||
	fn handle(&mut self, _msg: GetThis, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
		self.this.clone()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,21 +1,24 @@
 | 
			
		|||
use actix::Actor;
 | 
			
		||||
use emgauwa_lib::constants::WEBSOCKET_RETRY_TIMEOUT;
 | 
			
		||||
use emgauwa_lib::db;
 | 
			
		||||
use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
 | 
			
		||||
use emgauwa_lib::errors::EmgauwaError;
 | 
			
		||||
use emgauwa_lib::models::{Controller, FromDbModel};
 | 
			
		||||
use emgauwa_lib::types::ControllerUid;
 | 
			
		||||
use emgauwa_lib::{db, utils};
 | 
			
		||||
use emgauwa_lib::utils::init_logging;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
use tokio::time;
 | 
			
		||||
use utils::init_logging;
 | 
			
		||||
 | 
			
		||||
use crate::relay_loop::run_relay_loop;
 | 
			
		||||
use crate::settings::Settings;
 | 
			
		||||
use crate::ws::run_websocket;
 | 
			
		||||
 | 
			
		||||
mod app_state;
 | 
			
		||||
mod driver;
 | 
			
		||||
mod relay_loop;
 | 
			
		||||
mod settings;
 | 
			
		||||
mod utils;
 | 
			
		||||
mod ws;
 | 
			
		||||
 | 
			
		||||
async fn create_this_controller(
 | 
			
		||||
| 
						 | 
				
			
			@ -55,7 +58,7 @@ async fn create_this_relay(
 | 
			
		|||
	Ok(relay)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
#[actix::main]
 | 
			
		||||
async fn main() -> Result<(), std::io::Error> {
 | 
			
		||||
	let settings = settings::init()?;
 | 
			
		||||
	init_logging(&settings.logging.level)?;
 | 
			
		||||
| 
						 | 
				
			
			@ -98,6 +101,10 @@ async fn main() -> Result<(), std::io::Error> {
 | 
			
		|||
		.await
 | 
			
		||||
		.map_err(EmgauwaError::from)?;
 | 
			
		||||
 | 
			
		||||
	let this = Controller::from_db_model(&mut conn, db_controller).map_err(EmgauwaError::from)?;
 | 
			
		||||
 | 
			
		||||
	let app_state = app_state::AppState::new(pool.clone(), this).start();
 | 
			
		||||
 | 
			
		||||
	let url = format!(
 | 
			
		||||
		"ws://{}:{}/api/v1/ws/controllers",
 | 
			
		||||
		settings.core.host, settings.core.port
 | 
			
		||||
| 
						 | 
				
			
			@ -106,14 +113,7 @@ async fn main() -> Result<(), std::io::Error> {
 | 
			
		|||
	tokio::spawn(run_relay_loop(settings));
 | 
			
		||||
 | 
			
		||||
	loop {
 | 
			
		||||
		let db_controller = db_controller
 | 
			
		||||
			.reload(&mut conn)
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(EmgauwaError::from)?;
 | 
			
		||||
		let this =
 | 
			
		||||
			Controller::from_db_model(&mut conn, db_controller).map_err(EmgauwaError::from)?;
 | 
			
		||||
 | 
			
		||||
		let run_result = run_websocket(pool.clone(), this.clone(), &url).await;
 | 
			
		||||
		let run_result = run_websocket(pool.clone(), &app_state, &url).await;
 | 
			
		||||
		if let Err(err) = run_result {
 | 
			
		||||
			log::error!("Error running websocket: {}", err);
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										9
									
								
								emgauwa-controller/src/utils.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								emgauwa-controller/src/utils.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
use actix::Addr;
 | 
			
		||||
use emgauwa_lib::errors::EmgauwaError;
 | 
			
		||||
use emgauwa_lib::models::Controller;
 | 
			
		||||
 | 
			
		||||
use crate::app_state::{AppState, GetThis};
 | 
			
		||||
 | 
			
		||||
pub async fn get_this(app_state: &Addr<AppState>) -> Result<Controller, EmgauwaError> {
 | 
			
		||||
	app_state.send(GetThis {}).await.map_err(EmgauwaError::from)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
use actix::Addr;
 | 
			
		||||
use emgauwa_lib::db::DbController;
 | 
			
		||||
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
 | 
			
		||||
use emgauwa_lib::models::Controller;
 | 
			
		||||
| 
						 | 
				
			
			@ -8,9 +9,13 @@ use sqlx::{Pool, Sqlite};
 | 
			
		|||
use tokio_tungstenite::tungstenite::Message;
 | 
			
		||||
use tokio_tungstenite::{connect_async, tungstenite};
 | 
			
		||||
 | 
			
		||||
use crate::app_state;
 | 
			
		||||
use crate::app_state::AppState;
 | 
			
		||||
use crate::utils::get_this;
 | 
			
		||||
 | 
			
		||||
pub async fn run_websocket(
 | 
			
		||||
	pool: Pool<Sqlite>,
 | 
			
		||||
	this: Controller,
 | 
			
		||||
	app_state: &Addr<AppState>,
 | 
			
		||||
	url: &str,
 | 
			
		||||
) -> Result<(), EmgauwaError> {
 | 
			
		||||
	match connect_async(url).await {
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +24,7 @@ pub async fn run_websocket(
 | 
			
		|||
 | 
			
		||||
			let (mut write, read) = ws_stream.split();
 | 
			
		||||
 | 
			
		||||
			let ws_action = ControllerWsAction::Register(this.clone());
 | 
			
		||||
			let ws_action = ControllerWsAction::Register(get_this(app_state).await?);
 | 
			
		||||
 | 
			
		||||
			let ws_action_json = serde_json::to_string(&ws_action)?;
 | 
			
		||||
			if let Err(err) = write.send(Message::text(ws_action_json)).await {
 | 
			
		||||
| 
						 | 
				
			
			@ -27,7 +32,7 @@ pub async fn run_websocket(
 | 
			
		|||
				return Ok(());
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			let read_handler = read.for_each(|msg| handle_message(pool.clone(), this.clone(), msg));
 | 
			
		||||
			let read_handler = read.for_each(|msg| handle_message(pool.clone(), app_state, msg));
 | 
			
		||||
 | 
			
		||||
			read_handler.await;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -42,7 +47,7 @@ pub async fn run_websocket(
 | 
			
		|||
 | 
			
		||||
async fn handle_message(
 | 
			
		||||
	pool: Pool<Sqlite>,
 | 
			
		||||
	this: Controller,
 | 
			
		||||
	app_state: &Addr<AppState>,
 | 
			
		||||
	message_result: Result<Message, tungstenite::Error>,
 | 
			
		||||
) {
 | 
			
		||||
	let msg = match message_result {
 | 
			
		||||
| 
						 | 
				
			
			@ -52,8 +57,8 @@ async fn handle_message(
 | 
			
		|||
			return;
 | 
			
		||||
		}
 | 
			
		||||
	};
 | 
			
		||||
	match msg {
 | 
			
		||||
		Message::Text(text) => match serde_json::from_str(&text) {
 | 
			
		||||
	if let Message::Text(text) = msg {
 | 
			
		||||
		match serde_json::from_str(&text) {
 | 
			
		||||
			Ok(action) => {
 | 
			
		||||
				log::debug!("Received action: {:?}", action);
 | 
			
		||||
				let mut pool_conn = match pool.acquire().await {
 | 
			
		||||
| 
						 | 
				
			
			@ -63,7 +68,7 @@ async fn handle_message(
 | 
			
		|||
						return;
 | 
			
		||||
					}
 | 
			
		||||
				};
 | 
			
		||||
				let action_res = handle_action(&mut pool_conn, this, action).await;
 | 
			
		||||
				let action_res = handle_action(&mut pool_conn, app_state, action).await;
 | 
			
		||||
				if let Err(e) = action_res {
 | 
			
		||||
					log::error!("Error handling action: {:?}", e);
 | 
			
		||||
				}
 | 
			
		||||
| 
						 | 
				
			
			@ -71,19 +76,18 @@ async fn handle_message(
 | 
			
		|||
			Err(e) => {
 | 
			
		||||
				log::error!("Error deserializing action: {:?}", e);
 | 
			
		||||
			}
 | 
			
		||||
		},
 | 
			
		||||
		_ => (),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn handle_action(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	this: Controller,
 | 
			
		||||
	app_state: &Addr<AppState>,
 | 
			
		||||
	action: ControllerWsAction,
 | 
			
		||||
) -> Result<(), EmgauwaError> {
 | 
			
		||||
	match action {
 | 
			
		||||
		ControllerWsAction::Controller(controller) => {
 | 
			
		||||
			handle_controller(conn, this, controller).await
 | 
			
		||||
			handle_controller(conn, app_state, controller).await
 | 
			
		||||
		}
 | 
			
		||||
		_ => Ok(()),
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -91,9 +95,10 @@ pub async fn handle_action(
 | 
			
		|||
 | 
			
		||||
pub async fn handle_controller(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	this: Controller,
 | 
			
		||||
	app_state: &Addr<AppState>,
 | 
			
		||||
	controller: Controller,
 | 
			
		||||
) -> Result<(), EmgauwaError> {
 | 
			
		||||
	let this = get_this(app_state).await?;
 | 
			
		||||
	if controller.c.uid != this.c.uid {
 | 
			
		||||
		return Err(EmgauwaError::Other(String::from(
 | 
			
		||||
			"Controller UID mismatch during update",
 | 
			
		||||
| 
						 | 
				
			
			@ -105,5 +110,7 @@ pub async fn handle_controller(
 | 
			
		|||
		.update(conn, controller.c.name.as_str(), this.c.relay_count)
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
	app_state.send(app_state::Reload {}).await??;
 | 
			
		||||
 | 
			
		||||
	Ok(())
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue