summaryrefslogtreecommitdiff
path: root/src/services
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-04-10 00:16:55 +0200
committerFxQnLr <[email protected]>2024-04-10 00:16:55 +0200
commit3428a637ce420baef9aa9f9803e71bd587867005 (patch)
treea1ad8234ae9bf3709794324a41e38c2f7fa58d0d /src/services
parent907e5cb5bc48899b444f7fedd85af7b5974d9a2e (diff)
downloadwebol-3428a637ce420baef9aa9f9803e71bd587867005.tar
webol-3428a637ce420baef9aa9f9803e71bd587867005.tar.gz
webol-3428a637ce420baef9aa9f9803e71bd587867005.zip
Closes #24. Changed postgres to json directory storage
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);