1
0

2 Commits 9f0ff10abc ... 434eb25dda

Autor SHA1 Nachricht Datum
  JDierkse 434eb25dda Update UniFi Login vor 9 Monaten
  JDierkse dc17bbe535 Update Makefiles vor 9 Monaten
6 geänderte Dateien mit 126 neuen und 14 gelöschten Zeilen
  1. 1 0
      .gitignore
  2. 33 1
      Application/Test.cc
  3. 1 1
      Makefiles
  4. 71 11
      WiFi/Driver.cpp
  5. 19 1
      WiFi/Driver.h
  6. 1 0
      WiFi/Functions.h

+ 1 - 0
.gitignore

@@ -12,4 +12,5 @@ PresenceDetection*
 !PresenceDetection.ini
 test*
 !test.cc
+Test*
 fixPermissions.sh

+ 33 - 1
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 <INIReader.h>
 #include <algorithm>
 #include <iostream>
+#include <signal.h>
 #include <string>
 
 
@@ -86,11 +89,40 @@ void TestPing()
 	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[]*/)
 {
 	//TestINIReader();
 	//TestLogic();
-	TestPing();
+	//TestPing();
+	TestUniFi();
 
 	return 0;
 }

+ 1 - 1
Makefiles

@@ -1 +1 @@
-Subproject commit 4bcd6867800526e4483cd0ef3f475add2daab5e6
+Subproject commit 25568790824bea7883279ae5a92b3c655d535ac9

+ 71 - 11
WiFi/Driver.cpp

@@ -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

+ 19 - 1
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<Util::StaticDevice> m_staticDevices;
 
-	std::vector<std::string> m_presentDevices;
+	std::vector<PresentDevice> m_presentDevices;
 };
 
 } // namespace WiFi

+ 1 - 0
WiFi/Functions.h

@@ -1,6 +1,7 @@
 #ifndef WIFI_FUNCTIONS_H
 #define WIFI_FUNCTIONS_H
 
+#include <cstdint>
 #include <string>