Add Static Devices

This commit is contained in:
2020-05-01 11:39:46 +02:00
parent 157b1ceb51
commit 21dbdd41d9
12 changed files with 400 additions and 327 deletions
+58 -42
View File
@@ -9,11 +9,12 @@
namespace PresenceDetection {
namespace Bluetooth {
Device::Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::Device>& devices) :
Device::Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::StaticDevice>& staticDevices) :
m_timeout(timeout),
m_checkInterval(3),
m_inventoryURL(inventoryURL),
m_target(target),
m_staticDevices(devices)
m_staticDevices(staticDevices)
{
if (!m_inventoryURL.empty())
UpdateDevicesFromInventory();
@@ -27,36 +28,33 @@ Device::~Device()
void Device::Start()
{
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)));
int checkInterval = m_checkInterval * 1000;
m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
if (!m_inventoryURL.empty())
m_inventoryTimer.StartContinuous(600000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
}
void Device::Stop()
{
m_onlineDeviceTimer.Stop();
m_offlineDeviceTimer.Stop();
m_deviceTimer.Stop();
if (!m_inventoryURL.empty())
m_inventoryTimer.Stop();
}
void Device::Wait()
{
m_onlineDeviceTimer.Wait();
m_offlineDeviceTimer.Wait();
m_deviceTimer.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)
for (std::vector<Util::DynamicDevice>::iterator it = m_dynamicDevices.begin(); it != m_dynamicDevices.end(); ++it)
{
try
{
SendStateChange(false, *it);
SendStateChange(false, it->MacAddress());
}
catch (const std::exception& e)
{
@@ -66,7 +64,7 @@ void Device::ClearDevices()
}
}
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{
if (it->HasBluetoothMac())
{
@@ -91,13 +89,13 @@ void Device::UpdateDevicesFromInventory()
std::string devices = m_httpClient.GetUrlContents(m_inventoryURL);
Util::JSON json = Util::JSON::parse(devices);
m_devices.clear();
m_dynamicDevices.clear();
for (auto& element : json)
if (element["bluetooth"] != "")
{
std::string bluetooth = element["bluetooth"];
std::transform(bluetooth.begin(), bluetooth.end(), bluetooth.begin(), ::tolower);
m_devices.push_back(bluetooth);
m_dynamicDevices.push_back(Util::DynamicDevice(bluetooth));
}
}
catch (const std::exception& e)
@@ -108,40 +106,44 @@ void Device::UpdateDevicesFromInventory()
}
}
void Device::UpdateOnlineDevices()
{
UpdatePresentDevices(true);
}
void Device::UpdateOfflineDevices()
{
UpdatePresentDevices(false);
}
void Device::UpdatePresentDevices(bool updateOnlineDevices)
void Device::UpdatePresentDevices()
{
std::vector<std::string> presentDevices;
std::vector<std::string> ignoredDevices;
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
for (std::vector<Util::DynamicDevice>::iterator it = m_dynamicDevices.begin(); it != m_dynamicDevices.end(); ++it)
{
try
{
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->MacAddress()) == m_presentDevices.end())
{
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
SendStateChange(true, *it);
presentDevices.push_back(*it);
pollDevice = true;
}
else
{
it->IncrementProbeCounter();
if (it->ProbeCounter() * m_checkInterval >= m_timeout)
{
pollDevice = true;
it->ResetProbeCounter();
}
}
if (!pollDevice)
ignoredDevices.push_back(*it);
if (pollDevice && Functions::Ping(it->MacAddress()))
{
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->MacAddress()) == m_presentDevices.end())
SendStateChange(true, it->MacAddress());
presentDevices.push_back(it->MacAddress());
}
else if (pollDevice)
{
it->ResetProbeCounter();
}
else
{
ignoredDevices.push_back(it->MacAddress());
}
}
catch (const std::exception& e)
{
@@ -151,7 +153,7 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices)
}
}
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{
if (it->HasBluetoothMac())
{
@@ -159,9 +161,18 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices)
{
bool pollDevice = false;
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end())
{
pollDevice = true;
}
else
pollDevice = updateOnlineDevices;
{
it->IncrementProbeCounter();
if (it->ProbeCounter() * m_checkInterval >= m_timeout)
{
pollDevice = true;
it->ResetProbeCounter();
}
}
if (pollDevice && Functions::Ping(it->BluetoothMac()))
{
@@ -169,9 +180,14 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices)
SendStateChange(true, it->BluetoothMac());
presentDevices.push_back(it->BluetoothMac());
}
if (!pollDevice)
else if (pollDevice)
{
it->ResetProbeCounter();
}
else
{
ignoredDevices.push_back(it->BluetoothMac());
}
}
catch (const std::exception& e)
{
@@ -223,7 +239,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
}
}
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{
if (it->HasBluetoothMac() && it->BluetoothMac() == macAddress)
{
+8 -9
View File
@@ -1,7 +1,8 @@
#ifndef BLUETOOTH_DEVICE_H
#define BLUETOOTH_DEVICE_H
#include "Util/Device.h"
#include "Util/DynamicDevice.h"
#include "Util/StaticDevice.h"
#include <HttpClient.h>
#include <Timer.h>
#include <string>
@@ -14,7 +15,7 @@ namespace Bluetooth {
class Device
{
public:
Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::Device>& devices);
Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::StaticDevice>& staticDevices);
~Device();
void Start();
@@ -24,23 +25,21 @@ public:
private:
void ClearDevices();
void UpdateDevicesFromInventory();
void UpdateOnlineDevices();
void UpdateOfflineDevices();
void UpdatePresentDevices(bool updateOnlineDevices);
void UpdatePresentDevices();
void SendStateChange(bool present, const std::string& macAddress);
private:
Timer::Timer m_onlineDeviceTimer;
Timer::Timer m_offlineDeviceTimer;
Timer::Timer m_deviceTimer;
Timer::Timer m_inventoryTimer;
Http::HttpClient m_httpClient;
std::vector<std::string> m_devices;
std::vector<Util::DynamicDevice> m_dynamicDevices;
int m_timeout;
int m_checkInterval;
std::string m_inventoryURL;
std::string m_target;
std::vector<Util::Device> m_staticDevices;
std::vector<Util::StaticDevice> m_staticDevices;
std::vector<std::string> m_presentDevices;
};