Device.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <algorithm>
  2. #include <sstream>
  3. #include <syslog.h>
  4. #include "Util/JSON.h"
  5. #include "Functions.h"
  6. #include "Device.h"
  7. namespace PresenceDetection {
  8. namespace Bluetooth {
  9. Device::Device(const std::string& inventoryURL, const std::string& target) :
  10. m_inventoryURL(inventoryURL),
  11. m_target(target)
  12. {
  13. UpdateDevicesFromInventory();
  14. Start();
  15. }
  16. Device::~Device()
  17. {
  18. }
  19. void Device::Start()
  20. {
  21. m_deviceTimer.StartContinuous(10000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
  22. m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
  23. }
  24. void Device::Stop()
  25. {
  26. m_deviceTimer.Stop();
  27. }
  28. void Device::Wait()
  29. {
  30. m_deviceTimer.Wait();
  31. }
  32. void Device::UpdateDevicesFromInventory()
  33. {
  34. try
  35. {
  36. std::string devices = m_httpClient.GetUrlContents(m_inventoryURL);
  37. Util::JSON json = Util::JSON::parse(devices);
  38. m_devices.clear();
  39. for (auto& element : json)
  40. if (element["bluetooth"] != "")
  41. {
  42. std::string bluetooth = element["bluetooth"];
  43. std::transform(bluetooth.begin(), bluetooth.end(), bluetooth.begin(), ::tolower);
  44. m_devices.push_back(bluetooth);
  45. }
  46. }
  47. catch (const std::exception& e)
  48. {
  49. std::stringstream ss;
  50. ss << "Bluetooth::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
  51. syslog(LOG_ERR, "%s", ss.str().c_str());
  52. }
  53. }
  54. void Device::UpdatePresentDevices()
  55. {
  56. std::vector<std::string> presentDevices;
  57. std::vector<std::string> addedDevices;
  58. std::vector<std::string> removedDevices;
  59. for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
  60. {
  61. try
  62. {
  63. if (Functions::Ping(*it))
  64. {
  65. if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
  66. SendStateChange(true, *it);
  67. presentDevices.push_back(*it);
  68. }
  69. }
  70. catch (const std::exception& e)
  71. {
  72. std::stringstream ss;
  73. ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl;
  74. syslog(LOG_ERR, "%s", ss.str().c_str());
  75. }
  76. }
  77. for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
  78. if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
  79. SendStateChange(false, *it);
  80. m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
  81. }
  82. void Device::SendStateChange(bool present, const std::string& macAddress)
  83. {
  84. char sign;
  85. if (present)
  86. sign = '+';
  87. else
  88. sign = '-';
  89. std::stringstream ss;
  90. ss << "Bluetooth: " << sign << " " << macAddress;
  91. syslog(LOG_INFO, "%s", ss.str().c_str());
  92. std::stringstream url;
  93. url << m_target << "/Bluetooth/" << sign << "/" << macAddress;
  94. try
  95. {
  96. m_httpClient.GetUrlSilent(url.str());
  97. }
  98. catch (const std::exception& e)
  99. {
  100. std::stringstream ss;
  101. ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl;
  102. syslog(LOG_ERR, "%s", ss.str().c_str());
  103. }
  104. }
  105. } // namespace Bluetooth
  106. } // namespace PresenceDetection