Update UniFi Login

This commit is contained in:
2025-03-03 12:59:13 +01:00
parent dc17bbe535
commit 434eb25dda
5 changed files with 125 additions and 13 deletions
+71 -11
View File
@@ -2,6 +2,7 @@
#include <json.hpp>
#include <Logging.h>
#include <algorithm>
#include <fstream>
#include <sstream>
@@ -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<Util::StaticDevice>& 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<std::mutex> 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<std::string> presentDevices;
std::vector<PresentDevice> presentDevices;
std::vector<std::string> addedDevices;
std::vector<std::string> 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<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
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);
{
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);
@@ -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