diff --git a/.gitignore b/.gitignore index a85b626..8b86973 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ PresenceDetection* !PresenceDetection.ini test* !test.cc +Test* fixPermissions.sh diff --git a/Application/Test.cc b/Application/Test.cc index a241732..8381840 100644 --- a/Application/Test.cc +++ b/Application/Test.cc @@ -1,9 +1,12 @@ #include "Bluetooth/Functions.h" #include "Logic/Logic.h" +#include "Util/StaticDevice.h" +#include "WiFi/Driver.h" #include "WiFi/Functions.h" #include #include #include +#include #include @@ -86,11 +89,40 @@ void TestPing() std::cout << "--------------------------------" << std::endl << std::endl; } +void TestUniFi() +{ + std::shared_ptr pWiFiDriver; + + std::string hostname = "UniFi-Controller"; + int port = 8443; + std::string username = "admin"; + std::string password = "Wh1sK3y196909"; + std::string cookiefile = "UniFi_Cookies"; + int timeout = 60; + std::string inventoryURL = "http://DomoticaPi:8080/api/inventory/WirelessDevice"; + std::string target = ""; + std::vector staticDevices; + + pWiFiDriver = std::make_shared(hostname, port, username, password, cookiefile, timeout, inventoryURL, target, staticDevices); + + sigset_t wset; + sigemptyset(&wset); + sigaddset(&wset,SIGHUP); + sigaddset(&wset,SIGINT); + sigaddset(&wset,SIGTERM); + int sig; + sigwait(&wset,&sig); + + pWiFiDriver->Stop(); + pWiFiDriver->Wait(); +} + int main(int /*argc*/, char** /*argv[]*/) { //TestINIReader(); //TestLogic(); - TestPing(); + //TestPing(); + TestUniFi(); return 0; } diff --git a/WiFi/Driver.cpp b/WiFi/Driver.cpp index f309200..443454e 100644 --- a/WiFi/Driver.cpp +++ b/WiFi/Driver.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -9,6 +10,7 @@ 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& staticDevices) : + m_httpClient(5), m_loggedIn(false), m_hostname(hostname), m_port(port), @@ -57,7 +59,7 @@ void Driver::Wait() bool Driver::Login() { std::stringstream url; - url << "https://" << m_hostname << ":" << m_port << "/api/login"; + url << "https://" << m_hostname << ":" << m_port << "/api/auth/login"; nlohmann::json json; json["password"] = m_password; @@ -78,7 +80,7 @@ bool Driver::Login() nlohmann::json outputJSON = nlohmann::json::parse(output); - if (outputJSON["meta"]["rc"] != "ok") + if (outputJSON["code"] == "AUTHENTICATION_FAILED_INVALID_CREDENTIALS") { std::stringstream error; error << "Login Failed - " << output.str(); @@ -102,12 +104,14 @@ bool Driver::Login() void Driver::Logout() { - std::stringstream url; - url << "https://" << m_hostname << ":" << m_port << "/logout"; +// std::stringstream url; +// url << "https://" << m_hostname << ":" << m_port << "/api/auth/logout"; std::lock_guard lock(m_mutex); m_loggedIn = false; + ClearCookiesFile(); +/* try { Http::HttpRequest request(url.str()); @@ -121,6 +125,7 @@ void Driver::Logout() ss << "WiFi::Driver::Logout() - Error: " << e.what() << std::endl; Logging::Log(Logging::Severity::Error, ss.str()); } +*/ } void Driver::ClearDevices() @@ -195,10 +200,10 @@ void Driver::UpdatePresentDevices() return; std::stringstream url; - url << "https://" << m_hostname << ":" << m_port << "/api/s/default/stat/sta"; + url << "https://" << m_hostname << ":" << m_port << "/proxy/network/api/s/default/stat/sta"; std::time_t timeStamp = std::time(nullptr); - std::vector presentDevices; + std::vector presentDevices; std::vector addedDevices; std::vector removedDevices; @@ -210,6 +215,7 @@ void Driver::UpdatePresentDevices() output << m_httpClient.Open(request); nlohmann::json json = nlohmann::json::parse(output); + //Logging::Log(Logging::Severity::Debug, output.str()); if (json["meta"]["rc"] != "ok") { @@ -235,7 +241,7 @@ void Driver::UpdatePresentDevices() ss << "Device Added: " << device.dump() << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); } - presentDevices.push_back(macAddress); + presentDevices.push_back(PresentDevice(macAddress, 0)); } else { @@ -256,7 +262,7 @@ void Driver::UpdatePresentDevices() { if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end()) addedDevices.push_back(macAddress); - presentDevices.push_back(macAddress); + presentDevices.push_back(PresentDevice(macAddress, 0)); } } else @@ -274,15 +280,22 @@ void Driver::UpdatePresentDevices() catch (const std::exception& e) { std::stringstream ss; - ss << "WiFi::Driver::IsDevicePresent() - Error: " << e.what() << std::endl; + ss << "WiFi::Driver::UpdatePresentDevices() - Error: " << e.what() << std::endl; Logging::Log(Logging::Severity::Error, ss.str()); Logout(); return; } - for (std::vector::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it) + for (std::vector::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it) + { if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end()) - removedDevices.push_back(*it); + { + 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::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it) SendStateChange(true, *it); @@ -366,5 +379,52 @@ void Driver::SendStateChange(bool present, const std::string& macAddress) } } +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 diff --git a/WiFi/Driver.h b/WiFi/Driver.h index 31b6e57..1ddf167 100644 --- a/WiFi/Driver.h +++ b/WiFi/Driver.h @@ -31,6 +31,24 @@ private: void UpdatePresentDevices(); void SendStateChange(bool present, const std::string& macAddress); + void ClearCookiesFile(); + +private: + struct PresentDevice + { + PresentDevice(const std::string& macAddress); + PresentDevice(const std::string& macAddress, int lastSeen); + + std::string MacAddress; + int LastSeen; + + bool operator==(const PresentDevice& other) const; + bool operator!=(const PresentDevice& other) const; + + bool operator==(const std::string& other) const; + bool operator!=(const std::string& other) const; + }; + private: Timer::Timer m_deviceTimer; Timer::Timer m_inventoryTimer; @@ -52,7 +70,7 @@ private: std::string m_target; std::vector m_staticDevices; - std::vector m_presentDevices; + std::vector m_presentDevices; }; } // namespace WiFi diff --git a/WiFi/Functions.h b/WiFi/Functions.h index 899d5e5..738f49d 100644 --- a/WiFi/Functions.h +++ b/WiFi/Functions.h @@ -1,6 +1,7 @@ #ifndef WIFI_FUNCTIONS_H #define WIFI_FUNCTIONS_H +#include #include