From df4b62f2a1aa616e4038b1511b2e38cd956e6423 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 24 Jan 2024 22:25:00 +0100 Subject: Get Chat window working --- src/bin/fake-radio.rs | 4 ++++ src/db.rs | 2 +- src/main.rs | 30 ++++++++++++++++++------------ src/ui.rs | 51 ++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 61 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/bin/fake-radio.rs b/src/bin/fake-radio.rs index 6747937..f27b7d6 100644 --- a/src/bin/fake-radio.rs +++ b/src/bin/fake-radio.rs @@ -80,6 +80,10 @@ fn receive_loop() { eprintln!(" Ident {}-{}", ident.callsign, ident.ssid); } + for dest in packet.destination_iter() { + eprintln!(" TO {}-{}", dest.callsign(), dest.ssid()); + } + if let Some(gps) = packet.gps() { eprintln!(" GPS {} {}", gps.latitude(), gps.longitude()); } diff --git a/src/db.rs b/src/db.rs index 2e07792..68455b3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -103,7 +103,7 @@ impl Database { SELECT id, received_at, content FROM frames_received WHERE received_at > ?1 - ORDER BY received_at DESC"#) + ORDER BY received_at"#) .bind(unix_timestamp) .fetch_all(&self.pool) .await?; diff --git a/src/main.rs b/src/main.rs index 5eb47e8..35d7d65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,10 @@ const TUN_MTU : usize = 255; #[tokio::main] async fn main() -> std::io::Result<()> { - simple_logger::SimpleLogger::new().env().init().unwrap(); + simple_logger::SimpleLogger::new() + .with_level(log::LevelFilter::Debug) + .env() + .init().unwrap(); let conf = config::Config::load().expect("Could not load config"); @@ -125,18 +128,21 @@ async fn main() -> std::io::Result<()> { if let Some(ident) = packet.identification() { debug!(" From {}-{}", ident.callsign, ident.ssid); - let mut commentbuf = [0u8, 255]; - if let Ok(comment) = packet.comment(&mut commentbuf) { - let m = ui::UIPacket { - received_at: chrono::Utc::now(), - from_callsign: ident.callsign.to_string(), - from_ssid: ident.ssid, - comment: Some(comment.to_owned()) - }; - match ws_broadcast.send(m) { - Ok(num) => debug!("Send WS message to {num}"), - Err(_) => debug!("No WS receivers currently"), + let mut commentbuf = [0u8; 255]; + match packet.comment(&mut commentbuf) { + Ok(comment) => { + let m = ui::UIPacket { + received_at: chrono::Utc::now(), + from_callsign: ident.callsign.to_string(), + from_ssid: ident.ssid, + comment: Some(comment.to_owned()) + }; + match ws_broadcast.send(m) { + Ok(num) => debug!("Send WS message to {num}"), + Err(_) => debug!("No WS receivers currently"), + } } + Err(e) => warn!("Decode packet comment error: {e}"), } } diff --git a/src/ui.rs b/src/ui.rs index 5a5e322..7aa7773 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -41,7 +41,9 @@ pub async fn serve(port: u16, shared_state: SharedState) { .with_state(shared_state); let listener = tokio::net::TcpListener::bind(("0.0.0.0", port)).await.unwrap(); - axum::serve(listener, app).await.unwrap() + axum::serve(listener, + app.into_make_service_with_connect_info::()) + .await.unwrap() } #[derive(PartialEq)] @@ -58,8 +60,8 @@ impl ActivePage { fn styles(&self) -> Vec<&'static str> { match self { ActivePage::Dashboard => vec![], - ActivePage::Chat => vec!["chat.js"], - ActivePage::Send => vec!["send.js"], + ActivePage::Chat => vec!["chat.js", "main.js", "strftime.js"], + ActivePage::Send => vec!["send.js", "main.js", "strftime.js"], ActivePage::Settings => vec![], ActivePage::None => vec![], } @@ -208,7 +210,7 @@ async fn handle_socket( mut socket: WebSocket, mut rx: tokio::sync::broadcast::Receiver, who: SocketAddr) { - if socket.send(Message::Ping(vec![1, 2, 3])).await.is_ok() { + if socket.send(Message::Ping(vec![1])).await.is_ok() { info!("Pinged {who}..."); } else { info!("Could not ping {who}!"); @@ -218,13 +220,17 @@ async fn handle_socket( let mut send_task = tokio::spawn(async move { while let Ok(m) = rx.recv().await { - if let Ok(m_json) = serde_json::to_string(&m) { - if sender + debug!("Sending {:?} to {who}", m.comment); + match serde_json::to_string(&m) { + Ok(m_json) => if sender .send(Message::Text(m_json)) - .await - .is_err() + .await + .is_err() { return; + }, + Err(e) => { + warn!("Could not convert message to json: {e}"); } } } @@ -343,22 +349,41 @@ fn build_packet(config: config::Config, payload: ApiSendPacket) -> anyhow::Resul } async fn post_packet(State(state): State, Json(payload): Json) -> StatusCode { - let (config, transmit_queue) = { + let (config, transmit_queue, mut db, ws_broadcast) = { let s = state.lock().unwrap(); - (s.conf.clone(), s.transmit_queue.clone()) + (s.conf.clone(), s.transmit_queue.clone(), s.db.clone(), s.ws_broadcast.clone()) }; info!("send_packet {:?}", payload); + let m = UIPacket { + received_at: chrono::Utc::now(), + from_callsign: config.callsign.to_string(), + from_ssid: config.ssid, + comment: payload.comment.clone(), + }; + match build_packet(config, payload) { Ok(p) => { info!("Built packet of {} bytes", p.len()); - match transmit_queue.send(p).await { - Ok(()) => StatusCode::OK, + + match ws_broadcast.send(m) { + Ok(num) => debug!("Send own WS message to {num}"), + Err(_) => debug!("No WS receivers currently"), + } + + match transmit_queue.send(p.clone()).await { + Ok(()) => { + if let Err(e) = db.store_packet(&p[2..]).await { + warn!("Failed to write outgoing packet to sqlite: {}", e); + } + + StatusCode::OK + }, Err(_) => StatusCode::BAD_REQUEST, } }, - Err(_) =>StatusCode::INTERNAL_SERVER_ERROR, + Err(_) => StatusCode::INTERNAL_SERVER_ERROR, } } -- cgit v1.2.3