Refactor more stuff

This commit is contained in:
Tobias Reisinger 2023-12-04 23:59:26 +01:00
parent 5a7b2de0ea
commit 9394a1ae52
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
15 changed files with 167 additions and 86 deletions
emgauwa-core/src/handlers/v1

View file

@ -1,11 +1,15 @@
use actix::Addr;
use actix_web::{get, put, web, HttpResponse};
use emgauwa_lib::db::{DbController, DbRelay, DbTag};
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay};
use emgauwa_lib::types::ControllerUid;
use emgauwa_lib::types::{ControllerUid, ControllerWsAction};
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Sqlite};
use crate::app_state;
use crate::app_state::AppServer;
#[derive(Debug, Serialize, Deserialize)]
pub struct RequestRelay {
name: String,
@ -64,6 +68,7 @@ pub async fn index_for_controller(
#[get("/api/v1/controllers/{controller_id}/relays/{relay_num}")]
pub async fn show_for_controller(
pool: web::Data<Pool<Sqlite>>,
app_server: web::Data<Addr<AppServer>>,
path: web::Path<(String, i64)>,
) -> Result<HttpResponse, EmgauwaError> {
let mut pool_conn = pool.acquire().await?;

View file

@ -1,6 +1,6 @@
use actix::{Actor, AsyncContext};
use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
use emgauwa_lib::errors::DatabaseError;
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
use emgauwa_lib::models::{Controller, FromDbModel};
use futures::executor::block_on;
use sqlx::pool::PoolConnection;
@ -15,7 +15,7 @@ impl ControllerWs {
conn: &mut PoolConnection<Sqlite>,
ctx: &mut <ControllerWs as Actor>::Context,
controller: Controller,
) -> Result<(), DatabaseError> {
) -> Result<(), EmgauwaError> {
log::info!("Registering controller: {:?}", controller);
let c = &controller.c;
let controller_db = block_on(DbController::get_by_uid_or_create(
@ -60,10 +60,10 @@ impl ControllerWs {
let addr = ctx.address();
self.controller_uid = Some(controller_uid.clone());
self.app_server.do_send(ConnectController {
block_on(self.app_server.send(ConnectController {
address: addr.recipient(),
controller,
});
}))??;
Ok(())
}

View file

@ -6,7 +6,7 @@ use actix::{Actor, ActorContext, Addr, AsyncContext, Handler, StreamHandler};
use actix_web_actors::ws;
use actix_web_actors::ws::ProtocolError;
use emgauwa_lib::constants::{HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT};
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
use emgauwa_lib::errors::EmgauwaError;
use emgauwa_lib::types::{ControllerUid, ControllerWsAction};
use futures::executor::block_on;
use sqlx::pool::PoolConnection;
@ -14,6 +14,7 @@ use sqlx::{Pool, Sqlite};
use ws::Message;
use crate::app_state::{AppServer, DisconnectController};
use crate::utils::flatten_result;
pub struct ControllerWs {
pub pool: Pool<Sqlite>,
@ -31,9 +32,15 @@ impl Actor for ControllerWs {
fn stopped(&mut self, _ctx: &mut Self::Context) {
if let Some(controller_uid) = &self.controller_uid {
self.app_server.do_send(DisconnectController {
controller_uid: controller_uid.clone(),
})
let flat_res = flatten_result(
block_on(self.app_server.send(DisconnectController {
controller_uid: controller_uid.clone(),
}))
.map_err(EmgauwaError::from),
);
if let Err(err) = flat_res {
log::error!("Error disconnecting controller: {:?}", err);
}
}
}
}
@ -44,9 +51,10 @@ impl ControllerWs {
conn: &mut PoolConnection<Sqlite>,
ctx: &mut <ControllerWs as Actor>::Context,
action: ControllerWsAction,
) -> Result<(), DatabaseError> {
) -> Result<(), EmgauwaError> {
match action {
ControllerWsAction::Register(controller) => self.handle_register(conn, ctx, controller),
_ => Ok(()),
}
}
@ -78,7 +86,14 @@ impl Handler<ControllerWsAction> for ControllerWs {
impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
fn handle(&mut self, msg: Result<Message, ProtocolError>, ctx: &mut Self::Context) {
let mut pool_conn = block_on(self.pool.acquire()).unwrap();
let mut pool_conn = match block_on(self.pool.acquire()) {
Ok(conn) => conn,
Err(err) => {
log::error!("Failed to acquire database connection: {:?}", err);
ctx.stop();
return;
}
};
let msg = match msg {
Err(_) => {
@ -96,14 +111,22 @@ impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
Message::Pong(_) => {
self.hb = Instant::now();
}
Message::Text(text) => {
let action: ControllerWsAction = serde_json::from_str(&text).unwrap();
let action_res = self.handle_action(&mut pool_conn, ctx, action);
if let Err(e) = action_res {
log::error!("Error handling action: {:?}", e);
ctx.text(serde_json::to_string(&e).unwrap());
Message::Text(text) => match serde_json::from_str(&text) {
Ok(action) => {
let action_res = self.handle_action(&mut pool_conn, ctx, action);
if let Err(e) = action_res {
log::error!("Error handling action: {:?}", e);
ctx.text(serde_json::to_string(&e).expect("Failed to serialize error"));
}
}
}
Err(e) => {
log::error!("Error deserializing action: {:?}", e);
ctx.text(
serde_json::to_string(&EmgauwaError::Serialization(e))
.expect("Failed to serialize error"),
);
}
},
Message::Binary(_) => log::warn!("Received unexpected binary in controller ws"),
Message::Close(reason) => {
ctx.close(reason);

View file

@ -3,7 +3,7 @@ use std::time::Instant;
use actix::Addr;
use actix_web::{get, web, HttpRequest, HttpResponse};
use actix_web_actors::ws;
use emgauwa_lib::errors::{ApiError, EmgauwaError};
use emgauwa_lib::errors::EmgauwaError;
use sqlx::{Pool, Sqlite};
use crate::app_state::AppServer;
@ -28,10 +28,6 @@ pub async fn ws_controllers(
&req,
stream,
)
.map_err(|_| {
EmgauwaError::from(ApiError::InternalError(String::from(
"error starting websocket",
)))
});
.map_err(|_| EmgauwaError::Internal(String::from("error starting websocket")));
resp
}