Files
PresenceDetection/Bluetooth/Device.cpp
T

104 lines
2.4 KiB
C++

#include <sstream>
#include <syslog.h>
#include <boost/algorithm/string.hpp>
#include "Functions.h"
#include "Device.h"
namespace PresenceDetection {
namespace Bluetooth {
Device::Device(const std::string& devices, const std::string& target) :
m_target(target)
{
boost::split(m_devices, devices, boost::is_any_of(","));
Start();
}
Device::~Device()
{
}
void Device::Start()
{
m_timer.Start(10000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
}
void Device::Stop()
{
m_timer.Stop();
}
void Device::Wait()
{
m_timer.Wait();
}
void Device::UpdatePresentDevices()
{
std::vector<std::string> presentDevices;
std::vector<std::string> addedDevices;
std::vector<std::string> removedDevices;
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
{
if (Functions::Ping(*it))
{
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
addedDevices.push_back(*it);
presentDevices.push_back(*it);
}
}
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())
removedDevices.push_back(*it);
for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
{
std::stringstream ss;
ss << "Bluetooth: + " << *it;
syslog(LOG_INFO, "%s", ss.str().c_str());
std::stringstream url;
url << m_target << "/Bluetooth/+/" << *it;
try
{
m_httpClient.GetUrlSilent(url.str());
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "BluetoothDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
syslog(LOG_ERR, "%s", ss.str().c_str());
}
}
for (std::vector<std::string>::iterator it = removedDevices.begin(); it != removedDevices.end(); ++it)
{
std::stringstream ss;
ss << "Bluetooth: - " << *it;
syslog(LOG_INFO, "%s", ss.str().c_str());
std::stringstream url;
url << m_target << "/Bluetooth/-/" << *it;
try
{
m_httpClient.GetUrlSilent(url.str());
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "BluetoothDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
syslog(LOG_ERR, "%s", ss.str().c_str());
}
}
m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
}
} // namespace Bluetooth
} // namespace PresenceDetection