From d3cf93fb6c9b7e0faf9b7907328f0a042009e164 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 15 Apr 2024 20:33:32 +0200 Subject: Closes #35. Entry of Ip optional, error on ping request without saved ip --- src/routes/device.rs | 27 ++++++++++++++++----------- src/routes/start.rs | 9 +++++++-- 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'src/routes') diff --git a/src/routes/device.rs b/src/routes/device.rs index 49361f2..f767eab 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -36,7 +36,7 @@ pub struct DPayload { id: String, mac: String, broadcast_addr: String, - ip: String, + ip: Option, } #[utoipa::path( @@ -48,15 +48,17 @@ pub struct DPayload { ), security((), ("api_key" = [])) )] -pub async fn put( - Json(payload): Json, -) -> Result, Error> { +pub async fn put(Json(payload): Json) -> Result, Error> { info!( - "add device {} ({}, {}, {})", + "add device {} ({}, {}, {:?})", payload.id, payload.mac, payload.broadcast_addr, payload.ip ); - let ip = IpNetwork::from_str(&payload.ip)?; + let ip = if let Some(ip_s) = payload.ip { + Some(IpNetwork::from_str(&ip_s)?) + } else { + None + }; let mac = MacAddress::from_str(&payload.mac)?; let device = Device { id: payload.id, @@ -79,14 +81,17 @@ pub async fn put( ), security((), ("api_key" = [])) )] -pub async fn post( - Json(payload): Json, -) -> Result, Error> { +pub async fn post(Json(payload): Json) -> Result, Error> { info!( - "edit device {} ({}, {}, {})", + "edit device {} ({}, {}, {:?})", payload.id, payload.mac, payload.broadcast_addr, payload.ip ); - let ip = IpNetwork::from_str(&payload.ip)?; + + let ip = if let Some(ip_s) = payload.ip { + Some(IpNetwork::from_str(&ip_s)?) + } else { + None + }; let mac = MacAddress::from_str(&payload.mac)?; let times = Device::read(&payload.id)?.times; diff --git a/src/routes/start.rs b/src/routes/start.rs index bbc6ab8..192a54a 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -69,6 +69,9 @@ fn send_wol( let dev_id = device.id.clone(); let uuid = if let Some(pl) = payload { if pl.ping.is_some_and(|ping| ping) { + if device.ip.is_none() { + return Err(Error::NoIpOnPing); + } Some(setup_ping(state, device)) } else { None @@ -86,8 +89,10 @@ fn send_wol( fn setup_ping(state: Arc, device: Device) -> String { let mut uuid: Option = None; + // Safe: Only called when ip is set + let ip = device.ip.unwrap(); for (key, value) in state.ping_map.clone() { - if value.ip == device.ip { + if value.ip == ip { debug!("service already exists"); uuid = Some(key); break; @@ -103,7 +108,7 @@ fn setup_ping(state: Arc, device: Device) -> String { state.ping_map.insert( uuid_gen.clone(), PingValue { - ip: device.ip, + ip, eta: get_eta(device.clone().times), online: false, }, -- cgit v1.2.3