Refactor models names
This commit is contained in:
parent
76b14ce75b
commit
be7f31906c
24 changed files with 461 additions and 340 deletions
emgauwa-controller/src
|
@ -1,32 +1,51 @@
|
|||
use std::str;
|
||||
|
||||
use futures::{future, pin_mut, SinkExt, StreamExt};
|
||||
use crate::relay_loop::run_relay_loop;
|
||||
use crate::settings::Settings;
|
||||
use emgauwa_lib::db::errors::DatabaseError;
|
||||
use emgauwa_lib::db::{DbController, DbRelay};
|
||||
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;
|
||||
use sqlx::Sqlite;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
|
||||
use tokio_tungstenite::tungstenite::Error;
|
||||
use emgauwa_lib::db;
|
||||
use emgauwa_lib::db::Controller;
|
||||
use emgauwa_lib::db::types::ControllerUid;
|
||||
use crate::relay_loop::run_relay_loop;
|
||||
use crate::settings::Settings;
|
||||
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
|
||||
|
||||
mod settings;
|
||||
mod driver;
|
||||
mod relay_loop;
|
||||
mod settings;
|
||||
|
||||
fn create_this_controller(conn: &mut PoolConnection<Sqlite>, settings: &Settings) -> Controller {
|
||||
futures::executor::block_on(async {
|
||||
Controller::create(
|
||||
conn,
|
||||
&ControllerUid::new(),
|
||||
&settings.name,
|
||||
i64::try_from(settings.relays.len()).expect("Too many relays"),
|
||||
true
|
||||
).await.expect("Failed to create controller")
|
||||
})
|
||||
async fn create_this_controller(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
settings: &Settings,
|
||||
) -> DbController {
|
||||
DbController::create(
|
||||
conn,
|
||||
&ControllerUid::default(),
|
||||
&settings.name,
|
||||
i64::try_from(settings.relays.len()).expect("Too many relays"),
|
||||
true,
|
||||
)
|
||||
.await
|
||||
.expect("Failed to create controller")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
@ -37,22 +56,60 @@ async fn main() {
|
|||
|
||||
let mut conn = pool.acquire().await.unwrap();
|
||||
|
||||
let this = Controller::get_all(&mut conn)
|
||||
let db_controller = DbController::get_all(&mut conn)
|
||||
.await
|
||||
.expect("Failed to get controller from database")
|
||||
.pop()
|
||||
.unwrap_or_else(|| create_this_controller(&mut conn, &settings));
|
||||
.unwrap_or_else(|| {
|
||||
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
|
||||
{
|
||||
Ok(relay) => relay,
|
||||
Err(err) => match err {
|
||||
DatabaseError::NotFound => {
|
||||
create_this_relay(&mut conn, &db_controller, relay).await
|
||||
}
|
||||
_ => panic!("Failed to get relay from database"),
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
let db_controller = db_controller
|
||||
.update(&mut conn, &db_controller.name, db_relays.len() as i64, true)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let relays = db_relays
|
||||
.into_iter()
|
||||
.map(|relay| models::Relay::from_db_relay(relay, &mut conn))
|
||||
.collect();
|
||||
|
||||
let this = models::Controller {
|
||||
controller: db_controller,
|
||||
relays,
|
||||
};
|
||||
|
||||
let this_json = serde_json::to_string(&this).unwrap();
|
||||
|
||||
println!("{:?}", settings.relays);
|
||||
println!("{:?}", this);
|
||||
println!("{}", this_json);
|
||||
|
||||
let url = format!(
|
||||
"ws://{}:{}/api/v1/ws/controllers",
|
||||
settings.core.host,
|
||||
settings.core.port
|
||||
settings.core.host, settings.core.port
|
||||
);
|
||||
|
||||
let (stdin_tx, stdin_rx) = mpsc::unbounded();
|
||||
|
@ -83,13 +140,14 @@ async fn read_stdin(tx: mpsc::UnboundedSender<Message>) {
|
|||
Ok(n) => n,
|
||||
};
|
||||
buf.truncate(n);
|
||||
tx.unbounded_send(Message::text(str::from_utf8(&buf).unwrap())).unwrap();
|
||||
tx.unbounded_send(Message::text(str::from_utf8(&buf).unwrap()))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_message(message_result: Result<Message, Error>) {
|
||||
match message_result {
|
||||
Ok(message) => println!("{}", message.into_text().unwrap()),
|
||||
Err(err) => println!("Error: {}", err)
|
||||
Err(err) => println!("Error: {}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue