diff options
author | FxQnLr <[email protected]> | 2024-04-11 09:20:04 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-04-11 09:20:04 +0200 |
commit | 6b05d1a437a49db98056de7b029923e8aedf1a5a (patch) | |
tree | bc70f14cae1760e91369705273904c0de1bfbf75 /src/routes/start.rs | |
parent | 907e5cb5bc48899b444f7fedd85af7b5974d9a2e (diff) | |
parent | 2476e182f61d209768635e8eca6e75b4acfbd007 (diff) | |
download | webol-6b05d1a437a49db98056de7b029923e8aedf1a5a.tar webol-6b05d1a437a49db98056de7b029923e8aedf1a5a.tar.gz webol-6b05d1a437a49db98056de7b029923e8aedf1a5a.zip |
Merge pull request #32 from FxQnLr/0.4.0
0.4.0
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r-- | src/routes/start.rs | 100 |
1 files changed, 22 insertions, 78 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs index ff3d1be..ae2b384 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use crate::db::Device; | 1 | use crate::storage::Device; |
2 | use crate::error::Error; | 2 | use crate::error::Error; |
3 | use crate::services::ping::Value as PingValue; | 3 | use crate::services::ping::Value as PingValue; |
4 | use crate::wol::{create_buffer, send_packet}; | 4 | use crate::wol::send_packet; |
5 | use axum::extract::{Path, State}; | 5 | use axum::extract::{Path, State}; |
6 | use axum::Json; | 6 | use axum::Json; |
7 | use serde::{Deserialize, Serialize}; | 7 | use serde::{Deserialize, Serialize}; |
@@ -13,57 +13,8 @@ use uuid::Uuid; | |||
13 | 13 | ||
14 | #[utoipa::path( | 14 | #[utoipa::path( |
15 | post, | 15 | post, |
16 | path = "/start", | ||
17 | request_body = PayloadOld, | ||
18 | responses( | ||
19 | (status = 200, description = "DEP", body = [Response]) | ||
20 | ), | ||
21 | security((), ("api_key" = [])) | ||
22 | )] | ||
23 | #[deprecated] | ||
24 | pub async fn start_payload( | ||
25 | State(state): State<Arc<crate::AppState>>, | ||
26 | Json(payload): Json<PayloadOld>, | ||
27 | ) -> Result<Json<Value>, Error> { | ||
28 | info!("POST request"); | ||
29 | let device = sqlx::query_as!( | ||
30 | Device, | ||
31 | r#" | ||
32 | SELECT id, mac, broadcast_addr, ip, times | ||
33 | FROM devices | ||
34 | WHERE id = $1; | ||
35 | "#, | ||
36 | payload.id | ||
37 | ) | ||
38 | .fetch_one(&state.db) | ||
39 | .await?; | ||
40 | |||
41 | info!("starting {}", device.id); | ||
42 | |||
43 | let bind_addr = "0.0.0.0:0"; | ||
44 | |||
45 | let _ = send_packet( | ||
46 | bind_addr, | ||
47 | &device.broadcast_addr, | ||
48 | &create_buffer(&device.mac.to_string())?, | ||
49 | )?; | ||
50 | let dev_id = device.id.clone(); | ||
51 | let uuid = if payload.ping.is_some_and(|ping| ping) { | ||
52 | Some(setup_ping(state, device)) | ||
53 | } else { | ||
54 | None | ||
55 | }; | ||
56 | Ok(Json(json!(Response { | ||
57 | id: dev_id, | ||
58 | boot: true, | ||
59 | uuid | ||
60 | }))) | ||
61 | } | ||
62 | |||
63 | #[utoipa::path( | ||
64 | post, | ||
65 | path = "/start/{id}", | 16 | path = "/start/{id}", |
66 | request_body = Option<Payload>, | 17 | request_body = Option<SPayload>, |
67 | responses( | 18 | responses( |
68 | (status = 200, description = "start the device with the given id", body = [Response]) | 19 | (status = 200, description = "start the device with the given id", body = [Response]) |
69 | ), | 20 | ), |
@@ -75,9 +26,9 @@ pub async fn start_payload( | |||
75 | pub async fn post( | 26 | pub async fn post( |
76 | State(state): State<Arc<crate::AppState>>, | 27 | State(state): State<Arc<crate::AppState>>, |
77 | Path(id): Path<String>, | 28 | Path(id): Path<String>, |
78 | payload: Option<Json<Payload>>, | 29 | payload: Option<Json<SPayload>>, |
79 | ) -> Result<Json<Value>, Error> { | 30 | ) -> Result<Json<Value>, Error> { |
80 | send_wol(state, &id, payload).await | 31 | send_wol(state, &id, payload) |
81 | } | 32 | } |
82 | 33 | ||
83 | #[utoipa::path( | 34 | #[utoipa::path( |
@@ -95,26 +46,16 @@ pub async fn get( | |||
95 | State(state): State<Arc<crate::AppState>>, | 46 | State(state): State<Arc<crate::AppState>>, |
96 | Path(id): Path<String>, | 47 | Path(id): Path<String>, |
97 | ) -> Result<Json<Value>, Error> { | 48 | ) -> Result<Json<Value>, Error> { |
98 | send_wol(state, &id, None).await | 49 | send_wol(state, &id, None) |
99 | } | 50 | } |
100 | 51 | ||
101 | async fn send_wol( | 52 | fn send_wol( |
102 | state: Arc<crate::AppState>, | 53 | state: Arc<crate::AppState>, |
103 | id: &str, | 54 | id: &str, |
104 | payload: Option<Json<Payload>>, | 55 | payload: Option<Json<SPayload>>, |
105 | ) -> Result<Json<Value>, Error> { | 56 | ) -> Result<Json<Value>, Error> { |
106 | info!("Start request for {id}"); | 57 | info!("start request for {id}"); |
107 | let device = sqlx::query_as!( | 58 | let device = Device::read(id)?; |
108 | Device, | ||
109 | r#" | ||
110 | SELECT id, mac, broadcast_addr, ip, times | ||
111 | FROM devices | ||
112 | WHERE id = $1; | ||
113 | "#, | ||
114 | id | ||
115 | ) | ||
116 | .fetch_one(&state.db) | ||
117 | .await?; | ||
118 | 59 | ||
119 | info!("starting {}", device.id); | 60 | info!("starting {}", device.id); |
120 | 61 | ||
@@ -122,8 +63,8 @@ async fn send_wol( | |||
122 | 63 | ||
123 | let _ = send_packet( | 64 | let _ = send_packet( |
124 | bind_addr, | 65 | bind_addr, |
125 | &device.broadcast_addr, | 66 | &device.broadcast_addr.to_string(), |
126 | &create_buffer(&device.mac.to_string())?, | 67 | &device.mac.bytes() |
127 | )?; | 68 | )?; |
128 | let dev_id = device.id.clone(); | 69 | let dev_id = device.id.clone(); |
129 | let uuid = if let Some(pl) = payload { | 70 | let uuid = if let Some(pl) = payload { |
@@ -163,6 +104,7 @@ fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String { | |||
163 | uuid_gen.clone(), | 104 | uuid_gen.clone(), |
164 | PingValue { | 105 | PingValue { |
165 | ip: device.ip, | 106 | ip: device.ip, |
107 | eta: get_eta(device.clone().times), | ||
166 | online: false, | 108 | online: false, |
167 | }, | 109 | }, |
168 | ); | 110 | ); |
@@ -174,7 +116,6 @@ fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String { | |||
174 | device, | 116 | device, |
175 | uuid_gen, | 117 | uuid_gen, |
176 | &state.ping_map, | 118 | &state.ping_map, |
177 | &state.db, | ||
178 | ) | 119 | ) |
179 | .await; | 120 | .await; |
180 | }); | 121 | }); |
@@ -182,15 +123,18 @@ fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String { | |||
182 | uuid_ret | 123 | uuid_ret |
183 | } | 124 | } |
184 | 125 | ||
185 | #[derive(Deserialize, ToSchema)] | 126 | fn get_eta(times: Option<Vec<i64>>) -> i64 { |
186 | #[deprecated] | 127 | let times = if let Some(times) = times { |
187 | pub struct PayloadOld { | 128 | times |
188 | id: String, | 129 | } else { |
189 | ping: Option<bool>, | 130 | vec![0] |
131 | }; | ||
132 | |||
133 | times.iter().sum::<i64>() / i64::try_from(times.len()).unwrap() | ||
190 | } | 134 | } |
191 | 135 | ||
192 | #[derive(Deserialize, ToSchema)] | 136 | #[derive(Deserialize, ToSchema)] |
193 | pub struct Payload { | 137 | pub struct SPayload { |
194 | ping: Option<bool>, | 138 | ping: Option<bool>, |
195 | } | 139 | } |
196 | 140 | ||