diff options
author | FxQnLr <[email protected]> | 2023-10-29 21:09:46 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2023-10-29 21:09:46 +0100 |
commit | dd303dc41e4d500e48760f79351720cc2c1c9ffe (patch) | |
tree | b730a1da1e0e58e12494bbab88076d403f4ccd97 /src/routes | |
parent | 84c32953ae5f52be44af4b48381747f55cb04f4a (diff) | |
download | webol-dd303dc41e4d500e48760f79351720cc2c1c9ffe.tar webol-dd303dc41e4d500e48760f79351720cc2c1c9ffe.tar.gz webol-dd303dc41e4d500e48760f79351720cc2c1c9ffe.zip |
add ip to database and use for ping, remove arc from pingmap
Diffstat (limited to 'src/routes')
-rw-r--r-- | src/routes/device.rs | 16 | ||||
-rw-r--r-- | src/routes/start.rs | 11 | ||||
-rw-r--r-- | src/routes/status.rs | 2 |
3 files changed, 16 insertions, 13 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs index 248d1e0..7353733 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs | |||
@@ -16,7 +16,7 @@ pub async fn get_device(State(state): State<Arc<crate::AppState>>, headers: Head | |||
16 | let device = sqlx::query_as!( | 16 | let device = sqlx::query_as!( |
17 | Device, | 17 | Device, |
18 | r#" | 18 | r#" |
19 | SELECT id, mac, broadcast_addr | 19 | SELECT id, mac, broadcast_addr, ip |
20 | FROM devices | 20 | FROM devices |
21 | WHERE id = $1; | 21 | WHERE id = $1; |
22 | "#, | 22 | "#, |
@@ -40,12 +40,13 @@ pub async fn put_device(State(state): State<Arc<crate::AppState>>, headers: Head | |||
40 | if auth(secret).map_err(WebolError::Auth)? { | 40 | if auth(secret).map_err(WebolError::Auth)? { |
41 | sqlx::query!( | 41 | sqlx::query!( |
42 | r#" | 42 | r#" |
43 | INSERT INTO devices (id, mac, broadcast_addr) | 43 | INSERT INTO devices (id, mac, broadcast_addr, ip) |
44 | VALUES ($1, $2, $3); | 44 | VALUES ($1, $2, $3, $4); |
45 | "#, | 45 | "#, |
46 | payload.id, | 46 | payload.id, |
47 | payload.mac, | 47 | payload.mac, |
48 | payload.broadcast_addr | 48 | payload.broadcast_addr, |
49 | payload.ip | ||
49 | ).execute(&state.db).await.map_err(WebolError::DB)?; | 50 | ).execute(&state.db).await.map_err(WebolError::DB)?; |
50 | 51 | ||
51 | Ok(Json(json!(PutDeviceResponse { success: true }))) | 52 | Ok(Json(json!(PutDeviceResponse { success: true }))) |
@@ -59,6 +60,7 @@ pub struct PutDevicePayload { | |||
59 | id: String, | 60 | id: String, |
60 | mac: String, | 61 | mac: String, |
61 | broadcast_addr: String, | 62 | broadcast_addr: String, |
63 | ip: String | ||
62 | } | 64 | } |
63 | 65 | ||
64 | #[derive(Serialize)] | 66 | #[derive(Serialize)] |
@@ -74,11 +76,12 @@ pub async fn post_device(State(state): State<Arc<crate::AppState>>, headers: Hea | |||
74 | Device, | 76 | Device, |
75 | r#" | 77 | r#" |
76 | UPDATE devices | 78 | UPDATE devices |
77 | SET mac = $1, broadcast_addr = $2 WHERE id = $3 | 79 | SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4 |
78 | RETURNING id, mac, broadcast_addr; | 80 | RETURNING id, mac, broadcast_addr, ip; |
79 | "#, | 81 | "#, |
80 | payload.mac, | 82 | payload.mac, |
81 | payload.broadcast_addr, | 83 | payload.broadcast_addr, |
84 | payload.ip, | ||
82 | payload.id | 85 | payload.id |
83 | ).fetch_one(&state.db).await.map_err(WebolError::DB)?; | 86 | ).fetch_one(&state.db).await.map_err(WebolError::DB)?; |
84 | 87 | ||
@@ -93,4 +96,5 @@ pub struct PostDevicePayload { | |||
93 | id: String, | 96 | id: String, |
94 | mac: String, | 97 | mac: String, |
95 | broadcast_addr: String, | 98 | broadcast_addr: String, |
99 | ip: String, | ||
96 | } | 100 | } |
diff --git a/src/routes/start.rs b/src/routes/start.rs index 5b73281..3bccb0f 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 | 25 | SELECT id, mac, broadcast_addr, ip |
26 | FROM devices | 26 | FROM devices |
27 | WHERE id = $1; | 27 | WHERE id = $1; |
28 | "#, | 28 | "#, |
@@ -44,16 +44,15 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
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 uuid_gen = Uuid::new_v4().to_string(); |
46 | let uuid_genc = uuid_gen.clone(); | 46 | let uuid_genc = uuid_gen.clone(); |
47 | let uuid_gencc = uuid_gen.clone(); | 47 | tokio::spawn(async move { |
48 | tokio::spawn(async move{ | ||
49 | debug!("Init ping service"); | 48 | debug!("Init ping service"); |
50 | state.ping_map.insert(uuid_gen, ("192.168.178.94".to_string(), false)); | 49 | state.ping_map.insert(uuid_gen.clone(), (device.ip.clone(), false)); |
51 | 50 | ||
52 | warn!("{:?}", state.ping_map); | 51 | warn!("{:?}", state.ping_map); |
53 | 52 | ||
54 | crate::services::ping::spawn(state.ping_send.clone(), "192.168.178.94".to_string(), uuid_genc.clone(), state.ping_map.clone()).await | 53 | crate::services::ping::spawn(state.ping_send.clone(), device.ip, uuid_gen.clone(), &state.ping_map).await |
55 | }); | 54 | }); |
56 | Some(uuid_gencc) | 55 | Some(uuid_genc) |
57 | } else { None }; | 56 | } else { None }; |
58 | Ok(Json(json!(StartResponse { id: device.id, boot: true, uuid }))) | 57 | Ok(Json(json!(StartResponse { id: device.id, boot: true, uuid }))) |
59 | } else { | 58 | } else { |
diff --git a/src/routes/status.rs b/src/routes/status.rs index 4a5ec67..45f3e51 100644 --- a/src/routes/status.rs +++ b/src/routes/status.rs | |||
@@ -6,5 +6,5 @@ use crate::services::ping::status_websocket; | |||
6 | 6 | ||
7 | #[axum_macros::debug_handler] | 7 | #[axum_macros::debug_handler] |
8 | pub async fn status(State(state): State<Arc<AppState>>, ws: WebSocketUpgrade) -> Response { | 8 | pub async fn status(State(state): State<Arc<AppState>>, ws: WebSocketUpgrade) -> Response { |
9 | ws.on_upgrade(move |socket| status_websocket(socket, state.ping_send.clone(), state.ping_map.clone())) | 9 | ws.on_upgrade(move |socket| status_websocket(socket, state)) |
10 | } \ No newline at end of file | 10 | } \ No newline at end of file |