Update Bluetooth Detection Strategy

This commit is contained in:
2020-03-05 16:57:14 +01:00
parent 7b8fcc9722
commit b41bbcb2ff
2 changed files with 49 additions and 11 deletions
+44 -9
View File
@@ -27,18 +27,29 @@ Device::~Device()
void Device::Start() void Device::Start()
{ {
m_deviceTimer.StartContinuous(10000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this))); int timeout = m_timeout * 1000;
m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this))); m_onlineDeviceTimer.StartContinuous(timeout, static_cast<std::function<void()>>(std::bind(&Device::UpdateOnlineDevices, this)));
m_offlineDeviceTimer.StartContinuous(3000, static_cast<std::function<void()>>(std::bind(&Device::UpdateOfflineDevices, this)));
if (!m_inventoryURL.empty())
m_inventoryTimer.StartContinuous(600000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
} }
void Device::Stop() void Device::Stop()
{ {
m_deviceTimer.Stop(); m_onlineDeviceTimer.Stop();
m_offlineDeviceTimer.Stop();
if (!m_inventoryURL.empty())
m_inventoryTimer.Stop();
} }
void Device::Wait() void Device::Wait()
{ {
m_deviceTimer.Wait(); m_onlineDeviceTimer.Wait();
m_offlineDeviceTimer.Wait();
if (!m_inventoryURL.empty())
m_inventoryTimer.Wait();
}
void Device::ClearDevices() void Device::ClearDevices()
{ {
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it) for (std::vector<std::string>::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<std::string> presentDevices; std::vector<std::string> presentDevices;
std::vector<std::string> addedDevices; std::vector<std::string> ignoredDevices;
std::vector<std::string> removedDevices;
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it) for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
{ {
try 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()) if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
SendStateChange(true, *it); SendStateChange(true, *it);
presentDevices.push_back(*it); presentDevices.push_back(*it);
} }
if (!pollDevice)
ignoredDevices.push_back(*it);
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
@@ -152,10 +181,16 @@ void Device::UpdatePresentDevices()
} }
} }
} }
for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it) for (std::vector<std::string>::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); SendStateChange(false, *it);
for (std::vector<std::string>::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()); m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
} }
+5 -2
View File
@@ -24,11 +24,14 @@ public:
private: private:
void ClearDevices(); void ClearDevices();
void UpdateDevicesFromInventory(); void UpdateDevicesFromInventory();
void UpdatePresentDevices(); void UpdateOnlineDevices();
void UpdateOfflineDevices();
void UpdatePresentDevices(bool updateOnlineDevices);
void SendStateChange(bool present, const std::string& macAddress); void SendStateChange(bool present, const std::string& macAddress);
private: private:
Util::Timer m_deviceTimer; Util::Timer m_onlineDeviceTimer;
Util::Timer m_offlineDeviceTimer;
Util::Timer m_inventoryTimer; Util::Timer m_inventoryTimer;
Util::HttpClient m_httpClient; Util::HttpClient m_httpClient;