#include #include #include #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.StartContinuous(10000, static_cast>(std::bind(&Device::UpdatePresentDevices, this))); } void Device::Stop() { m_timer.Stop(); } void Device::Wait() { m_timer.Wait(); } void Device::UpdatePresentDevices() { std::vector presentDevices; std::vector addedDevices; std::vector removedDevices; for (std::vector::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()) SendStateChange(true, *it); presentDevices.push_back(*it); } } for (std::vector::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it) if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end()) SendStateChange(false, *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; syslog(LOG_INFO, "%s", ss.str().c_str()); 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; syslog(LOG_ERR, "%s", ss.str().c_str()); } } } // namespace Bluetooth } // namespace PresenceDetection