diff options
author | FxQnLr <[email protected]> | 2023-11-16 14:17:03 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2023-11-16 14:17:03 +0100 |
commit | 075b0bdc47713e303f9954556fa4b4bb472b441a (patch) | |
tree | b7ee38cc5a247151d99f0fbb75a529e5b07718e9 | |
parent | a197f4721d3b6e79c73f16c8db69ae9f3154acec (diff) | |
download | webol-075b0bdc47713e303f9954556fa4b4bb472b441a.tar webol-075b0bdc47713e303f9954556fa4b4bb472b441a.tar.gz webol-075b0bdc47713e303f9954556fa4b4bb472b441a.zip |
check if already runs and bug fix
-rw-r--r-- | src/routes/start.rs | 15 | ||||
-rw-r--r-- | src/services/ping.rs | 14 |
2 files changed, 19 insertions, 10 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs index 401ae97..1555db3 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -42,9 +42,20 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
42 | )?; | 42 | )?; |
43 | let dev_id = device.id.clone(); | 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(); |
47 | // TODO: Check if service already runs | 58 | |
48 | tokio::spawn(async move { | 59 | tokio::spawn(async move { |
49 | debug!("init ping service"); | 60 | debug!("init ping service"); |
50 | 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 }); |
diff --git a/src/services/ping.rs b/src/services/ping.rs index 2bff61f..c3bdced 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs | |||
@@ -104,15 +104,13 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { | |||
104 | async fn get_eta(db: &PgPool) -> i64 { | 104 | async fn get_eta(db: &PgPool) -> i64 { |
105 | let query = sqlx::query!( | 105 | let query = sqlx::query!( |
106 | r#"SELECT times FROM devices;"# | 106 | r#"SELECT times FROM devices;"# |
107 | ).fetch_optional(db).await.unwrap(); | 107 | ).fetch_one(db).await.unwrap(); |
108 | 108 | ||
109 | match query { | 109 | let times = match query.times { |
110 | None => { -1 }, | 110 | None => { vec![0] }, |
111 | Some(rec) => { | 111 | Some(t) => t, |
112 | let times = rec.times.unwrap(); | 112 | }; |
113 | times.iter().sum::<i64>() / times.len() as i64 | 113 | times.iter().sum::<i64>() / times.len() as i64 |
114 | } | ||
115 | } | ||
116 | 114 | ||
117 | } | 115 | } |
118 | 116 | ||