aboutsummaryrefslogtreecommitdiff
path: root/src/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/mod.rs3
-rw-r--r--src/routes/start.rs24
-rw-r--r--src/routes/status.rs12
3 files changed, 31 insertions, 8 deletions
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
index 12fbfab..d5ab0d6 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -1,2 +1,3 @@
1pub mod start; 1pub mod start;
2pub mod device; \ No newline at end of file 2pub mod device;
3pub mod status; \ No newline at end of file
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};
4use std::sync::Arc; 4use std::sync::Arc;
5use axum::extract::State; 5use axum::extract::State;
6use serde_json::{json, Value}; 6use serde_json::{json, Value};
7use tracing::{debug, info}; 7use tracing::{debug, info, warn};
8use uuid::Uuid;
8use crate::auth::auth; 9use crate::auth::auth;
9use crate::config::SETTINGS; 10use crate::config::SETTINGS;
10use crate::wol::{create_buffer, send_packet}; 11use crate::wol::{create_buffer, send_packet};
11use crate::db::Device; 12use crate::db::Device;
12use crate::error::WebolError; 13use crate::error::WebolError;
13 14
15#[axum_macros::debug_handler]
14pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { 16pub 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 {
61struct StartResponse { 70struct StartResponse {
62 id: String, 71 id: String,
63 boot: bool, 72 boot: bool,
73 uuid: Option<String>,
64} 74}
diff --git a/src/routes/status.rs b/src/routes/status.rs
new file mode 100644
index 0000000..cdecf6a
--- /dev/null
+++ b/src/routes/status.rs
@@ -0,0 +1,12 @@
1use std::sync::Arc;
2use axum::extract::{State, WebSocketUpgrade};
3use axum::response::Response;
4use serde::Deserialize;
5use crate::AppState;
6use crate::services::ping::status_websocket;
7
8#[axum_macros::debug_handler]
9pub async fn status(State(state): State<Arc<AppState>>, ws: WebSocketUpgrade) -> Response {
10 // TODO: remove unwrap
11 ws.on_upgrade(move |socket| status_websocket(socket, state.ping_send.clone(), state.ping_map.clone()))
12} \ No newline at end of file