Add connected controllers hashmap for controller-ws

This commit is contained in:
Tobias Reisinger 2023-11-28 20:20:12 +01:00
parent 6459804e1f
commit 6536ff0792
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
17 changed files with 267 additions and 649 deletions
emgauwa-controller/src

View file

@ -2,11 +2,11 @@ use std::str;
use crate::relay_loop::run_relay_loop;
use crate::settings::Settings;
use emgauwa_lib::db;
use emgauwa_lib::db::{DbController, DbRelay};
use emgauwa_lib::handlers::v1::ws::controllers::ControllerWsAction;
use emgauwa_lib::models::convert_db_list;
use emgauwa_lib::models::{Controller, FromDbModel};
use emgauwa_lib::types::ControllerUid;
use emgauwa_lib::{db, models};
use futures::channel::mpsc;
use futures::{future, pin_mut, SinkExt, StreamExt};
use sqlx::pool::PoolConnection;
@ -64,37 +64,23 @@ async fn main() {
futures::executor::block_on(create_this_controller(&mut conn, &settings))
});
let db_relays: Vec<DbRelay> = settings
.relays
.iter()
.map(|relay| {
futures::executor::block_on(async {
match DbRelay::get_by_controller_and_num(
&mut conn,
&db_controller,
relay.number.unwrap(),
)
.await
.expect("Failed to get relay from database")
{
None => create_this_relay(&mut conn, &db_controller, relay).await,
Some(relay) => relay,
}
})
})
.collect();
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;
}
}
let db_controller = db_controller
.update(&mut conn, &db_controller.name, db_relays.len() as i64)
.update(&mut conn, &db_controller.name, settings.relays.len() as i64)
.await
.unwrap();
let relays = convert_db_list(&mut conn, db_relays).expect("Failed to convert relays");
let this = models::Controller {
controller: db_controller,
relays,
};
let this = Controller::from_db_model(&mut conn, db_controller)
.expect("Failed to convert database models");
let url = format!(
"ws://{}:{}/api/v1/ws/controllers",