| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #include "Driver.h"
- #include <json.hpp>
- #include <Logging.h>
- #include <algorithm>
- #include <fstream>
- #include <sstream>
- namespace PresenceDetection {
- namespace WiFi {
- Driver::Driver(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) :
- m_httpClient(5),
- m_loggedIn(false),
- m_hostname(hostname),
- m_port(port),
- m_username(username),
- m_password(password),
- m_cookieFile(cookieFile),
- m_timeout(timeout),
- m_checkInterval(5),
- m_inventoryURL(inventoryURL),
- m_target(target),
- m_staticDevices(staticDevices)
- {
- if (!m_inventoryURL.empty())
- UpdateDevicesFromInventory();
- ClearDevices();
- Start();
- }
- Driver::~Driver()
- {
- Logout();
- }
- void Driver::Start()
- {
- int checkInterval = m_checkInterval * 1000;
- m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Driver::UpdatePresentDevices, this)));
- if (!m_inventoryURL.empty())
- m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Driver::UpdateDevicesFromInventory, this)));
- }
- void Driver::Stop()
- {
- m_deviceTimer.Stop();
- if (!m_inventoryURL.empty())
- m_inventoryTimer.Stop();
- }
- void Driver::Wait()
- {
- m_deviceTimer.Wait();
- if (!m_inventoryURL.empty())
- m_inventoryTimer.Stop();
- }
- bool Driver::Login()
- {
- std::stringstream url;
- url << "https://" << m_hostname << ":" << m_port << "/api/auth/login";
- nlohmann::json json;
- json["password"] = m_password;
- json["username"] = m_username;
- try
- {
- std::vector<std::string> headers;
- headers.push_back("Content-Type: application/json");
- std::stringstream output;
- Http::HttpRequest request(url.str());
- request.Method(Http::HttpRequest::Method::POST);
- request.Headers(headers);
- request.CookieFile(m_cookieFile);
- request.Data(json.dump());
- output << m_httpClient.Open(request);
- nlohmann::json outputJSON = nlohmann::json::parse(output);
- if (outputJSON["code"] == "AUTHENTICATION_FAILED_INVALID_CREDENTIALS")
- {
- std::stringstream error;
- error << "Login Failed - " << output.str();
- throw std::runtime_error(error.str());
- }
- }
- catch (const std::exception& e)
- {
- std::stringstream ss;
- ss << "WiFi::Driver::Login() - Error: " << e.what() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- std::lock_guard<std::mutex> lock(m_mutex);
- m_loggedIn = false;
- return m_loggedIn;
- }
- std::lock_guard<std::mutex> lock(m_mutex);
- m_loggedIn = true;
- return m_loggedIn;
- }
- void Driver::Logout()
- {
- // std::stringstream url;
- // url << "https://" << m_hostname << ":" << m_port << "/api/auth/logout";
- std::lock_guard<std::mutex> lock(m_mutex);
- m_loggedIn = false;
- ClearCookiesFile();
- /*
- try
- {
- Http::HttpRequest request(url.str());
- request.ReturnType(Http::HttpRequest::ReturnType::None);
- request.CookieFile(m_cookieFile);
- m_httpClient.Open(request);
- }
- catch (const std::exception& e)
- {
- std::stringstream ss;
- ss << "WiFi::Driver::Logout() - Error: " << e.what() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- }
- */
- }
- void Driver::ClearDevices()
- {
- for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
- {
- try
- {
- SendStateChange(false, *it);
- }
- catch (const std::exception& e)
- {
- std::stringstream ss;
- ss << "WiFi::Driver::ClearDevices() - Error: " << e.what() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- }
- }
- for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
- {
- if (it->HasWifiMac())
- {
- try
- {
- SendStateChange(false, it->WifiMac());
- }
- catch (const std::exception& e)
- {
- std::stringstream ss;
- ss << "WiFi::Driver::ClearDevices() - Error: " << e.what() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- }
- }
- }
- }
- void Driver::UpdateDevicesFromInventory()
- {
- try
- {
- Http::HttpRequest request(m_inventoryURL);
- std::string devices = m_httpClient.Open(request);
- nlohmann::json json = nlohmann::json::parse(devices);
- m_devices.clear();
- for (auto& element : json)
- if (element["macaddress"] != "")
- {
- std::string macAddress = element["macaddress"];
- std::transform(macAddress.begin(), macAddress.end(), macAddress.begin(), ::tolower);
- m_devices.push_back(macAddress);
- }
- }
- catch (const std::exception& e)
- {
- std::stringstream ss;
- ss << "WiFi::Driver::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- }
- }
- void Driver::UpdatePresentDevices()
- {
- bool loggedIn;
- {
- std::lock_guard<std::mutex> lock(m_mutex);
- loggedIn = m_loggedIn;
- }
- if (!loggedIn)
- if (!Login())
- return;
- std::stringstream url;
- url << "https://" << m_hostname << ":" << m_port << "/proxy/network/api/s/default/stat/sta";
- std::time_t timeStamp = std::time(nullptr);
- std::vector<PresentDevice> presentDevices;
- std::vector<std::string> addedDevices;
- std::vector<std::string> removedDevices;
- try
- {
- std::stringstream output;
- Http::HttpRequest request(url.str());
- request.CookieFile(m_cookieFile);
- output << m_httpClient.Open(request);
- nlohmann::json json = nlohmann::json::parse(output);
- //Logging::Log(Logging::Severity::Debug, output.str());
- if (json["meta"]["rc"] != "ok")
- {
- std::stringstream error;
- error << "Query Failed";
- throw std::runtime_error(error.str());
- }
- for (auto& device : json["data"])
- {
- std::string macAddress = device["mac"];
- std::transform(macAddress.begin(), macAddress.end(), macAddress.begin(), ::tolower);
- int lastSeen = device["last_seen"];
- if (std::find(m_devices.begin(), m_devices.end(), macAddress) != m_devices.end())
- {
- if ((timeStamp - lastSeen) < m_timeout)
- {
- if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
- {
- addedDevices.push_back(macAddress);
- std::stringstream ss;
- ss << "Device Added: " << device.dump() << std::endl;
- Logging::Log(Logging::Severity::Info, ss.str());
- }
- presentDevices.push_back(PresentDevice(macAddress, 0));
- }
- else
- {
- if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) != m_presentDevices.end())
- {
- std::stringstream ss;
- ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl;
- Logging::Log(Logging::Severity::Info, ss.str());
- }
- }
- }
- for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
- {
- if ((timeStamp - lastSeen) < m_timeout)
- {
- if (it->HasWifiMac() && it->WifiMac() == macAddress)
- {
- if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
- addedDevices.push_back(macAddress);
- presentDevices.push_back(PresentDevice(macAddress, 0));
- }
- }
- else
- {
- if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) != m_presentDevices.end())
- {
- std::stringstream ss;
- ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl;
- Logging::Log(Logging::Severity::Info, ss.str());
- }
- }
- }
- }
- }
- catch (const std::exception& e)
- {
- std::stringstream ss;
- ss << "WiFi::Driver::UpdatePresentDevices() - Error: " << e.what() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- Logout();
- return;
- }
- for (std::vector<PresentDevice>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
- {
- if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
- {
- removedDevices.push_back((*it).MacAddress);
- std::stringstream ss;
- ss << "Device Removed: " << (*it).MacAddress << std::endl;
- Logging::Log(Logging::Severity::Info, ss.str());
- }
- }
- for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
- SendStateChange(true, *it);
- for (std::vector<std::string>::iterator it = removedDevices.begin(); it != removedDevices.end(); ++it)
- SendStateChange(false, *it);
- m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
- }
- void Driver::SendStateChange(bool present, const std::string& macAddress)
- {
- char sign;
- if (present)
- sign = '+';
- else
- sign = '-';
- std::stringstream ss;
- ss << "WiFi: " << sign << " " << macAddress;
- Logging::Log(Logging::Severity::Info, ss.str());
- if (!m_target.empty())
- {
- std::stringstream url;
- url << m_target << "/WiFi/" << sign << "/" << macAddress;
- try
- {
- Http::HttpRequest request(url.str());
- request.ReturnType(Http::HttpRequest::ReturnType::None);
- m_httpClient.Open(request);
- }
- catch (const std::exception& e)
- {
- std::stringstream ss;
- ss << "WiFi::Driver::SendStateChange() - Error: " << e.what() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- }
- }
- for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
- {
- if (it->HasWifiMac() && it->WifiMac() == macAddress)
- {
- it->SetWifiState(present);
- std::vector<std::string> urls;
- if (present)
- {
- if (!it->GetBluetoothState() && it->HasOnlineURL())
- urls.push_back(it->OnlineURL());
- if (it->HasWifiOnlineURL())
- urls.push_back(it->WifiOnlineURL());
- }
- else if (!present)
- {
- if (!it->GetBluetoothState() && it->HasOfflineURL())
- urls.push_back(it->OfflineURL());
- if (it->HasWifiOfflineURL())
- urls.push_back(it->WifiOfflineURL());
- }
- for (auto& url : urls)
- {
- try
- {
- Http::HttpRequest request(url);
- request.ReturnType(Http::HttpRequest::ReturnType::None);
- m_httpClient.Open(request);
- }
- catch (const std::exception& e)
- {
- std::stringstream ss;
- ss << "WiFi::Driver::SendStateChange() - Error: " << e.what() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- }
- }
- }
- }
- }
- void Driver::ClearCookiesFile()
- {
- std::ifstream file;
- file.open(m_cookieFile.c_str(), std::ifstream::out | std::ifstream::trunc);
- if (!file.is_open() || file.fail())
- {
- file.close();
- std::stringstream ss;
- ss << "WiFi::Driver::ClearCookiesFile() - Error: Failed to erase file content." << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- }
- file.close();
- }
- Driver::PresentDevice::PresentDevice(const std::string& macAddress) :
- MacAddress(macAddress)
- {
- }
- Driver::PresentDevice::PresentDevice(const std::string& macAddress, int lastSeen) :
- MacAddress(macAddress),
- LastSeen(lastSeen)
- {
- }
- bool Driver::PresentDevice::operator==(const Driver::PresentDevice& other) const
- {
- return MacAddress == other.MacAddress;
- }
- bool Driver::PresentDevice::operator!=(const Driver::PresentDevice& other) const
- {
- return !(*this == other);
- }
- bool Driver::PresentDevice::operator==(const std::string& other) const
- {
- return MacAddress == other;
- }
- bool Driver::PresentDevice::operator!=(const std::string& other) const
- {
- return !(*this == other);
- }
- } // namespace WiFi
- } // namespace PresenceDetection
|