Initial Commit
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
#include <syslog.h>
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
#include "Device.h"
|
||||
|
||||
|
||||
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& target) :
|
||||
m_loggedIn(false),
|
||||
m_hostname(hostname),
|
||||
m_username(username),
|
||||
m_password(password),
|
||||
m_cookieFile(cookieFile),
|
||||
m_target(target)
|
||||
{
|
||||
m_offlineTimeout = 120;
|
||||
Start();
|
||||
}
|
||||
|
||||
Device::~Device()
|
||||
{
|
||||
Logout();
|
||||
}
|
||||
|
||||
void Device::Start()
|
||||
{
|
||||
m_timer.Start(5000, std::bind(&Device::UpdatePresentDevices, this));
|
||||
}
|
||||
|
||||
void Device::Stop()
|
||||
{
|
||||
m_timer.Stop();
|
||||
}
|
||||
|
||||
void Device::Wait()
|
||||
{
|
||||
m_timer.Wait();
|
||||
}
|
||||
|
||||
bool Device::Login()
|
||||
{
|
||||
std::stringstream url;
|
||||
url << "https://" << m_hostname << "/api/login";
|
||||
|
||||
std::ostringstream json;
|
||||
boost::property_tree::ptree root;
|
||||
root.put("password", m_password);
|
||||
root.put("username", m_username);
|
||||
boost::property_tree::write_json(json, root);
|
||||
|
||||
try
|
||||
{
|
||||
std::stringstream output;
|
||||
output << m_httpClient.GetUrlPostContents(url.str(), m_cookieFile, json.str(), "application/json");
|
||||
|
||||
boost::property_tree::ptree pt;
|
||||
boost::property_tree::read_json(output, pt);
|
||||
|
||||
std::string result = pt.get_child("meta").get<std::string>("rc");
|
||||
|
||||
if (result != "ok")
|
||||
{
|
||||
std::stringstream error;
|
||||
error << "Login Failed - " << output.str();
|
||||
throw std::runtime_error(error.str());
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "UniFiDevice::Login() - Error: " << e.what() << std::endl;
|
||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_loggedIn = false;
|
||||
return m_loggedIn;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_loggedIn = true;
|
||||
return m_loggedIn;
|
||||
}
|
||||
|
||||
void Device::Logout()
|
||||
{
|
||||
std::stringstream url;
|
||||
url << "https://" << m_hostname << "/logout";
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_loggedIn = false;
|
||||
|
||||
try
|
||||
{
|
||||
m_httpClient.GetUrlSilent(url.str(), m_cookieFile);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "UniFiDevice::Logout() - Error: " << e.what() << std::endl;
|
||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void Device::UpdatePresentDevices()
|
||||
{
|
||||
bool loggedIn;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
loggedIn = m_loggedIn;
|
||||
}
|
||||
|
||||
if (!loggedIn)
|
||||
if (!Login())
|
||||
return;
|
||||
|
||||
std::stringstream url;
|
||||
url << "https://" << m_hostname << "/api/s/default/stat/sta";
|
||||
|
||||
std::time_t timeStamp = std::time(nullptr);
|
||||
std::vector<std::string> presentDevices;
|
||||
std::vector<std::string> addedDevices;
|
||||
std::vector<std::string> removedDevices;
|
||||
|
||||
try
|
||||
{
|
||||
std::stringstream output;
|
||||
output << m_httpClient.GetUrlContents(url.str(), m_cookieFile);
|
||||
|
||||
boost::property_tree::ptree pt;
|
||||
boost::property_tree::read_json(output, pt);
|
||||
|
||||
std::string result = pt.get_child("meta").get<std::string>("rc");
|
||||
|
||||
if (result != "ok")
|
||||
{
|
||||
std::stringstream error;
|
||||
error << "Query Failed";
|
||||
throw std::runtime_error(error.str());
|
||||
}
|
||||
|
||||
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("data"))
|
||||
{
|
||||
std::string macAddress = v.second.get<std::string>("mac");
|
||||
int lastSeen = v.second.get<int>("last_seen");
|
||||
|
||||
if ((timeStamp - lastSeen) < m_offlineTimeout)
|
||||
{
|
||||
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)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "UniFiDevice::IsDevicePresent() - Error: " << e.what() << std::endl;
|
||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||
Logout();
|
||||
return;
|
||||
}
|
||||
|
||||
for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
|
||||
if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
|
||||
removedDevices.push_back(*it);
|
||||
|
||||
for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "UniFi: + " << *it;
|
||||
syslog(LOG_INFO, "%s", ss.str().c_str());
|
||||
|
||||
std::stringstream url;
|
||||
url << m_target << "/UniFi/+/" << *it;
|
||||
|
||||
try
|
||||
{
|
||||
m_httpClient.GetUrlSilent(url.str());
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "UniFiDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
|
||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
for (std::vector<std::string>::iterator it = removedDevices.begin(); it != removedDevices.end(); ++it)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "UniFi: - " << *it;
|
||||
syslog(LOG_INFO, "%s", ss.str().c_str());
|
||||
|
||||
std::stringstream url;
|
||||
url << m_target << "/UniFi/-/" << *it;
|
||||
|
||||
try
|
||||
{
|
||||
m_httpClient.GetUrlSilent(url.str());
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "UniFiDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
|
||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
|
||||
}
|
||||
|
||||
} // namespace UniFi
|
||||
} // namespace PresenceDetection
|
||||
@@ -0,0 +1,50 @@
|
||||
#ifndef UNIFI_DEVICE_H
|
||||
#define UNIFI_DEVICE_H
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "Util/Timer.h"
|
||||
#include "Util/HttpClient.h"
|
||||
|
||||
|
||||
namespace PresenceDetection {
|
||||
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& target);
|
||||
~Device();
|
||||
|
||||
void Start();
|
||||
void Stop();
|
||||
void Wait();
|
||||
|
||||
private:
|
||||
bool Login();
|
||||
void Logout();
|
||||
|
||||
void UpdatePresentDevices();
|
||||
|
||||
private:
|
||||
Util::Timer m_timer;
|
||||
Util::HttpClient m_httpClient;
|
||||
|
||||
std::mutex m_mutex;
|
||||
bool m_loggedIn;
|
||||
std::string m_hostname;
|
||||
std::string m_username;
|
||||
std::string m_password;
|
||||
std::string m_cookieFile;
|
||||
|
||||
std::string m_target;
|
||||
int m_offlineTimeout;
|
||||
|
||||
std::vector<std::string> m_presentDevices;
|
||||
};
|
||||
|
||||
} // namespace UniFi
|
||||
} // namespace PresenceDetection
|
||||
|
||||
#endif // UNIFI_DEVICE_H
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../Makefile
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../Makefile.conf
|
||||
Reference in New Issue
Block a user