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

View file

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