diff options
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r-- | src/routes/start.rs | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs index 863ef16..45e7ec8 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -4,15 +4,18 @@ use serde::{Deserialize, Serialize}; | |||
4 | use std::sync::Arc; | 4 | use std::sync::Arc; |
5 | use axum::extract::State; | 5 | use axum::extract::State; |
6 | use serde_json::{json, Value}; | 6 | use serde_json::{json, Value}; |
7 | use tracing::{debug, info}; | 7 | use tracing::{debug, info, warn}; |
8 | use uuid::Uuid; | ||
8 | use crate::auth::auth; | 9 | use crate::auth::auth; |
9 | use crate::config::SETTINGS; | 10 | use crate::config::SETTINGS; |
10 | use crate::wol::{create_buffer, send_packet}; | 11 | use crate::wol::{create_buffer, send_packet}; |
11 | use crate::db::Device; | 12 | use crate::db::Device; |
12 | use crate::error::WebolError; | 13 | use crate::error::WebolError; |
13 | 14 | ||
15 | #[axum_macros::debug_handler] | ||
14 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { | 16 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { |
15 | info!("POST request"); | 17 | info!("POST request"); |
18 | warn!("{:?}", state.ping_map); | ||
16 | let secret = headers.get("authorization"); | 19 | let secret = headers.get("authorization"); |
17 | let authorized = auth(secret).map_err(WebolError::Auth)?; | 20 | let authorized = auth(secret).map_err(WebolError::Auth)?; |
18 | if authorized { | 21 | if authorized { |
@@ -38,14 +41,20 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
38 | create_buffer(&device.mac)? | 41 | create_buffer(&device.mac)? |
39 | )?; | 42 | )?; |
40 | 43 | ||
41 | if payload.ping.is_some_and(|ping| ping) { | 44 | let uuid = if payload.ping.is_some_and(|ping| ping) { |
42 | debug!("ping true"); | 45 | let uuid_gen = Uuid::new_v4().to_string(); |
43 | tokio::spawn(async move { | 46 | let uuid_genc = uuid_gen.clone(); |
47 | tokio::spawn(async move{ | ||
44 | debug!("Init ping service"); | 48 | debug!("Init ping service"); |
45 | crate::services::ping::spawn(state.ping_send.clone()).await | 49 | state.ping_map.lock().await.insert(uuid_gen, ("192.168.178.94".to_string(), false)); |
50 | |||
51 | warn!("{:?}", state.ping_map); | ||
52 | |||
53 | crate::services::ping::spawn(state.ping_send.clone(), "192.168.178.94".to_string()).await; | ||
46 | }); | 54 | }); |
47 | }; | 55 | Some(uuid_genc) |
48 | Ok(Json(json!(StartResponse { id: device.id, boot: true }))) | 56 | } else { None }; |
57 | Ok(Json(json!(StartResponse { id: device.id, boot: true, uuid }))) | ||
49 | } else { | 58 | } else { |
50 | Err(WebolError::Generic) | 59 | Err(WebolError::Generic) |
51 | } | 60 | } |
@@ -61,4 +70,5 @@ pub struct StartPayload { | |||
61 | struct StartResponse { | 70 | struct StartResponse { |
62 | id: String, | 71 | id: String, |
63 | boot: bool, | 72 | boot: bool, |
73 | uuid: Option<String>, | ||
64 | } | 74 | } |