summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs2
-rw-r--r--src/routes/start.rs3
-rw-r--r--src/services/ping.rs16
3 files changed, 14 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs
index ee540af..e96b736 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -50,7 +50,7 @@ async fn main() {
50 50
51 let (tx, _) = channel(32); 51 let (tx, _) = channel(32);
52 52
53 let ping_map: DashMap<String, (String, bool)> = DashMap::new(); 53 let ping_map: PingMap = DashMap::new();
54 54
55 let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map }); 55 let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map });
56 56
diff --git a/src/routes/start.rs b/src/routes/start.rs
index 3bccb0f..c2c9378 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -11,6 +11,7 @@ use crate::config::SETTINGS;
11use crate::wol::{create_buffer, send_packet}; 11use crate::wol::{create_buffer, send_packet};
12use crate::db::Device; 12use crate::db::Device;
13use crate::error::WebolError; 13use crate::error::WebolError;
14use crate::services::ping::PingValue;
14 15
15#[axum_macros::debug_handler] 16#[axum_macros::debug_handler]
16pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { 17pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> {
@@ -46,7 +47,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
46 let uuid_genc = uuid_gen.clone(); 47 let uuid_genc = uuid_gen.clone();
47 tokio::spawn(async move { 48 tokio::spawn(async move {
48 debug!("Init ping service"); 49 debug!("Init ping service");
49 state.ping_map.insert(uuid_gen.clone(), (device.ip.clone(), false)); 50 state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false });
50 51
51 warn!("{:?}", state.ping_map); 52 warn!("{:?}", state.ping_map);
52 53
diff --git a/src/services/ping.rs b/src/services/ping.rs
index 04ad511..f0cc4a3 100644
--- a/src/services/ping.rs
+++ b/src/services/ping.rs
@@ -10,7 +10,13 @@ use crate::AppState;
10 10
11use crate::error::WebolError; 11use crate::error::WebolError;
12 12
13pub type PingMap = DashMap<String, (String, bool)>; 13pub type PingMap = DashMap<String, PingValue>;
14
15#[derive(Debug, Clone)]
16pub struct PingValue {
17 pub ip: String,
18 pub online: bool
19}
14 20
15pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: &PingMap) -> Result<(), WebolError> { 21pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: &PingMap) -> Result<(), WebolError> {
16 let payload = [0; 8]; 22 let payload = [0; 8];
@@ -32,7 +38,7 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping
32 let (_, duration) = ping.unwrap(); 38 let (_, duration) = ping.unwrap();
33 debug!("Ping took {:?}", duration); 39 debug!("Ping took {:?}", duration);
34 cont = false; 40 cont = false;
35 handle_broadcast_send(&tx, ip.clone(), &ping_map, uuid.clone()).await; 41 handle_broadcast_send(&tx, ip.clone(), ping_map, uuid.clone()).await;
36 }; 42 };
37 } 43 }
38 44
@@ -41,7 +47,7 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping
41 47
42async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: &PingMap, uuid: String) { 48async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: &PingMap, uuid: String) {
43 debug!("sending pingsuccess message"); 49 debug!("sending pingsuccess message");
44 ping_map.insert(uuid.clone(), (ip.clone(), true)); 50 ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true });
45 let _ = tx.send(BroadcastCommands::PingSuccess(ip)); 51 let _ = tx.send(BroadcastCommands::PingSuccess(ip));
46 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; 52 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
47 trace!("remove {} from ping_map", uuid); 53 trace!("remove {} from ping_map", uuid);
@@ -67,7 +73,7 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) {
67 73
68 trace!("got device: {:?}", device); 74 trace!("got device: {:?}", device);
69 75
70 match device.1 { 76 match device.online {
71 true => { 77 true => {
72 debug!("already started"); 78 debug!("already started");
73 // TODO: What's better? 79 // TODO: What's better?
@@ -76,7 +82,7 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) {
76 socket.send(Message::Close(Some(CloseFrame { code: 4001, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap(); 82 socket.send(Message::Close(Some(CloseFrame { code: 4001, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap();
77 }, 83 },
78 false => { 84 false => {
79 let ip = device.0.to_owned(); 85 let ip = device.ip.to_owned();
80 loop{ 86 loop{
81 trace!("wait for tx message"); 87 trace!("wait for tx message");
82 let message = state.ping_send.subscribe().recv().await.unwrap(); 88 let message = state.ping_send.subscribe().recv().await.unwrap();