Device.cpp 7.2 KB

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