Przeglądaj źródła

Update Bluetooth Detection Strategy

JDierkse 5 lat temu
rodzic
commit
b41bbcb2ff
2 zmienionych plików z 49 dodań i 11 usunięć
  1. 44 9
      Bluetooth/Device.cpp
  2. 5 2
      Bluetooth/Device.h

+ 44 - 9
Bluetooth/Device.cpp

@@ -27,18 +27,29 @@ Device::~Device()
 
 void Device::Start()
 {
-	m_deviceTimer.StartContinuous(10000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
-	m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
+	int timeout = m_timeout * 1000;
+	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()
 {
-	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<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> addedDevices;
-	std::vector<std::string> removedDevices;
+	std::vector<std::string> ignoredDevices;
 
 	for (std::vector<std::string>::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<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);
 
+	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());
 }
 

+ 5 - 2
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;