aboutsummaryrefslogtreecommitdiff
path: root/src/services
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/services
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/services')
-rw-r--r--src/services/ping.rs45
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 @@
1use crate::config::Config; 1use crate::config::Config;
2use crate::db::Device; 2use crate::storage::Device;
3use dashmap::DashMap; 3use dashmap::DashMap;
4use ipnetwork::IpNetwork; 4use ipnetwork::IpNetwork;
5use sqlx::PgPool;
6use std::fmt::Display; 5use std::fmt::Display;
7use time::{Duration, Instant}; 6use time::{Duration, Instant};
8use tokio::sync::broadcast::Sender; 7use tokio::sync::broadcast::Sender;
@@ -13,6 +12,7 @@ pub type StatusMap = DashMap<String, Value>;
13#[derive(Debug, Clone)] 12#[derive(Debug, Clone)]
14pub struct Value { 13pub 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);