diff options
Diffstat (limited to 'src')
-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 | ||