From 348cf5fbe4527865e8aa5cb719fd8790f4d8953e Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 30 Oct 2023 12:22:31 +0100 Subject: changed pingmap tuple to own struct --- src/main.rs | 2 +- src/routes/start.rs | 3 ++- src/services/ping.rs | 16 +++++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index ee540af..e96b736 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,7 @@ async fn main() { let (tx, _) = channel(32); - let ping_map: DashMap = DashMap::new(); + let ping_map: PingMap = DashMap::new(); let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map }); diff --git a/src/routes/start.rs b/src/routes/start.rs index 3bccb0f..c2c9378 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -11,6 +11,7 @@ use crate::config::SETTINGS; use crate::wol::{create_buffer, send_packet}; use crate::db::Device; use crate::error::WebolError; +use crate::services::ping::PingValue; #[axum_macros::debug_handler] pub async fn start(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { @@ -46,7 +47,7 @@ pub async fn start(State(state): State>, headers: HeaderMap let uuid_genc = uuid_gen.clone(); tokio::spawn(async move { debug!("Init ping service"); - state.ping_map.insert(uuid_gen.clone(), (device.ip.clone(), false)); + state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); warn!("{:?}", state.ping_map); diff --git a/src/services/ping.rs b/src/services/ping.rs index 04ad511..f0cc4a3 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs @@ -10,7 +10,13 @@ use crate::AppState; use crate::error::WebolError; -pub type PingMap = DashMap; +pub type PingMap = DashMap; + +#[derive(Debug, Clone)] +pub struct PingValue { + pub ip: String, + pub online: bool +} pub async fn spawn(tx: Sender, ip: String, uuid: String, ping_map: &PingMap) -> Result<(), WebolError> { let payload = [0; 8]; @@ -32,7 +38,7 @@ pub async fn spawn(tx: Sender, ip: String, uuid: String, ping let (_, duration) = ping.unwrap(); debug!("Ping took {:?}", duration); cont = false; - handle_broadcast_send(&tx, ip.clone(), &ping_map, uuid.clone()).await; + handle_broadcast_send(&tx, ip.clone(), ping_map, uuid.clone()).await; }; } @@ -41,7 +47,7 @@ pub async fn spawn(tx: Sender, ip: String, uuid: String, ping async fn handle_broadcast_send(tx: &Sender, ip: String, ping_map: &PingMap, uuid: String) { debug!("sending pingsuccess message"); - ping_map.insert(uuid.clone(), (ip.clone(), true)); + ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true }); let _ = tx.send(BroadcastCommands::PingSuccess(ip)); tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; trace!("remove {} from ping_map", uuid); @@ -67,7 +73,7 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc) { trace!("got device: {:?}", device); - match device.1 { + match device.online { true => { debug!("already started"); // TODO: What's better? @@ -76,7 +82,7 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc) { socket.send(Message::Close(Some(CloseFrame { code: 4001, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap(); }, false => { - let ip = device.0.to_owned(); + let ip = device.ip.to_owned(); loop{ trace!("wait for tx message"); let message = state.ping_send.subscribe().recv().await.unwrap(); -- cgit v1.2.3