Device.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <algorithm>
  2. #include <sstream>
  3. #include "Util/JSON.h"
  4. #include "Util/Logger.h"
  5. #include "Functions.h"
  6. #include "Device.h"
  7. namespace PresenceDetection {
  8. namespace Bluetooth {
  9. Device::Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::Device>& devices) :
  10. m_timeout(timeout),
  11. m_inventoryURL(inventoryURL),
  12. m_target(target),
  13. m_staticDevices(devices)
  14. {
  15. if (!m_inventoryURL.empty())
  16. UpdateDevicesFromInventory();
  17. ClearDevices();
  18. Start();
  19. }
  20. Device::~Device()
  21. {
  22. }
  23. void Device::Start()
  24. {
  25. m_deviceTimer.StartContinuous(10000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
  26. m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
  27. }
  28. void Device::Stop()
  29. {
  30. m_deviceTimer.Stop();
  31. }
  32. void Device::Wait()
  33. {
  34. m_deviceTimer.Wait();
  35. void Device::ClearDevices()
  36. {
  37. for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
  38. {
  39. try
  40. {
  41. SendStateChange(false, *it);
  42. }
  43. catch (const std::exception& e)
  44. {
  45. std::stringstream ss;
  46. ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl;
  47. Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
  48. }
  49. }
  50. for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
  51. {
  52. if (it->HasBluetoothMac())
  53. {
  54. try
  55. {
  56. SendStateChange(false, it->BluetoothMac());
  57. }
  58. catch (const std::exception& e)
  59. {
  60. std::stringstream ss;
  61. ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl;
  62. Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
  63. }
  64. }
  65. }
  66. }
  67. void Device::UpdateDevicesFromInventory()
  68. {
  69. try
  70. {
  71. std::string devices = m_httpClient.GetUrlContents(m_inventoryURL);
  72. Util::JSON json = Util::JSON::parse(devices);
  73. m_devices.clear();
  74. for (auto& element : json)
  75. if (element["bluetooth"] != "")
  76. {
  77. std::string bluetooth = element["bluetooth"];
  78. std::transform(bluetooth.begin(), bluetooth.end(), bluetooth.begin(), ::tolower);
  79. m_devices.push_back(bluetooth);
  80. }
  81. }
  82. catch (const std::exception& e)
  83. {
  84. std::stringstream ss;
  85. ss << "Bluetooth::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
  86. Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
  87. }
  88. }
  89. void Device::UpdatePresentDevices()
  90. {
  91. std::vector<std::string> presentDevices;
  92. std::vector<std::string> addedDevices;
  93. std::vector<std::string> removedDevices;
  94. for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
  95. {
  96. try
  97. {
  98. if (Functions::Ping(*it))
  99. {
  100. if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
  101. SendStateChange(true, *it);
  102. presentDevices.push_back(*it);
  103. }
  104. }
  105. catch (const std::exception& e)
  106. {
  107. std::stringstream ss;
  108. ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl;
  109. Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
  110. }
  111. }
  112. for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
  113. {
  114. if (it->HasBluetoothMac())
  115. {
  116. try
  117. {
  118. bool pollDevice = false;
  119. if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end())
  120. pollDevice = true;
  121. else
  122. pollDevice = updateOnlineDevices;
  123. if (pollDevice && Functions::Ping(it->BluetoothMac()))
  124. {
  125. if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end())
  126. SendStateChange(true, it->BluetoothMac());
  127. presentDevices.push_back(it->BluetoothMac());
  128. }
  129. if (!pollDevice)
  130. ignoredDevices.push_back(it->BluetoothMac());
  131. }
  132. catch (const std::exception& e)
  133. {
  134. std::stringstream ss;
  135. ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl;
  136. Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
  137. }
  138. }
  139. }
  140. for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
  141. if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
  142. SendStateChange(false, *it);
  143. m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
  144. }
  145. void Device::SendStateChange(bool present, const std::string& macAddress)
  146. {
  147. char sign;
  148. if (present)
  149. sign = '+';
  150. else
  151. sign = '-';
  152. std::stringstream ss;
  153. ss << "Bluetooth: " << sign << " " << macAddress;
  154. Util::Logger::Log(Util::Logger::Severity::Info, ss.str());
  155. if (!m_target.empty())
  156. {
  157. std::stringstream url;
  158. url << m_target << "/Bluetooth/" << sign << "/" << macAddress;
  159. try
  160. {
  161. m_httpClient.GetUrlSilent(url.str());
  162. }
  163. catch (const std::exception& e)
  164. {
  165. std::stringstream ss;
  166. ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl;
  167. Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
  168. }
  169. }
  170. for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
  171. {
  172. if (it->HasBluetoothMac() && it->BluetoothMac() == macAddress)
  173. {
  174. it->SetBluetoothState(present);
  175. std::vector<std::string> urls;
  176. if (present)
  177. {
  178. if (!it->GetWifiState() && it->HasOnlineURL())
  179. urls.push_back(it->OnlineURL());
  180. if (it->HasBluetoothOnlineURL())
  181. urls.push_back(it->BluetoothOnlineURL());
  182. }
  183. else if (!present)
  184. {
  185. if (!it->GetWifiState() && it->HasOfflineURL())
  186. urls.push_back(it->OfflineURL());
  187. if (it->HasBluetoothOfflineURL())
  188. urls.push_back(it->BluetoothOfflineURL());
  189. }
  190. for (auto& url : urls)
  191. {
  192. try
  193. {
  194. m_httpClient.GetUrlSilent(url);
  195. }
  196. catch (const std::exception& e)
  197. {
  198. std::stringstream ss;
  199. ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl;
  200. Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
  201. }
  202. }
  203. }
  204. }
  205. }
  206. } // namespace Bluetooth
  207. } // namespace PresenceDetection