Fix bugs and add controller action for controller ws
This commit is contained in:
parent
8b1affd8c7
commit
6f8d63e7be
11 changed files with 177 additions and 56 deletions
emgauwa-controller/src
|
@ -2,23 +2,21 @@ use emgauwa_lib::constants::WEBSOCKET_RETRY_TIMEOUT;
|
|||
use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
|
||||
use emgauwa_lib::errors::EmgauwaError;
|
||||
use emgauwa_lib::models::{Controller, FromDbModel};
|
||||
use emgauwa_lib::types::{ControllerUid, ControllerWsAction};
|
||||
use emgauwa_lib::types::ControllerUid;
|
||||
use emgauwa_lib::{db, utils};
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::Sqlite;
|
||||
use tokio::time;
|
||||
use tokio_tungstenite::connect_async;
|
||||
use tokio_tungstenite::tungstenite::protocol::Message;
|
||||
use tokio_tungstenite::tungstenite::Error;
|
||||
use utils::init_logging;
|
||||
|
||||
use crate::relay_loop::run_relay_loop;
|
||||
use crate::settings::Settings;
|
||||
use crate::ws::run_websocket;
|
||||
|
||||
mod driver;
|
||||
mod relay_loop;
|
||||
mod settings;
|
||||
mod ws;
|
||||
|
||||
async fn create_this_controller(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
|
@ -57,34 +55,6 @@ async fn create_this_relay(
|
|||
Ok(relay)
|
||||
}
|
||||
|
||||
async fn run_websocket(this: Controller, url: &str) -> Result<(), EmgauwaError> {
|
||||
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)?;
|
||||
if let Err(err) = write.send(Message::text(ws_action_json)).await {
|
||||
log::error!("Failed to register at websocket: {}", err);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
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,);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
let settings = settings::init()?;
|
||||
|
@ -128,8 +98,6 @@ 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 url = format!(
|
||||
"ws://{}:{}/api/v1/ws/controllers",
|
||||
settings.core.host, settings.core.port
|
||||
|
@ -138,7 +106,14 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
tokio::spawn(run_relay_loop(settings));
|
||||
|
||||
loop {
|
||||
let run_result = run_websocket(this.clone(), &url).await;
|
||||
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;
|
||||
if let Err(err) = run_result {
|
||||
log::error!("Error running websocket: {}", err);
|
||||
}
|
||||
|
@ -150,14 +125,3 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
time::sleep(WEBSOCKET_RETRY_TIMEOUT).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_message(message_result: Result<Message, Error>) {
|
||||
match message_result {
|
||||
Ok(message) => {
|
||||
if let Message::Text(msg_text) = message {
|
||||
log::debug!("{}", msg_text)
|
||||
}
|
||||
}
|
||||
Err(err) => log::debug!("Error: {}", err),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue