Refactor errors and some other stuff/fixes

This commit is contained in:
Tobias Reisinger 2023-12-01 18:27:04 +01:00
parent 8d996888bd
commit 5a7b2de0ea
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
28 changed files with 507 additions and 341 deletions
emgauwa-controller/src

View file

@ -1,6 +1,6 @@
use emgauwa_lib::constants::WEBSOCKET_RETRY_TIMEOUT;
use emgauwa_lib::db::errors::DatabaseError;
use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
use emgauwa_lib::errors::DatabaseError;
use emgauwa_lib::models::{Controller, FromDbModel};
use emgauwa_lib::types::{ControllerUid, ControllerWsAction};
use emgauwa_lib::{db, utils};
@ -62,7 +62,10 @@ async fn main() {
let pool = db::init(&settings.database).await;
let mut conn = pool.acquire().await.unwrap();
let mut conn = pool
.acquire()
.await
.expect("Failed to get database connection");
let db_controller = DbController::get_all(&mut conn)
.await
@ -100,31 +103,33 @@ async fn main() {
tokio::spawn(run_relay_loop(settings));
loop {
match connect_async(&url).await {
Ok(connection) => {
let (ws_stream, _) = connection;
let (mut write, read) = ws_stream.split();
let ws_action = ControllerWsAction::Register(this.clone());
let ws_action_json = serde_json::to_string(&ws_action).unwrap();
write.send(Message::text(ws_action_json)).await.unwrap();
let read_handler = read.for_each(handle_message);
read_handler.await;
log::warn!("Lost connection to websocket");
}
Err(err) => {
log::warn!("Failed to connect to websocket: {}", err,);
}
}
log::info!(
"Trying to connect in {} seconds...",
"Retrying to connect in {} seconds...",
WEBSOCKET_RETRY_TIMEOUT.as_secs()
);
time::sleep(WEBSOCKET_RETRY_TIMEOUT).await;
let connect_result = connect_async(&url).await;
if let Err(err) = connect_result {
log::warn!("Failed to connect to websocket: {}", err,);
continue;
}
let (ws_stream, _) = connect_result.unwrap();
let (mut write, read) = ws_stream.split();
let ws_action = ControllerWsAction::Register(this.clone());
let ws_action_json = serde_json::to_string(&ws_action).unwrap();
write.send(Message::text(ws_action_json)).await.unwrap();
let read_handler = read.for_each(handle_message);
read_handler.await;
log::warn!("Lost connection to websocket");
}
}