Device.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <sstream>
  2. #include <syslog.h>
  3. #include <boost/algorithm/string.hpp>
  4. #include "Functions.h"
  5. #include "Device.h"
  6. namespace PresenceDetection {
  7. namespace Bluetooth {
  8. Device::Device(const std::string& devices, const std::string& target) :
  9. m_target(target)
  10. {
  11. boost::split(m_devices, devices, boost::is_any_of(","));
  12. Start();
  13. }
  14. Device::~Device()
  15. {
  16. }
  17. void Device::Start()
  18. {
  19. m_timer.Start(10000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
  20. }
  21. void Device::Stop()
  22. {
  23. m_timer.Stop();
  24. }
  25. void Device::Wait()
  26. {
  27. m_timer.Wait();
  28. }
  29. void Device::UpdatePresentDevices()
  30. {
  31. std::vector<std::string> presentDevices;
  32. std::vector<std::string> addedDevices;
  33. std::vector<std::string> removedDevices;
  34. for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
  35. {
  36. if (Functions::Ping(*it))
  37. {
  38. if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
  39. addedDevices.push_back(*it);
  40. presentDevices.push_back(*it);
  41. }
  42. }
  43. for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
  44. if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
  45. removedDevices.push_back(*it);
  46. for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
  47. {
  48. std::stringstream ss;
  49. ss << "Bluetooth: + " << *it;
  50. syslog(LOG_INFO, "%s", ss.str().c_str());
  51. std::stringstream url;
  52. url << m_target << "/Bluetooth/+/" << *it;
  53. try
  54. {
  55. m_httpClient.GetUrlSilent(url.str());
  56. }
  57. catch (const std::exception& e)
  58. {
  59. std::stringstream ss;
  60. ss << "BluetoothDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
  61. syslog(LOG_ERR, "%s", ss.str().c_str());
  62. }
  63. }
  64. for (std::vector<std::string>::iterator it = removedDevices.begin(); it != removedDevices.end(); ++it)
  65. {
  66. std::stringstream ss;
  67. ss << "Bluetooth: - " << *it;
  68. syslog(LOG_INFO, "%s", ss.str().c_str());
  69. std::stringstream url;
  70. url << m_target << "/Bluetooth/-/" << *it;
  71. try
  72. {
  73. m_httpClient.GetUrlSilent(url.str());
  74. }
  75. catch (const std::exception& e)
  76. {
  77. std::stringstream ss;
  78. ss << "BluetoothDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
  79. syslog(LOG_ERR, "%s", ss.str().c_str());
  80. }
  81. }
  82. m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
  83. }
  84. } // namespace Bluetooth
  85. } // namespace PresenceDetection