Implement use of Inventory API

This commit is contained in:
2019-04-23 11:41:34 +02:00
parent ea71693886
commit 5724bd45a7
9 changed files with 184 additions and 62 deletions
+44 -15
View File
@@ -1,6 +1,6 @@
#include <algorithm>
#include <sstream>
#include <syslog.h>
#include <boost/algorithm/string.hpp>
#include "Util/JSON.h"
#include "Functions.h"
#include "Device.h"
@@ -10,15 +10,10 @@ namespace PresenceDetection {
namespace Bluetooth {
Device::Device(const std::string& inventoryURL, const std::string& target) :
m_inventoryURL(inventoryURL),
m_target(target)
{
std::string devices = m_httpClient.GetUrlContents(inventoryURL);
Util::JSON json = Util::JSON::parse(devices);
for (auto& element : json)
if (element["bluetooth"] != "")
m_devices.push_back(element["bluetooth"]);
UpdateDevicesFromInventory();
Start();
}
@@ -28,17 +23,42 @@ Device::~Device()
void Device::Start()
{
m_timer.StartContinuous(10000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
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)));
}
void Device::Stop()
{
m_timer.Stop();
m_deviceTimer.Stop();
}
void Device::Wait()
{
m_timer.Wait();
m_deviceTimer.Wait();
}
void Device::UpdateDevicesFromInventory()
{
try
{
std::string devices = m_httpClient.GetUrlContents(m_inventoryURL);
Util::JSON json = Util::JSON::parse(devices);
m_devices.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);
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
syslog(LOG_ERR, "%s", ss.str().c_str());
}
}
void Device::UpdatePresentDevices()
@@ -49,11 +69,20 @@ void Device::UpdatePresentDevices()
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
{
if (Functions::Ping(*it))
try
{
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
SendStateChange(true, *it);
presentDevices.push_back(*it);
if (Functions::Ping(*it))
{
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
SendStateChange(true, *it);
presentDevices.push_back(*it);
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl;
syslog(LOG_ERR, "%s", ss.str().c_str());
}
}
+4 -1
View File
@@ -21,15 +21,18 @@ public:
void Wait();
private:
void UpdateDevicesFromInventory();
void UpdatePresentDevices();
void SendStateChange(bool present, const std::string& macAddress);
private:
Util::Timer m_timer;
Util::Timer m_deviceTimer;
Util::Timer m_inventoryTimer;
Util::HttpClient m_httpClient;
std::vector<std::string> m_devices;
std::string m_inventoryURL;
std::string m_target;
std::vector<std::string> m_presentDevices;