2023-11-29 00:03:04 +00:00
|
|
|
use emgauwa_lib::constants::WEBSOCKET_RETRY_TIMEOUT;
|
2023-11-29 23:57:03 +00:00
|
|
|
use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
|
2023-12-05 00:51:06 +00:00
|
|
|
use emgauwa_lib::errors::EmgauwaError;
|
2023-11-28 19:20:12 +00:00
|
|
|
use emgauwa_lib::models::{Controller, FromDbModel};
|
2023-12-05 15:11:40 +00:00
|
|
|
use emgauwa_lib::types::ControllerUid;
|
2023-11-29 00:03:04 +00:00
|
|
|
use emgauwa_lib::{db, utils};
|
2023-11-24 23:39:44 +00:00
|
|
|
use sqlx::pool::PoolConnection;
|
|
|
|
use sqlx::Sqlite;
|
2023-11-29 00:03:04 +00:00
|
|
|
use tokio::time;
|
|
|
|
use utils::init_logging;
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-29 13:27:46 +00:00
|
|
|
use crate::relay_loop::run_relay_loop;
|
|
|
|
use crate::settings::Settings;
|
2023-12-05 15:11:40 +00:00
|
|
|
use crate::ws::run_websocket;
|
2023-11-29 13:27:46 +00:00
|
|
|
|
2023-11-24 23:39:44 +00:00
|
|
|
mod driver;
|
2023-11-25 23:54:03 +00:00
|
|
|
mod relay_loop;
|
2023-11-27 11:49:40 +00:00
|
|
|
mod settings;
|
2023-12-05 15:11:40 +00:00
|
|
|
mod ws;
|
2023-11-27 11:49:40 +00:00
|
|
|
|
|
|
|
async fn create_this_controller(
|
|
|
|
conn: &mut PoolConnection<Sqlite>,
|
|
|
|
settings: &Settings,
|
2023-12-05 00:42:19 +00:00
|
|
|
) -> Result<DbController, EmgauwaError> {
|
2023-11-27 11:49:40 +00:00
|
|
|
DbController::create(
|
|
|
|
conn,
|
|
|
|
&ControllerUid::default(),
|
|
|
|
&settings.name,
|
2023-11-29 16:55:49 +00:00
|
|
|
settings.relays.len() as i64,
|
2023-11-27 11:49:40 +00:00
|
|
|
)
|
|
|
|
.await
|
2023-12-05 00:42:19 +00:00
|
|
|
.map_err(EmgauwaError::from)
|
2023-11-27 11:49:40 +00:00
|
|
|
}
|
2023-11-24 23:39:44 +00:00
|
|
|
|
2023-11-27 11:49:40 +00:00
|
|
|
async fn create_this_relay(
|
|
|
|
conn: &mut PoolConnection<Sqlite>,
|
|
|
|
this_controller: &DbController,
|
|
|
|
settings_relay: &settings::Relay,
|
2023-12-05 00:42:19 +00:00
|
|
|
) -> Result<DbRelay, EmgauwaError> {
|
2023-11-29 23:57:03 +00:00
|
|
|
let relay = DbRelay::create(
|
2023-11-27 11:49:40 +00:00
|
|
|
conn,
|
|
|
|
&settings_relay.name,
|
2023-12-05 00:42:19 +00:00
|
|
|
settings_relay.number.ok_or(EmgauwaError::Internal(
|
|
|
|
"Relay number is missing".to_string(),
|
|
|
|
))?,
|
2023-11-27 11:49:40 +00:00
|
|
|
this_controller,
|
|
|
|
)
|
2023-11-29 23:57:03 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
let off = DbSchedule::get_off(conn).await?;
|
|
|
|
for weekday in 0..7 {
|
|
|
|
DbJunctionRelaySchedule::set_schedule(conn, &relay, &off, weekday).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(relay)
|
2023-11-24 23:39:44 +00:00
|
|
|
}
|
2023-11-22 22:28:03 +00:00
|
|
|
|
2023-11-23 15:00:24 +00:00
|
|
|
#[tokio::main]
|
2023-12-05 00:42:19 +00:00
|
|
|
async fn main() -> Result<(), std::io::Error> {
|
|
|
|
let settings = settings::init()?;
|
|
|
|
init_logging(&settings.logging.level)?;
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-12-05 00:42:19 +00:00
|
|
|
let pool = db::init(&settings.database)
|
2023-12-01 17:27:04 +00:00
|
|
|
.await
|
2023-12-05 00:42:19 +00:00
|
|
|
.map_err(EmgauwaError::from)?;
|
|
|
|
|
|
|
|
let mut conn = pool.acquire().await.map_err(EmgauwaError::from)?;
|
2023-11-24 23:39:44 +00:00
|
|
|
|
2023-12-04 22:59:26 +00:00
|
|
|
let db_controller = match DbController::get_all(&mut conn)
|
2023-11-24 23:39:44 +00:00
|
|
|
.await
|
2023-12-05 00:42:19 +00:00
|
|
|
.map_err(EmgauwaError::from)?
|
2023-11-24 23:39:44 +00:00
|
|
|
.pop()
|
2023-12-04 22:59:26 +00:00
|
|
|
{
|
2023-12-05 00:51:06 +00:00
|
|
|
None => futures::executor::block_on(create_this_controller(&mut conn, &settings))?,
|
2023-12-04 22:59:26 +00:00
|
|
|
Some(c) => c,
|
|
|
|
};
|
2023-11-27 11:49:40 +00:00
|
|
|
|
2023-11-28 19:20:12 +00:00
|
|
|
for relay in &settings.relays {
|
2023-12-04 22:59:26 +00:00
|
|
|
if DbRelay::get_by_controller_and_num(
|
|
|
|
&mut conn,
|
|
|
|
&db_controller,
|
2023-12-05 00:42:19 +00:00
|
|
|
relay.number.ok_or(EmgauwaError::Internal(
|
|
|
|
"Relay number is missing".to_string(),
|
|
|
|
))?,
|
2023-12-04 22:59:26 +00:00
|
|
|
)
|
|
|
|
.await
|
2023-12-05 00:42:19 +00:00
|
|
|
.map_err(EmgauwaError::from)?
|
2023-12-04 22:59:26 +00:00
|
|
|
.is_none()
|
2023-11-28 19:20:12 +00:00
|
|
|
{
|
2023-11-29 23:57:03 +00:00
|
|
|
create_this_relay(&mut conn, &db_controller, relay)
|
|
|
|
.await
|
2023-12-05 00:42:19 +00:00
|
|
|
.map_err(EmgauwaError::from)?;
|
2023-11-28 19:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-27 11:49:40 +00:00
|
|
|
|
|
|
|
let db_controller = db_controller
|
2023-11-28 19:20:12 +00:00
|
|
|
.update(&mut conn, &db_controller.name, settings.relays.len() as i64)
|
2023-11-27 11:49:40 +00:00
|
|
|
.await
|
2023-12-05 00:42:19 +00:00
|
|
|
.map_err(EmgauwaError::from)?;
|
2023-11-27 11:49:40 +00:00
|
|
|
|
2023-11-23 15:00:24 +00:00
|
|
|
let url = format!(
|
|
|
|
"ws://{}:{}/api/v1/ws/controllers",
|
2023-11-27 11:49:40 +00:00
|
|
|
settings.core.host, settings.core.port
|
2023-11-23 15:00:24 +00:00
|
|
|
);
|
|
|
|
|
2023-11-29 00:03:04 +00:00
|
|
|
tokio::spawn(run_relay_loop(settings));
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-29 00:03:04 +00:00
|
|
|
loop {
|
2023-12-05 15:11:40 +00:00
|
|
|
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;
|
2023-12-05 00:42:19 +00:00
|
|
|
if let Err(err) = run_result {
|
|
|
|
log::error!("Error running websocket: {}", err);
|
|
|
|
}
|
2023-11-29 00:03:04 +00:00
|
|
|
|
2023-12-01 17:27:04 +00:00
|
|
|
log::info!(
|
|
|
|
"Retrying to connect in {} seconds...",
|
|
|
|
WEBSOCKET_RETRY_TIMEOUT.as_secs()
|
|
|
|
);
|
|
|
|
time::sleep(WEBSOCKET_RETRY_TIMEOUT).await;
|
2023-11-23 15:00:24 +00:00
|
|
|
}
|
2023-11-24 21:45:44 +00:00
|
|
|
}
|