From dcfb83fb2069bfcf4642b03453253e35479bf3da Mon Sep 17 00:00:00 2001 From: fx Date: Tue, 24 Oct 2023 01:15:22 +0200 Subject: first ping impl baseline, doesnt work --- src/routes/device.rs | 8 ++++---- src/routes/start.rs | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) (limited to 'src/routes') diff --git a/src/routes/device.rs b/src/routes/device.rs index 025c7d0..248d1e0 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -21,7 +21,7 @@ pub async fn get_device(State(state): State>, headers: Head WHERE id = $1; "#, payload.id - ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; + ).fetch_one(&state.db).await.map_err(WebolError::DB)?; Ok(Json(json!(device))) } else { @@ -46,7 +46,7 @@ pub async fn put_device(State(state): State>, headers: Head payload.id, payload.mac, payload.broadcast_addr - ).execute(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; + ).execute(&state.db).await.map_err(WebolError::DB)?; Ok(Json(json!(PutDeviceResponse { success: true }))) } else { @@ -80,7 +80,7 @@ pub async fn post_device(State(state): State>, headers: Hea payload.mac, payload.broadcast_addr, payload.id - ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; + ).fetch_one(&state.db).await.map_err(WebolError::DB)?; Ok(Json(json!(device))) } else { @@ -93,4 +93,4 @@ pub struct PostDevicePayload { id: String, mac: String, broadcast_addr: String, -} \ No newline at end of file +} diff --git a/src/routes/start.rs b/src/routes/start.rs index 163d58c..b45fe5b 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -14,7 +14,8 @@ use crate::error::WebolError; pub async fn start(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { info!("POST request"); let secret = headers.get("authorization"); - if auth(secret).map_err(WebolError::Auth)? { + let authorized = auth(secret).map_err(WebolError::Auth)?; + if authorized { let device = sqlx::query_as!( Device, r#" @@ -23,7 +24,7 @@ pub async fn start(State(state): State>, headers: HeaderMap WHERE id = $1; "#, payload.id - ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; + ).fetch_one(&state.db).await.map_err(WebolError::DB)?; info!("starting {}", device.id); @@ -32,10 +33,14 @@ pub async fn start(State(state): State>, headers: HeaderMap .unwrap_or("0.0.0.0:1111".to_string()); let _ = send_packet( - &bind_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, - &device.broadcast_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, - create_buffer(&device.mac).map_err(|err| WebolError::Server(Box::new(err)))? - ).map_err(|err| WebolError::Server(Box::new(err))); + &bind_addr.parse().map_err(WebolError::IpParse)?, + &device.broadcast_addr.parse().map_err(WebolError::IpParse)?, + create_buffer(&device.mac)? + )?; + + if payload.ping.is_some_and(|ping| ping) { + tokio::spawn(async move {crate::services::ping::spawn(state.ping_send.clone()).await}); + } Ok(Json(json!(StartResponse { id: device.id, boot: true }))) } else { Err(WebolError::Generic) @@ -45,11 +50,11 @@ pub async fn start(State(state): State>, headers: HeaderMap #[derive(Deserialize)] pub struct StartPayload { id: String, - _test: Option, + ping: Option, } #[derive(Serialize)] struct StartResponse { id: String, boot: bool, -} \ No newline at end of file +} -- cgit v1.2.3