From b41bbcb2ff82414afba20a3aa35a65c99099fcef Mon Sep 17 00:00:00 2001 From: JDierkse Date: Thu, 5 Mar 2020 16:57:14 +0100 Subject: [PATCH] Update Bluetooth Detection Strategy --- Bluetooth/Device.cpp | 53 ++++++++++++++++++++++++++++++++++++-------- Bluetooth/Device.h | 7 ++++-- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/Bluetooth/Device.cpp b/Bluetooth/Device.cpp index a896a8a..4e72869 100644 --- a/Bluetooth/Device.cpp +++ b/Bluetooth/Device.cpp @@ -27,18 +27,29 @@ Device::~Device() void Device::Start() { - m_deviceTimer.StartContinuous(10000, static_cast>(std::bind(&Device::UpdatePresentDevices, this))); - m_inventoryTimer.StartContinuous(300000, static_cast>(std::bind(&Device::UpdateDevicesFromInventory, this))); + int timeout = m_timeout * 1000; + m_onlineDeviceTimer.StartContinuous(timeout, static_cast>(std::bind(&Device::UpdateOnlineDevices, this))); + m_offlineDeviceTimer.StartContinuous(3000, static_cast>(std::bind(&Device::UpdateOfflineDevices, this))); + if (!m_inventoryURL.empty()) + m_inventoryTimer.StartContinuous(600000, static_cast>(std::bind(&Device::UpdateDevicesFromInventory, this))); } void Device::Stop() { - m_deviceTimer.Stop(); + m_onlineDeviceTimer.Stop(); + m_offlineDeviceTimer.Stop(); + if (!m_inventoryURL.empty()) + m_inventoryTimer.Stop(); } void Device::Wait() { - m_deviceTimer.Wait(); + m_onlineDeviceTimer.Wait(); + m_offlineDeviceTimer.Wait(); + if (!m_inventoryURL.empty()) + m_inventoryTimer.Wait(); +} + void Device::ClearDevices() { for (std::vector::iterator it = m_devices.begin(); it != m_devices.end(); ++it) @@ -97,22 +108,40 @@ void Device::UpdateDevicesFromInventory() } } -void Device::UpdatePresentDevices() +void Device::UpdateOnlineDevices() +{ + UpdatePresentDevices(true); +} + +void Device::UpdateOfflineDevices() +{ + UpdatePresentDevices(false); +} + +void Device::UpdatePresentDevices(bool updateOnlineDevices) { std::vector presentDevices; - std::vector addedDevices; - std::vector removedDevices; + std::vector ignoredDevices; for (std::vector::iterator it = m_devices.begin(); it != m_devices.end(); ++it) { try { - if (Functions::Ping(*it)) + bool pollDevice = false; + if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end()) + pollDevice = true; + else + pollDevice = updateOnlineDevices; + + if (pollDevice && Functions::Ping(*it)) { if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end()) SendStateChange(true, *it); presentDevices.push_back(*it); } + + if (!pollDevice) + ignoredDevices.push_back(*it); } catch (const std::exception& e) { @@ -152,10 +181,16 @@ void Device::UpdatePresentDevices() } } } + for (std::vector::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it) - if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end()) + if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end() && + std::find(ignoredDevices.begin(), ignoredDevices.end(), *it) == ignoredDevices.end()) SendStateChange(false, *it); + for (std::vector::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it) + if (std::find(ignoredDevices.begin(), ignoredDevices.end(), *it) != ignoredDevices.end()) + presentDevices.push_back(*it); + m_presentDevices.assign(presentDevices.begin(), presentDevices.end()); } diff --git a/Bluetooth/Device.h b/Bluetooth/Device.h index 2706bd2..a329e94 100644 --- a/Bluetooth/Device.h +++ b/Bluetooth/Device.h @@ -24,11 +24,14 @@ public: private: void ClearDevices(); void UpdateDevicesFromInventory(); - void UpdatePresentDevices(); + void UpdateOnlineDevices(); + void UpdateOfflineDevices(); + void UpdatePresentDevices(bool updateOnlineDevices); void SendStateChange(bool present, const std::string& macAddress); private: - Util::Timer m_deviceTimer; + Util::Timer m_onlineDeviceTimer; + Util::Timer m_offlineDeviceTimer; Util::Timer m_inventoryTimer; Util::HttpClient m_httpClient;