Device.cpp 6.9 KB

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