From 9b20f4976f5e3775ea7a65f0d1f838037065ec90 Mon Sep 17 00:00:00 2001 From: Evgenii Vilkov Date: Thu, 2 Jan 2025 00:08:11 +0100 Subject: [PATCH] send all data to newly connected devices --- src/main.rs | 14 +++++++++----- src/providers/time.rs | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 522c7a2..323f0a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,14 +100,18 @@ fn start(data_sender: broadcast::Sender>, mut is_connected_receiver: mps connected_count += if is_connected { 1 } else { -1 }; tracing::info!("Connected devices: {}", connected_count); - if connected_count > 0 && !is_started { - tracing::info!("Starting providers"); - is_started = true; - providers.iter().for_each(|p| p.start()); - } else if connected_count == 0 && is_started { + // if new device is connected - restart providers to send all available data + if is_started && (connected_count == 0 || is_connected) { tracing::info!("Stopping providers"); is_started = false; providers.iter().for_each(|p| p.stop()); + std::thread::sleep(std::time::Duration::from_millis(200)); + } + + if !is_started && connected_count > 0 { + tracing::info!("Starting providers"); + is_started = true; + providers.iter().for_each(|p| p.start()); } } } diff --git a/src/providers/time.rs b/src/providers/time.rs index 8057f41..83efcaa 100644 --- a/src/providers/time.rs +++ b/src/providers/time.rs @@ -53,7 +53,7 @@ impl Provider for TimeProvider { send_data(&synced_time, &data_sender); } - std::thread::sleep(std::time::Duration::from_secs(1)); + std::thread::sleep(std::time::Duration::from_millis(100)); } tracing::info!("Time Provider stopped");