summaryrefslogtreecommitdiff
path: root/src/routes/status.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-04-11 09:20:04 +0200
committerGitHub <[email protected]>2024-04-11 09:20:04 +0200
commit6b05d1a437a49db98056de7b029923e8aedf1a5a (patch)
treebc70f14cae1760e91369705273904c0de1bfbf75 /src/routes/status.rs
parent907e5cb5bc48899b444f7fedd85af7b5974d9a2e (diff)
parent2476e182f61d209768635e8eca6e75b4acfbd007 (diff)
downloadwebol-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/status.rs')
-rw-r--r--src/routes/status.rs24
1 files changed, 4 insertions, 20 deletions
diff --git a/src/routes/status.rs b/src/routes/status.rs
index 0e25f7d..b38202b 100644
--- a/src/routes/status.rs
+++ b/src/routes/status.rs
@@ -3,7 +3,6 @@ use crate::AppState;
3use axum::extract::ws::{Message, WebSocket}; 3use axum::extract::ws::{Message, WebSocket};
4use axum::extract::{State, WebSocketUpgrade}; 4use axum::extract::{State, WebSocketUpgrade};
5use axum::response::Response; 5use axum::response::Response;
6use sqlx::PgPool;
7use std::sync::Arc; 6use std::sync::Arc;
8use tracing::{debug, trace}; 7use tracing::{debug, trace};
9 8
@@ -18,13 +17,13 @@ pub async fn websocket(mut socket: WebSocket, state: Arc<AppState>) {
18 17
19 trace!("Search for uuid: {}", uuid); 18 trace!("Search for uuid: {}", uuid);
20 19
21 let eta = get_eta(&state.db).await;
22 let _ = socket
23 .send(Message::Text(format!("eta_{eta}_{uuid}")))
24 .await;
25 20
26 let device_exists = state.ping_map.contains_key(&uuid); 21 let device_exists = state.ping_map.contains_key(&uuid);
27 if device_exists { 22 if device_exists {
23 let eta = state.ping_map.get(&uuid).unwrap().eta;
24 let _ = socket
25 .send(Message::Text(format!("eta_{eta}_{uuid}")))
26 .await;
28 let _ = socket 27 let _ = socket
29 .send(receive_ping_broadcast(state.clone(), uuid).await) 28 .send(receive_ping_broadcast(state.clone(), uuid).await)
30 .await; 29 .await;
@@ -62,18 +61,3 @@ async fn receive_ping_broadcast(state: Arc<AppState>, uuid: String) -> Message {
62 } 61 }
63 } 62 }
64} 63}
65
66async fn get_eta(db: &PgPool) -> i64 {
67 let query = sqlx::query!(r#"SELECT times FROM devices;"#)
68 .fetch_one(db)
69 .await
70 .unwrap();
71
72 let times = if let Some(times) = query.times {
73 times
74 } else {
75 vec![0]
76 };
77
78 times.iter().sum::<i64>() / i64::try_from(times.len()).unwrap()
79}