2023-11-29 00:03:04 +00:00
|
|
|
use emgauwa_lib::constants::WEBSOCKET_RETRY_TIMEOUT;
|
2023-11-27 11:49:40 +00:00
|
|
|
use emgauwa_lib::db::{DbController, DbRelay};
|
2023-11-28 19:20:12 +00:00
|
|
|
use emgauwa_lib::models::{Controller, FromDbModel};
|
2023-11-29 13:27:46 +00:00
|
|
|
use emgauwa_lib::types::{ControllerUid, ControllerWsAction};
|
2023-11-29 00:03:04 +00:00
|
|
|
use emgauwa_lib::{db, utils};
|
|
|
|
use futures::{SinkExt, StreamExt};
|
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;
|
2023-11-29 13:27:46 +00:00
|
|
|
use tokio_tungstenite::connect_async;
|
|
|
|
use tokio_tungstenite::tungstenite::protocol::Message;
|
2023-11-24 21:45:44 +00:00
|
|
|
use tokio_tungstenite::tungstenite::Error;
|
2023-11-29 00:03:04 +00:00
|
|
|
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-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;
|
|
|
|
|
|
|
|
async fn create_this_controller(
|
|
|
|
conn: &mut PoolConnection<Sqlite>,
|
|
|
|
settings: &Settings,
|
|
|
|
) -> DbController {
|
|
|
|
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
|
|
|
|
.expect("Failed to create controller")
|
|
|
|
}
|
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,
|
|
|
|
) -> DbRelay {
|
|
|
|
DbRelay::create(
|
|
|
|
conn,
|
|
|
|
&settings_relay.name,
|
|
|
|
settings_relay.number.unwrap(),
|
|
|
|
this_controller,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.expect("Failed to create 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]
|
|
|
|
async fn main() {
|
|
|
|
let settings = settings::init();
|
2023-11-29 00:03:04 +00:00
|
|
|
init_logging(&settings.logging.level);
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-24 23:39:44 +00:00
|
|
|
let pool = db::init(&settings.database).await;
|
|
|
|
|
|
|
|
let mut conn = pool.acquire().await.unwrap();
|
|
|
|
|
2023-11-27 11:49:40 +00:00
|
|
|
let db_controller = DbController::get_all(&mut conn)
|
2023-11-24 23:39:44 +00:00
|
|
|
.await
|
|
|
|
.expect("Failed to get controller from database")
|
|
|
|
.pop()
|
2023-11-27 11:49:40 +00:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
futures::executor::block_on(create_this_controller(&mut conn, &settings))
|
|
|
|
});
|
|
|
|
|
2023-11-28 19:20:12 +00:00
|
|
|
for relay in &settings.relays {
|
|
|
|
if DbRelay::get_by_controller_and_num(&mut conn, &db_controller, relay.number.unwrap())
|
|
|
|
.await
|
|
|
|
.expect("Failed to get relay from database")
|
|
|
|
.is_none()
|
|
|
|
{
|
|
|
|
create_this_relay(&mut conn, &db_controller, relay).await;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-28 19:20:12 +00:00
|
|
|
let this = Controller::from_db_model(&mut conn, db_controller)
|
|
|
|
.expect("Failed to convert database models");
|
2023-11-24 23:39:44 +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-11-29 16:55:49 +00:00
|
|
|
log::info!(
|
|
|
|
"Trying to connect in {} seconds...",
|
|
|
|
WEBSOCKET_RETRY_TIMEOUT.as_secs()
|
|
|
|
);
|
2023-11-29 00:03:04 +00:00
|
|
|
time::sleep(WEBSOCKET_RETRY_TIMEOUT).await;
|
|
|
|
|
|
|
|
let connect_result = connect_async(&url).await;
|
|
|
|
if let Err(err) = connect_result {
|
2023-11-29 16:55:49 +00:00
|
|
|
log::warn!("Failed to connect to websocket: {}", err,);
|
2023-11-29 00:03:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let (ws_stream, _) = connect_result.unwrap();
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-29 00:03:04 +00:00
|
|
|
let (mut write, read) = ws_stream.split();
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-29 00:03:04 +00:00
|
|
|
let ws_action = ControllerWsAction::Register(this.clone());
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-29 00:03:04 +00:00
|
|
|
let ws_action_json = serde_json::to_string(&ws_action).unwrap();
|
|
|
|
write.send(Message::text(ws_action_json)).await.unwrap();
|
2023-11-25 23:54:03 +00:00
|
|
|
|
2023-11-29 00:03:04 +00:00
|
|
|
let read_handler = read.for_each(handle_message);
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-29 00:03:04 +00:00
|
|
|
read_handler.await;
|
|
|
|
|
2023-11-29 16:55:49 +00:00
|
|
|
log::warn!("Lost connection to websocket");
|
2023-11-23 15:00:24 +00:00
|
|
|
}
|
2023-11-24 21:45:44 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 13:27:46 +00:00
|
|
|
async fn handle_message(message_result: Result<Message, Error>) {
|
2023-11-24 21:45:44 +00:00
|
|
|
match message_result {
|
2023-11-29 00:03:04 +00:00
|
|
|
Ok(message) => {
|
|
|
|
if let Message::Text(msg_text) = message {
|
|
|
|
log::debug!("{}", msg_text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => log::debug!("Error: {}", err),
|
2023-11-24 21:45:44 +00:00
|
|
|
}
|
2023-11-27 11:49:40 +00:00
|
|
|
}
|