Add AppState to Controller and split up models

This commit is contained in:
Tobias Reisinger 2023-12-07 01:32:20 +01:00
parent 8dc9072fe8
commit 83c1f033d5
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
11 changed files with 261 additions and 150 deletions
emgauwa-controller/src/ws

View file

@ -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(())
}