Add Static Devices, Timeout and UniFi Port Directive

This commit is contained in:
2020-03-05 16:56:12 +01:00
parent 2fbec833a2
commit 7b8fcc9722
8 changed files with 556 additions and 35 deletions
+118 -13
View File
@@ -1,3 +1,4 @@
#include <algorithm>
#include <sstream>
#include "Util/JSON.h"
#include "Util/Logger.h"
@@ -7,17 +8,21 @@
namespace PresenceDetection {
namespace UniFi {
Device::Device(const std::string& hostname, const std::string& username, const std::string& password, const std::string& cookieFile, const std::string& inventoryURL, const std::string& target) :
Device::Device(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::Device>& devices) :
m_loggedIn(false),
m_hostname(hostname),
m_port(port),
m_username(username),
m_password(password),
m_cookieFile(cookieFile),
m_timeout(timeout),
m_inventoryURL(inventoryURL),
m_target(target),
m_offlineTimeout(120)
m_staticDevices(devices)
{
UpdateDevicesFromInventory();
if (!m_inventoryURL.empty())
UpdateDevicesFromInventory();
ClearDevices();
Start();
}
@@ -29,23 +34,28 @@ Device::~Device()
void Device::Start()
{
m_deviceTimer.StartContinuous(5000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
if (!m_inventoryURL.empty())
m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
}
void Device::Stop()
{
m_deviceTimer.Stop();
if (!m_inventoryURL.empty())
m_inventoryTimer.Stop();
}
void Device::Wait()
{
m_deviceTimer.Wait();
if (!m_inventoryURL.empty())
m_inventoryTimer.Stop();
}
bool Device::Login()
{
std::stringstream url;
url << "https://" << m_hostname << "/api/login";
url << "https://" << m_hostname << ":" << m_port << "/api/login";
Util::JSON json;
json["password"] = m_password;
@@ -83,7 +93,7 @@ bool Device::Login()
void Device::Logout()
{
std::stringstream url;
url << "https://" << m_hostname << "/logout";
url << "https://" << m_hostname << ":" << m_port << "/logout";
std::lock_guard<std::mutex> lock(m_mutex);
m_loggedIn = false;
@@ -100,6 +110,40 @@ void Device::Logout()
}
}
void Device::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 << "UniFi::Device::ClearDevices() - Error: " << e.what() << std::endl;
Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
}
}
for (std::vector<Util::Device>::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 << "UniFi::Device::ClearDevices() - Error: " << e.what() << std::endl;
Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
}
}
}
}
void Device::UpdateDevicesFromInventory()
{
try
@@ -137,7 +181,7 @@ void Device::UpdatePresentDevices()
return;
std::stringstream url;
url << "https://" << m_hostname << "/api/s/default/stat/sta";
url << "https://" << m_hostname << ":" << m_port << "/api/s/default/stat/sta";
std::time_t timeStamp = std::time(nullptr);
std::vector<std::string> presentDevices;
@@ -161,17 +205,31 @@ void Device::UpdatePresentDevices()
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 ((timeStamp - lastSeen) < m_offlineTimeout)
if (std::find(m_devices.begin(), m_devices.end(), macAddress) != m_devices.end())
{
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);
presentDevices.push_back(macAddress);
}
}
for (std::vector<Util::Device>::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(macAddress);
}
}
}
}
}
catch (const std::exception& e)
@@ -208,13 +266,60 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
ss << "UniFi: " << sign << " " << macAddress;
Util::Logger::Log(Util::Logger::Severity::Info, ss.str());
if (!m_target.empty())
{
std::stringstream url;
url << m_target << "/UniFi/" << sign << "/" << macAddress;
try
{
m_httpClient.GetUrlSilent(url.str());
try
{
m_httpClient.GetUrlSilent(url.str());
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl;
Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
}
}
catch (const std::exception& e)
for (std::vector<Util::Device>::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
{
m_httpClient.GetUrlSilent(url);
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl;
Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
}
}
}
}
}
+6 -2
View File
@@ -4,6 +4,7 @@
#include <mutex>
#include <string>
#include <vector>
#include "Util/Device.h"
#include "Util/Timer.h"
#include "Util/HttpClient.h"
@@ -14,7 +15,7 @@ namespace UniFi {
class Device
{
public:
Device(const std::string& hostname, const std::string& username, const std::string& password, const std::string& cookieFile, const std::string& inventoryURL, const std::string& target);
Device(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::Device>& devices);
~Device();
void Start();
@@ -25,6 +26,7 @@ private:
bool Login();
void Logout();
void ClearDevices();
void UpdateDevicesFromInventory();
void UpdatePresentDevices();
void SendStateChange(bool present, const std::string& macAddress);
@@ -37,15 +39,17 @@ private:
std::mutex m_mutex;
bool m_loggedIn;
std::string m_hostname;
int m_port;
std::string m_username;
std::string m_password;
std::string m_cookieFile;
std::vector<std::string> m_devices;
int m_timeout;
std::string m_inventoryURL;
std::string m_target;
int m_offlineTimeout;
std::vector<Util::Device> m_staticDevices;
std::vector<std::string> m_presentDevices;
};