2023-11-23 15:00:24 +00:00
|
|
|
use std::str;
|
|
|
|
|
2023-11-24 23:39:44 +00:00
|
|
|
use futures::{future, pin_mut, SinkExt, StreamExt};
|
2023-11-23 15:00:24 +00:00
|
|
|
use futures::channel::mpsc;
|
2023-11-24 23:39:44 +00:00
|
|
|
use sqlx::pool::PoolConnection;
|
|
|
|
use sqlx::Sqlite;
|
2023-11-24 21:45:44 +00:00
|
|
|
use tokio::io::AsyncReadExt;
|
2023-11-23 15:00:24 +00:00
|
|
|
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
|
2023-11-24 21:45:44 +00:00
|
|
|
use tokio_tungstenite::tungstenite::Error;
|
|
|
|
use emgauwa_lib::db;
|
2023-11-24 23:39:44 +00:00
|
|
|
use emgauwa_lib::db::Controller;
|
|
|
|
use emgauwa_lib::db::types::ControllerUid;
|
2023-11-25 23:54:03 +00:00
|
|
|
use crate::relay_loop::run_relay_loop;
|
2023-11-24 23:39:44 +00:00
|
|
|
use crate::settings::Settings;
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-22 22:28:03 +00:00
|
|
|
mod settings;
|
2023-11-24 23:39:44 +00:00
|
|
|
mod driver;
|
2023-11-25 23:54:03 +00:00
|
|
|
mod relay_loop;
|
2023-11-24 23:39:44 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
})
|
|
|
|
}
|
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-24 23:39:44 +00:00
|
|
|
let pool = db::init(&settings.database).await;
|
|
|
|
|
|
|
|
let mut conn = pool.acquire().await.unwrap();
|
|
|
|
|
|
|
|
let this = Controller::get_all(&mut conn)
|
|
|
|
.await
|
|
|
|
.expect("Failed to get controller from database")
|
|
|
|
.pop()
|
|
|
|
.unwrap_or_else(|| create_this_controller(&mut conn, &settings));
|
|
|
|
|
|
|
|
let this_json = serde_json::to_string(&this).unwrap();
|
|
|
|
|
2023-11-25 23:54:03 +00:00
|
|
|
println!("{:?}", settings.relays);
|
2023-11-24 23:39:44 +00:00
|
|
|
println!("{:?}", this);
|
2023-11-25 23:54:03 +00:00
|
|
|
println!("{}", this_json);
|
2023-11-24 21:45:44 +00:00
|
|
|
|
2023-11-23 15:00:24 +00:00
|
|
|
let url = format!(
|
|
|
|
"ws://{}:{}/api/v1/ws/controllers",
|
|
|
|
settings.core.host,
|
|
|
|
settings.core.port
|
|
|
|
);
|
|
|
|
|
|
|
|
let (stdin_tx, stdin_rx) = mpsc::unbounded();
|
|
|
|
tokio::spawn(read_stdin(stdin_tx));
|
|
|
|
|
|
|
|
let (ws_stream, _) = connect_async(url).await.expect("Failed to connect");
|
|
|
|
|
2023-11-24 23:39:44 +00:00
|
|
|
let (mut write, read) = ws_stream.split();
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-24 23:39:44 +00:00
|
|
|
write.send(Message::text(this_json)).await.unwrap();
|
2023-11-24 21:45:44 +00:00
|
|
|
let ws_to_stdout = read.for_each(handle_message);
|
2023-11-25 23:54:03 +00:00
|
|
|
let stdin_to_ws = stdin_rx.map(Ok).forward(write);
|
2023-11-23 15:00:24 +00:00
|
|
|
|
2023-11-25 23:54:03 +00:00
|
|
|
tokio::spawn(run_relay_loop(settings));
|
|
|
|
|
|
|
|
pin_mut!(stdin_to_ws, ws_to_stdout);
|
|
|
|
future::select(stdin_to_ws, ws_to_stdout).await;
|
2023-11-22 22:28:03 +00:00
|
|
|
}
|
2023-11-23 15:00:24 +00:00
|
|
|
|
|
|
|
// Our helper method which will read data from stdin and send it along the
|
|
|
|
// sender provided.
|
|
|
|
async fn read_stdin(tx: mpsc::UnboundedSender<Message>) {
|
|
|
|
let mut stdin = tokio::io::stdin();
|
|
|
|
loop {
|
|
|
|
let mut buf = vec![0; 1024];
|
|
|
|
let n = match stdin.read(&mut buf).await {
|
|
|
|
Err(_) | Ok(0) => break,
|
|
|
|
Ok(n) => n,
|
|
|
|
};
|
|
|
|
buf.truncate(n);
|
|
|
|
tx.unbounded_send(Message::text(str::from_utf8(&buf).unwrap())).unwrap();
|
|
|
|
}
|
2023-11-24 21:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-11-23 15:00:24 +00:00
|
|
|
}
|