Device.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "Device.h"
  2. #include "Util/JSON.h"
  3. #include <Logging.h>
  4. #include <algorithm>
  5. #include <sstream>
  6. namespace PresenceDetection {
  7. namespace UniFi {
  8. Device::Device(const std::string& hostname, int port, const std::string& username, const std::string& password, const std::string& cookieFile, int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::StaticDevice>& staticDevices) :
  9. m_loggedIn(false),
  10. m_hostname(hostname),
  11. m_port(port),
  12. m_username(username),
  13. m_password(password),
  14. m_cookieFile(cookieFile),
  15. m_timeout(timeout),
  16. m_checkInterval(5),
  17. m_inventoryURL(inventoryURL),
  18. m_target(target),
  19. m_staticDevices(staticDevices)
  20. {
  21. if (!m_inventoryURL.empty())
  22. UpdateDevicesFromInventory();
  23. ClearDevices();
  24. Start();
  25. }
  26. Device::~Device()
  27. {
  28. Logout();
  29. }
  30. void Device::Start()
  31. {
  32. int checkInterval = m_checkInterval * 1000;
  33. m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
  34. if (!m_inventoryURL.empty())
  35. m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
  36. }
  37. void Device::Stop()
  38. {
  39. m_deviceTimer.Stop();
  40. if (!m_inventoryURL.empty())
  41. m_inventoryTimer.Stop();
  42. }
  43. void Device::Wait()
  44. {
  45. m_deviceTimer.Wait();
  46. if (!m_inventoryURL.empty())
  47. m_inventoryTimer.Stop();
  48. }
  49. bool Device::Login()
  50. {
  51. std::stringstream url;
  52. url << "https://" << m_hostname << ":" << m_port << "/api/login";
  53. Util::JSON json;
  54. json["password"] = m_password;
  55. json["username"] = m_username;
  56. try
  57. {
  58. std::vector<std::string> headers;
  59. headers.push_back("Content-Type: application/json");
  60. std::stringstream output;
  61. output << m_httpClient.GetUrlPostContents(url.str(), headers, m_cookieFile, json.dump());
  62. Util::JSON outputJSON = Util::JSON::parse(output);
  63. if (outputJSON["meta"]["rc"] != "ok")
  64. {
  65. std::stringstream error;
  66. error << "Login Failed - " << output.str();
  67. throw std::runtime_error(error.str());
  68. }
  69. }
  70. catch (const std::exception& e)
  71. {
  72. std::stringstream ss;
  73. ss << "UniFi::Device::Login() - Error: " << e.what() << std::endl;
  74. Logging::Log(Logging::Severity::Error, ss.str());
  75. std::lock_guard<std::mutex> lock(m_mutex);
  76. m_loggedIn = false;
  77. return m_loggedIn;
  78. }
  79. std::lock_guard<std::mutex> lock(m_mutex);
  80. m_loggedIn = true;
  81. return m_loggedIn;
  82. }
  83. void Device::Logout()
  84. {
  85. std::stringstream url;
  86. url << "https://" << m_hostname << ":" << m_port << "/logout";
  87. std::lock_guard<std::mutex> lock(m_mutex);
  88. m_loggedIn = false;
  89. try
  90. {
  91. m_httpClient.GetUrlSilent(url.str(), m_cookieFile);
  92. }
  93. catch (const std::exception& e)
  94. {
  95. std::stringstream ss;
  96. ss << "UniFi::Device::Logout() - Error: " << e.what() << std::endl;
  97. Logging::Log(Logging::Severity::Error, ss.str());
  98. }
  99. }
  100. void Device::ClearDevices()
  101. {
  102. for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
  103. {
  104. try
  105. {
  106. SendStateChange(false, *it);
  107. }
  108. catch (const std::exception& e)
  109. {
  110. std::stringstream ss;
  111. ss << "UniFi::Device::ClearDevices() - Error: " << e.what() << std::endl;
  112. Logging::Log(Logging::Severity::Error, ss.str());
  113. }
  114. }
  115. for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
  116. {
  117. if (it->HasWifiMac())
  118. {
  119. try
  120. {
  121. SendStateChange(false, it->WifiMac());
  122. }
  123. catch (const std::exception& e)
  124. {
  125. std::stringstream ss;
  126. ss << "UniFi::Device::ClearDevices() - Error: " << e.what() << std::endl;
  127. Logging::Log(Logging::Severity::Error, ss.str());
  128. }
  129. }
  130. }
  131. }
  132. void Device::UpdateDevicesFromInventory()
  133. {
  134. try
  135. {
  136. std::string devices = m_httpClient.GetUrlContents(m_inventoryURL);
  137. Util::JSON json = Util::JSON::parse(devices);
  138. m_devices.clear();
  139. for (auto& element : json)
  140. if (element["macaddress"] != "")
  141. {
  142. std::string macAddress = element["macaddress"];
  143. std::transform(macAddress.begin(), macAddress.end(), macAddress.begin(), ::tolower);
  144. m_devices.push_back(macAddress);
  145. }
  146. }
  147. catch (const std::exception& e)
  148. {
  149. std::stringstream ss;
  150. ss << "UniFi::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
  151. Logging::Log(Logging::Severity::Error, ss.str());
  152. }
  153. }
  154. void Device::UpdatePresentDevices()
  155. {
  156. bool loggedIn;
  157. {
  158. std::lock_guard<std::mutex> lock(m_mutex);
  159. loggedIn = m_loggedIn;
  160. }
  161. if (!loggedIn)
  162. if (!Login())
  163. return;
  164. std::stringstream url;
  165. url << "https://" << m_hostname << ":" << m_port << "/api/s/default/stat/sta";
  166. std::time_t timeStamp = std::time(nullptr);
  167. std::vector<std::string> presentDevices;
  168. std::vector<std::string> addedDevices;
  169. std::vector<std::string> removedDevices;
  170. try
  171. {
  172. std::stringstream output;
  173. output << m_httpClient.GetUrlContents(url.str(), m_cookieFile);
  174. Util::JSON json = Util::JSON::parse(output);
  175. if (json["meta"]["rc"] != "ok")
  176. {
  177. std::stringstream error;
  178. error << "Query Failed";
  179. throw std::runtime_error(error.str());
  180. }
  181. for (auto& device : json["data"])
  182. {
  183. std::string macAddress = device["mac"];
  184. std::transform(macAddress.begin(), macAddress.end(), macAddress.begin(), ::tolower);
  185. int lastSeen = device["last_seen"];
  186. if (std::find(m_devices.begin(), m_devices.end(), macAddress) != m_devices.end())
  187. {
  188. if ((timeStamp - lastSeen) < m_timeout)
  189. {
  190. if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
  191. addedDevices.push_back(macAddress);
  192. presentDevices.push_back(macAddress);
  193. }
  194. else
  195. {
  196. if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) != m_presentDevices.end())
  197. {
  198. std::stringstream ss;
  199. ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl;
  200. Logging::Log(Logging::Severity::Debug, ss.str());
  201. }
  202. }
  203. }
  204. for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
  205. {
  206. if ((timeStamp - lastSeen) < m_timeout)
  207. {
  208. if (it->HasWifiMac() && it->WifiMac() == macAddress)
  209. {
  210. if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
  211. addedDevices.push_back(macAddress);
  212. presentDevices.push_back(macAddress);
  213. }
  214. }
  215. else
  216. {
  217. if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) != m_presentDevices.end())
  218. {
  219. std::stringstream ss;
  220. ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl;
  221. Logging::Log(Logging::Severity::Debug, ss.str());
  222. }
  223. }
  224. }
  225. }
  226. }
  227. catch (const std::exception& e)
  228. {
  229. std::stringstream ss;
  230. ss << "UniFi::Device::IsDevicePresent() - Error: " << e.what() << std::endl;
  231. Logging::Log(Logging::Severity::Error, ss.str());
  232. Logout();
  233. return;
  234. }
  235. for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
  236. if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
  237. removedDevices.push_back(*it);
  238. for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
  239. SendStateChange(true, *it);
  240. for (std::vector<std::string>::iterator it = removedDevices.begin(); it != removedDevices.end(); ++it)
  241. SendStateChange(false, *it);
  242. m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
  243. }
  244. void Device::SendStateChange(bool present, const std::string& macAddress)
  245. {
  246. char sign;
  247. if (present)
  248. sign = '+';
  249. else
  250. sign = '-';
  251. std::stringstream ss;
  252. ss << "UniFi: " << sign << " " << macAddress;
  253. Logging::Log(Logging::Severity::Info, ss.str());
  254. if (!m_target.empty())
  255. {
  256. std::stringstream url;
  257. url << m_target << "/UniFi/" << sign << "/" << macAddress;
  258. try
  259. {
  260. m_httpClient.GetUrlSilent(url.str());
  261. }
  262. catch (const std::exception& e)
  263. {
  264. std::stringstream ss;
  265. ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl;
  266. Logging::Log(Logging::Severity::Error, ss.str());
  267. }
  268. }
  269. for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
  270. {
  271. if (it->HasWifiMac() && it->WifiMac() == macAddress)
  272. {
  273. it->SetWifiState(present);
  274. std::vector<std::string> urls;
  275. if (present)
  276. {
  277. if (!it->GetBluetoothState() && it->HasOnlineURL())
  278. urls.push_back(it->OnlineURL());
  279. if (it->HasWifiOnlineURL())
  280. urls.push_back(it->WifiOnlineURL());
  281. }
  282. else if (!present)
  283. {
  284. if (!it->GetBluetoothState() && it->HasOfflineURL())
  285. urls.push_back(it->OfflineURL());
  286. if (it->HasWifiOfflineURL())
  287. urls.push_back(it->WifiOfflineURL());
  288. }
  289. for (auto& url : urls)
  290. {
  291. try
  292. {
  293. m_httpClient.GetUrlSilent(url);
  294. }
  295. catch (const std::exception& e)
  296. {
  297. std::stringstream ss;
  298. ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl;
  299. Logging::Log(Logging::Severity::Error, ss.str());
  300. }
  301. }
  302. }
  303. }
  304. }
  305. } // namespace UniFi
  306. } // namespace PresenceDetection