Update UniFi Login
This commit is contained in:
@@ -12,4 +12,5 @@ PresenceDetection*
|
|||||||
!PresenceDetection.ini
|
!PresenceDetection.ini
|
||||||
test*
|
test*
|
||||||
!test.cc
|
!test.cc
|
||||||
|
Test*
|
||||||
fixPermissions.sh
|
fixPermissions.sh
|
||||||
|
|||||||
+33
-1
@@ -1,9 +1,12 @@
|
|||||||
#include "Bluetooth/Functions.h"
|
#include "Bluetooth/Functions.h"
|
||||||
#include "Logic/Logic.h"
|
#include "Logic/Logic.h"
|
||||||
|
#include "Util/StaticDevice.h"
|
||||||
|
#include "WiFi/Driver.h"
|
||||||
#include "WiFi/Functions.h"
|
#include "WiFi/Functions.h"
|
||||||
#include <INIReader.h>
|
#include <INIReader.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <signal.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
@@ -86,11 +89,40 @@ void TestPing()
|
|||||||
std::cout << "--------------------------------" << std::endl << std::endl;
|
std::cout << "--------------------------------" << std::endl << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestUniFi()
|
||||||
|
{
|
||||||
|
std::shared_ptr<PresenceDetection::WiFi::Driver> 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<PresenceDetection::Util::StaticDevice> staticDevices;
|
||||||
|
|
||||||
|
pWiFiDriver = std::make_shared<PresenceDetection::WiFi::Driver>(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[]*/)
|
int main(int /*argc*/, char** /*argv[]*/)
|
||||||
{
|
{
|
||||||
//TestINIReader();
|
//TestINIReader();
|
||||||
//TestLogic();
|
//TestLogic();
|
||||||
TestPing();
|
//TestPing();
|
||||||
|
TestUniFi();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+71
-11
@@ -2,6 +2,7 @@
|
|||||||
#include <json.hpp>
|
#include <json.hpp>
|
||||||
#include <Logging.h>
|
#include <Logging.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
@@ -9,6 +10,7 @@ namespace PresenceDetection {
|
|||||||
namespace WiFi {
|
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) :
|
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_loggedIn(false),
|
||||||
m_hostname(hostname),
|
m_hostname(hostname),
|
||||||
m_port(port),
|
m_port(port),
|
||||||
@@ -57,7 +59,7 @@ void Driver::Wait()
|
|||||||
bool Driver::Login()
|
bool Driver::Login()
|
||||||
{
|
{
|
||||||
std::stringstream url;
|
std::stringstream url;
|
||||||
url << "https://" << m_hostname << ":" << m_port << "/api/login";
|
url << "https://" << m_hostname << ":" << m_port << "/api/auth/login";
|
||||||
|
|
||||||
nlohmann::json json;
|
nlohmann::json json;
|
||||||
json["password"] = m_password;
|
json["password"] = m_password;
|
||||||
@@ -78,7 +80,7 @@ bool Driver::Login()
|
|||||||
|
|
||||||
nlohmann::json outputJSON = nlohmann::json::parse(output);
|
nlohmann::json outputJSON = nlohmann::json::parse(output);
|
||||||
|
|
||||||
if (outputJSON["meta"]["rc"] != "ok")
|
if (outputJSON["code"] == "AUTHENTICATION_FAILED_INVALID_CREDENTIALS")
|
||||||
{
|
{
|
||||||
std::stringstream error;
|
std::stringstream error;
|
||||||
error << "Login Failed - " << output.str();
|
error << "Login Failed - " << output.str();
|
||||||
@@ -102,12 +104,14 @@ bool Driver::Login()
|
|||||||
|
|
||||||
void Driver::Logout()
|
void Driver::Logout()
|
||||||
{
|
{
|
||||||
std::stringstream url;
|
// std::stringstream url;
|
||||||
url << "https://" << m_hostname << ":" << m_port << "/logout";
|
// url << "https://" << m_hostname << ":" << m_port << "/api/auth/logout";
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(m_mutex);
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||||||
m_loggedIn = false;
|
m_loggedIn = false;
|
||||||
|
|
||||||
|
ClearCookiesFile();
|
||||||
|
/*
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Http::HttpRequest request(url.str());
|
Http::HttpRequest request(url.str());
|
||||||
@@ -121,6 +125,7 @@ void Driver::Logout()
|
|||||||
ss << "WiFi::Driver::Logout() - Error: " << e.what() << std::endl;
|
ss << "WiFi::Driver::Logout() - Error: " << e.what() << std::endl;
|
||||||
Logging::Log(Logging::Severity::Error, ss.str());
|
Logging::Log(Logging::Severity::Error, ss.str());
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void Driver::ClearDevices()
|
void Driver::ClearDevices()
|
||||||
@@ -195,10 +200,10 @@ void Driver::UpdatePresentDevices()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
std::stringstream url;
|
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::time_t timeStamp = std::time(nullptr);
|
||||||
std::vector<std::string> presentDevices;
|
std::vector<PresentDevice> presentDevices;
|
||||||
std::vector<std::string> addedDevices;
|
std::vector<std::string> addedDevices;
|
||||||
std::vector<std::string> removedDevices;
|
std::vector<std::string> removedDevices;
|
||||||
|
|
||||||
@@ -210,6 +215,7 @@ void Driver::UpdatePresentDevices()
|
|||||||
output << m_httpClient.Open(request);
|
output << m_httpClient.Open(request);
|
||||||
|
|
||||||
nlohmann::json json = nlohmann::json::parse(output);
|
nlohmann::json json = nlohmann::json::parse(output);
|
||||||
|
//Logging::Log(Logging::Severity::Debug, output.str());
|
||||||
|
|
||||||
if (json["meta"]["rc"] != "ok")
|
if (json["meta"]["rc"] != "ok")
|
||||||
{
|
{
|
||||||
@@ -235,7 +241,7 @@ void Driver::UpdatePresentDevices()
|
|||||||
ss << "Device Added: " << device.dump() << std::endl;
|
ss << "Device Added: " << device.dump() << std::endl;
|
||||||
Logging::Log(Logging::Severity::Info, ss.str());
|
Logging::Log(Logging::Severity::Info, ss.str());
|
||||||
}
|
}
|
||||||
presentDevices.push_back(macAddress);
|
presentDevices.push_back(PresentDevice(macAddress, 0));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -256,7 +262,7 @@ void Driver::UpdatePresentDevices()
|
|||||||
{
|
{
|
||||||
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
|
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
|
||||||
addedDevices.push_back(macAddress);
|
addedDevices.push_back(macAddress);
|
||||||
presentDevices.push_back(macAddress);
|
presentDevices.push_back(PresentDevice(macAddress, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -274,15 +280,22 @@ void Driver::UpdatePresentDevices()
|
|||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
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());
|
Logging::Log(Logging::Severity::Error, ss.str());
|
||||||
Logout();
|
Logout();
|
||||||
return;
|
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())
|
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)
|
for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
|
||||||
SendStateChange(true, *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 WiFi
|
||||||
} // namespace PresenceDetection
|
} // namespace PresenceDetection
|
||||||
|
|||||||
+19
-1
@@ -31,6 +31,24 @@ private:
|
|||||||
void UpdatePresentDevices();
|
void UpdatePresentDevices();
|
||||||
void SendStateChange(bool present, const std::string& macAddress);
|
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:
|
private:
|
||||||
Timer::Timer m_deviceTimer;
|
Timer::Timer m_deviceTimer;
|
||||||
Timer::Timer m_inventoryTimer;
|
Timer::Timer m_inventoryTimer;
|
||||||
@@ -52,7 +70,7 @@ private:
|
|||||||
std::string m_target;
|
std::string m_target;
|
||||||
std::vector<Util::StaticDevice> m_staticDevices;
|
std::vector<Util::StaticDevice> m_staticDevices;
|
||||||
|
|
||||||
std::vector<std::string> m_presentDevices;
|
std::vector<PresentDevice> m_presentDevices;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace WiFi
|
} // namespace WiFi
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef WIFI_FUNCTIONS_H
|
#ifndef WIFI_FUNCTIONS_H
|
||||||
#define WIFI_FUNCTIONS_H
|
#define WIFI_FUNCTIONS_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user