Implement use of Inventory API
This commit is contained in:
+44
-15
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user