diff options
author | FxQnLr <[email protected]> | 2024-02-18 21:16:46 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-02-18 21:16:46 +0100 |
commit | 2f9f18b80a9e2134f674f345e48a5f21de5efadd (patch) | |
tree | c4202bb5c1a490233e89d928cf8c5b91258d4c90 /src/routes/start.rs | |
parent | 016fa3a31f8847d3f52800941b7f8fe5ef872240 (diff) | |
download | webol-2f9f18b80a9e2134f674f345e48a5f21de5efadd.tar webol-2f9f18b80a9e2134f674f345e48a5f21de5efadd.tar.gz webol-2f9f18b80a9e2134f674f345e48a5f21de5efadd.zip |
Refactor stuff. Use Postgres Types
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r-- | src/routes/start.rs | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs index ec4f98f..4888325 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -12,7 +12,6 @@ use std::sync::Arc; | |||
12 | use tracing::{debug, info}; | 12 | use tracing::{debug, info}; |
13 | use uuid::Uuid; | 13 | use uuid::Uuid; |
14 | 14 | ||
15 | #[axum_macros::debug_handler] | ||
16 | pub async fn start( | 15 | pub async fn start( |
17 | State(state): State<Arc<crate::AppState>>, | 16 | State(state): State<Arc<crate::AppState>>, |
18 | headers: HeaderMap, | 17 | headers: HeaderMap, |
@@ -41,45 +40,11 @@ pub async fn start( | |||
41 | let _ = send_packet( | 40 | let _ = send_packet( |
42 | bind_addr, | 41 | bind_addr, |
43 | &device.broadcast_addr, | 42 | &device.broadcast_addr, |
44 | &create_buffer(&device.mac)?, | 43 | &create_buffer(&device.mac.to_string())?, |
45 | )?; | 44 | )?; |
46 | let dev_id = device.id.clone(); | 45 | let dev_id = device.id.clone(); |
47 | let uuid = if payload.ping.is_some_and(|ping| ping) { | 46 | let uuid = if payload.ping.is_some_and(|ping| ping) { |
48 | let mut uuid: Option<String> = None; | 47 | Some(setup_ping(state, device)) |
49 | for (key, value) in state.ping_map.clone() { | ||
50 | if value.ip == device.ip { | ||
51 | debug!("service already exists"); | ||
52 | uuid = Some(key); | ||
53 | break; | ||
54 | } | ||
55 | } | ||
56 | let uuid_gen = match uuid { | ||
57 | Some(u) => u, | ||
58 | None => Uuid::new_v4().to_string(), | ||
59 | }; | ||
60 | let uuid_genc = uuid_gen.clone(); | ||
61 | |||
62 | tokio::spawn(async move { | ||
63 | debug!("init ping service"); | ||
64 | state.ping_map.insert( | ||
65 | uuid_gen.clone(), | ||
66 | PingValue { | ||
67 | ip: device.ip.clone(), | ||
68 | online: false, | ||
69 | }, | ||
70 | ); | ||
71 | |||
72 | crate::services::ping::spawn( | ||
73 | state.ping_send.clone(), | ||
74 | &state.config, | ||
75 | device, | ||
76 | uuid_gen.clone(), | ||
77 | &state.ping_map, | ||
78 | &state.db, | ||
79 | ) | ||
80 | .await; | ||
81 | }); | ||
82 | Some(uuid_genc) | ||
83 | } else { | 48 | } else { |
84 | None | 49 | None |
85 | }; | 50 | }; |
@@ -93,6 +58,45 @@ pub async fn start( | |||
93 | } | 58 | } |
94 | } | 59 | } |
95 | 60 | ||
61 | fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String { | ||
62 | let mut uuid: Option<String> = None; | ||
63 | for (key, value) in state.ping_map.clone() { | ||
64 | if value.ip == device.ip { | ||
65 | debug!("service already exists"); | ||
66 | uuid = Some(key); | ||
67 | break; | ||
68 | } | ||
69 | } | ||
70 | let uuid_gen = match uuid { | ||
71 | Some(u) => u, | ||
72 | None => Uuid::new_v4().to_string(), | ||
73 | }; | ||
74 | let uuid_ret = uuid_gen.clone(); | ||
75 | |||
76 | debug!("init ping service"); | ||
77 | state.ping_map.insert( | ||
78 | uuid_gen.clone(), | ||
79 | PingValue { | ||
80 | ip: device.ip, | ||
81 | online: false, | ||
82 | }, | ||
83 | ); | ||
84 | |||
85 | tokio::spawn(async move { | ||
86 | crate::services::ping::spawn( | ||
87 | state.ping_send.clone(), | ||
88 | &state.config, | ||
89 | device, | ||
90 | uuid_gen, | ||
91 | &state.ping_map, | ||
92 | &state.db, | ||
93 | ) | ||
94 | .await; | ||
95 | }); | ||
96 | |||
97 | uuid_ret | ||
98 | } | ||
99 | |||
96 | #[derive(Deserialize)] | 100 | #[derive(Deserialize)] |
97 | pub struct Payload { | 101 | pub struct Payload { |
98 | id: String, | 102 | id: String, |