Files
PresenceDetection/Bluetooth/Device.cpp
T

284 lines
7.2 KiB
C++

#include "Device.h"
#include "Functions.h"
#include <json.hpp>
#include <Logging.h>
#include <algorithm>
#include <sstream>
namespace PresenceDetection {
namespace Bluetooth {
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(staticDevices)
{
if (!m_inventoryURL.empty())
UpdateDevicesFromInventory();
ClearDevices();
Start();
}
Device::~Device()
{
}
void Device::Start()
{
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_deviceTimer.Stop();
if (!m_inventoryURL.empty())
m_inventoryTimer.Stop();
}
void Device::Wait()
{
m_deviceTimer.Wait();
if (!m_inventoryURL.empty())
m_inventoryTimer.Wait();
}
void Device::ClearDevices()
{
for (std::vector<Util::DynamicDevice>::iterator it = m_dynamicDevices.begin(); it != m_dynamicDevices.end(); ++it)
{
try
{
SendStateChange(false, it->MacAddress());
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{
if (it->HasBluetoothMac())
{
try
{
SendStateChange(false, it->BluetoothMac());
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
}
}
void Device::UpdateDevicesFromInventory()
{
try
{
std::string devices = m_httpClient.GetUrlContents(m_inventoryURL);
nlohmann::json json = nlohmann::json::parse(devices);
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_dynamicDevices.push_back(Util::DynamicDevice(bluetooth));
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
void Device::UpdatePresentDevices()
{
std::vector<std::string> presentDevices;
std::vector<std::string> ignoredDevices;
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->MacAddress()) == m_presentDevices.end())
{
pollDevice = true;
}
else
{
it->IncrementProbeCounter();
if (it->ProbeCounter() * m_checkInterval >= m_timeout)
{
pollDevice = true;
it->ResetProbeCounter();
}
}
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)
{
std::stringstream ss;
ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{
if (it->HasBluetoothMac())
{
try
{
bool pollDevice = false;
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end())
{
pollDevice = true;
}
else
{
it->IncrementProbeCounter();
if (it->ProbeCounter() * m_checkInterval >= m_timeout)
{
pollDevice = true;
it->ResetProbeCounter();
}
}
if (pollDevice && Functions::Ping(it->BluetoothMac()))
{
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end())
SendStateChange(true, it->BluetoothMac());
presentDevices.push_back(it->BluetoothMac());
}
else if (pollDevice)
{
it->ResetProbeCounter();
}
else
{
ignoredDevices.push_back(it->BluetoothMac());
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
}
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() &&
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());
}
void Device::SendStateChange(bool present, const std::string& macAddress)
{
char sign;
if (present)
sign = '+';
else
sign = '-';
std::stringstream ss;
ss << "Bluetooth: " << sign << " " << macAddress;
Logging::Log(Logging::Severity::Info, ss.str());
if (!m_target.empty())
{
std::stringstream url;
url << m_target << "/Bluetooth/" << sign << "/" << macAddress;
try
{
m_httpClient.GetUrlSilent(url.str());
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{
if (it->HasBluetoothMac() && it->BluetoothMac() == macAddress)
{
it->SetBluetoothState(present);
std::vector<std::string> urls;
if (present)
{
if (!it->GetWifiState() && it->HasOnlineURL())
urls.push_back(it->OnlineURL());
if (it->HasBluetoothOnlineURL())
urls.push_back(it->BluetoothOnlineURL());
}
else if (!present)
{
if (!it->GetWifiState() && it->HasOfflineURL())
urls.push_back(it->OfflineURL());
if (it->HasBluetoothOfflineURL())
urls.push_back(it->BluetoothOfflineURL());
}
for (auto& url : urls)
{
try
{
m_httpClient.GetUrlSilent(url);
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
}
}
}
} // namespace Bluetooth
} // namespace PresenceDetection