common/emgauwa-controller/src/main.rs
Tobias Reisinger 1d4e9efa15
Try to fix the threading
Add explicit tokio::spawn in controller
Add an arbiter for the app_state in core
2024-03-28 01:23:49 +01:00

121 lines
2.8 KiB
Rust

use actix::Actor;
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::utils::{drop_privileges, init_logging};
use sqlx::pool::PoolConnection;
use sqlx::Sqlite;
use crate::relay_loop::run_relays_loop;
use crate::settings::Settings;
use crate::ws::run_ws_loop;
mod app_state;
mod driver;
mod relay_loop;
mod settings;
mod utils;
mod ws;
async fn create_this_controller(
conn: &mut PoolConnection<Sqlite>,
settings: &Settings,
) -> Result<DbController, EmgauwaError> {
DbController::create(
conn,
&ControllerUid::default(),
&settings.name,
settings.relays.len() as i64,
)
.await
.map_err(EmgauwaError::from)
}
async fn create_this_relay(
conn: &mut PoolConnection<Sqlite>,
this_controller: &DbController,
settings_relay: &settings::Relay,
) -> Result<DbRelay, EmgauwaError> {
let relay = DbRelay::create(
conn,
&settings_relay.name,
settings_relay.number.ok_or(EmgauwaError::Internal(
"Relay number is missing".to_string(),
))?,
this_controller,
)
.await?;
let off = DbSchedule::get_off(conn).await?;
for weekday in 0..7 {
DbJunctionRelaySchedule::set_schedule(conn, &relay, &off, weekday).await?;
}
Ok(relay)
}
#[actix::main]
async fn main() -> Result<(), std::io::Error> {
let settings = settings::init()?;
drop_privileges(&settings.permissions)?;
init_logging(&settings.logging.level)?;
let pool = db::init(&settings.database)
.await
.map_err(EmgauwaError::from)?;
let mut conn = pool.acquire().await.map_err(EmgauwaError::from)?;
let db_controller = match DbController::get_all(&mut conn)
.await
.map_err(EmgauwaError::from)?
.pop()
{
None => futures::executor::block_on(create_this_controller(&mut conn, &settings))?,
Some(c) => c,
};
for relay in &settings.relays {
if DbRelay::get_by_controller_and_num(
&mut conn,
&db_controller,
relay.number.ok_or(EmgauwaError::Internal(
"Relay number is missing".to_string(),
))?,
)
.await
.map_err(EmgauwaError::from)?
.is_none()
{
create_this_relay(&mut conn, &db_controller, relay)
.await
.map_err(EmgauwaError::from)?;
}
}
let db_controller = db_controller
.update(&mut conn, &db_controller.name, settings.relays.len() as i64)
.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.server.host, settings.server.port
);
let _ = tokio::join!(
tokio::spawn(run_relays_loop(app_state.clone())),
tokio::spawn(run_ws_loop(pool.clone(), app_state.clone(), url)),
);
Ok(())
}