diff options
Diffstat (limited to 'src/services/ping.rs')
-rw-r--r-- | src/services/ping.rs | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/src/services/ping.rs b/src/services/ping.rs index 8cf6072..1bf022d 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs | |||
@@ -1,8 +1,7 @@ | |||
1 | use crate::config::Config; | 1 | use crate::config::Config; |
2 | use crate::db::Device; | 2 | use crate::storage::Device; |
3 | use dashmap::DashMap; | 3 | use dashmap::DashMap; |
4 | use ipnetwork::IpNetwork; | 4 | use ipnetwork::IpNetwork; |
5 | use sqlx::PgPool; | ||
6 | use std::fmt::Display; | 5 | use std::fmt::Display; |
7 | use time::{Duration, Instant}; | 6 | use time::{Duration, Instant}; |
8 | use tokio::sync::broadcast::Sender; | 7 | use tokio::sync::broadcast::Sender; |
@@ -13,6 +12,7 @@ pub type StatusMap = DashMap<String, Value>; | |||
13 | #[derive(Debug, Clone)] | 12 | #[derive(Debug, Clone)] |
14 | pub struct Value { | 13 | pub struct Value { |
15 | pub ip: IpNetwork, | 14 | pub ip: IpNetwork, |
15 | pub eta: i64, | ||
16 | pub online: bool, | 16 | pub online: bool, |
17 | } | 17 | } |
18 | 18 | ||
@@ -22,7 +22,6 @@ pub async fn spawn( | |||
22 | device: Device, | 22 | device: Device, |
23 | uuid: String, | 23 | uuid: String, |
24 | ping_map: &StatusMap, | 24 | ping_map: &StatusMap, |
25 | db: &PgPool, | ||
26 | ) { | 25 | ) { |
27 | let timer = Instant::now(); | 26 | let timer = Instant::now(); |
28 | let payload = [0; 8]; | 27 | let payload = [0; 8]; |
@@ -56,27 +55,29 @@ pub async fn spawn( | |||
56 | let _ = tx.send(msg.clone()); | 55 | let _ = tx.send(msg.clone()); |
57 | if msg.command == BroadcastCommands::Success { | 56 | if msg.command == BroadcastCommands::Success { |
58 | if timer.elapsed().whole_seconds() > config.pingthreshold { | 57 | if timer.elapsed().whole_seconds() > config.pingthreshold { |
59 | sqlx::query!( | 58 | let newtimes = if let Some(mut oldtimes) = device.times { |
60 | r#" | 59 | oldtimes.push(timer.elapsed().whole_seconds()); |
61 | UPDATE devices | 60 | oldtimes |
62 | SET times = array_append(times, $1) | 61 | } else { |
63 | WHERE id = $2; | 62 | vec![timer.elapsed().whole_seconds()] |
64 | "#, | 63 | }; |
65 | timer.elapsed().whole_seconds(), | 64 | |
66 | device.id | 65 | let updatedev = Device { |
67 | ) | 66 | id: device.id, |
68 | .execute(db) | 67 | mac: device.mac, |
69 | .await | 68 | broadcast_addr: device.broadcast_addr, |
70 | .unwrap(); | 69 | ip: device.ip, |
70 | times: Some(newtimes), | ||
71 | }; | ||
72 | updatedev.write().unwrap(); | ||
71 | } | 73 | } |
72 | 74 | ||
73 | ping_map.insert( | 75 | ping_map.alter(&uuid, |_, v| Value { |
74 | uuid.clone(), | 76 | ip: v.ip, |
75 | Value { | 77 | eta: v.eta, |
76 | ip: device.ip, | 78 | online: true, |
77 | online: true, | 79 | }); |
78 | }, | 80 | |
79 | ); | ||
80 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; | 81 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; |
81 | } | 82 | } |
82 | trace!("remove {} from ping_map", uuid); | 83 | trace!("remove {} from ping_map", uuid); |