diff options
author | FxQnLr <[email protected]> | 2023-11-17 11:19:43 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2023-11-17 11:19:43 +0100 |
commit | da6367885d31698464e1bec122e3e673974427c6 (patch) | |
tree | b7ee38cc5a247151d99f0fbb75a529e5b07718e9 /src/routes/start.rs | |
parent | 4f124e6ba636e6191c2960d96d0057f3061988fc (diff) | |
parent | 075b0bdc47713e303f9954556fa4b4bb472b441a (diff) | |
download | webol-da6367885d31698464e1bec122e3e673974427c6.tar webol-da6367885d31698464e1bec122e3e673974427c6.tar.gz webol-da6367885d31698464e1bec122e3e673974427c6.zip |
Merge pull request #8 from FxQnLr/eta
Eta
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r-- | src/routes/start.rs | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs index 271f924..1555db3 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -22,7 +22,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
22 | let device = sqlx::query_as!( | 22 | let device = sqlx::query_as!( |
23 | Device, | 23 | Device, |
24 | r#" | 24 | r#" |
25 | SELECT id, mac, broadcast_addr, ip | 25 | SELECT id, mac, broadcast_addr, ip, times |
26 | FROM devices | 26 | FROM devices |
27 | WHERE id = $1; | 27 | WHERE id = $1; |
28 | "#, | 28 | "#, |
@@ -40,19 +40,31 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
40 | &device.broadcast_addr.parse().map_err(WebolError::IpParse)?, | 40 | &device.broadcast_addr.parse().map_err(WebolError::IpParse)?, |
41 | create_buffer(&device.mac)? | 41 | create_buffer(&device.mac)? |
42 | )?; | 42 | )?; |
43 | 43 | let dev_id = device.id.clone(); | |
44 | let uuid = if payload.ping.is_some_and(|ping| ping) { | 44 | let uuid = if payload.ping.is_some_and(|ping| ping) { |
45 | let uuid_gen = Uuid::new_v4().to_string(); | 45 | let mut uuid: Option<String> = None; |
46 | for (key, value) in state.ping_map.clone() { | ||
47 | if value.ip == device.ip { | ||
48 | debug!("service already exists"); | ||
49 | uuid = Some(key); | ||
50 | break; | ||
51 | } | ||
52 | }; | ||
53 | let uuid_gen = match uuid { | ||
54 | Some(u) => u, | ||
55 | None => Uuid::new_v4().to_string(), | ||
56 | }; | ||
46 | let uuid_genc = uuid_gen.clone(); | 57 | let uuid_genc = uuid_gen.clone(); |
58 | |||
47 | tokio::spawn(async move { | 59 | tokio::spawn(async move { |
48 | debug!("init ping service"); | 60 | debug!("init ping service"); |
49 | state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); | 61 | state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); |
50 | 62 | ||
51 | crate::services::ping::spawn(state.ping_send.clone(), device.ip, uuid_gen.clone(), &state.ping_map).await | 63 | crate::services::ping::spawn(state.ping_send.clone(), device, uuid_gen.clone(), &state.ping_map, &state.db).await |
52 | }); | 64 | }); |
53 | Some(uuid_genc) | 65 | Some(uuid_genc) |
54 | } else { None }; | 66 | } else { None }; |
55 | Ok(Json(json!(StartResponse { id: device.id, boot: true, uuid }))) | 67 | Ok(Json(json!(StartResponse { id: dev_id, boot: true, uuid }))) |
56 | } else { | 68 | } else { |
57 | Err(WebolError::Generic) | 69 | Err(WebolError::Generic) |
58 | } | 70 | } |