|
|
@@ -1,22 +1,23 @@
|
|
|
+#include <sstream>
|
|
|
#include <syslog.h>
|
|
|
-#include <boost/foreach.hpp>
|
|
|
-#include <boost/property_tree/ptree.hpp>
|
|
|
-#include <boost/property_tree/json_parser.hpp>
|
|
|
+#include "Util/JSON.h"
|
|
|
#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) :
|
|
|
+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) :
|
|
|
m_loggedIn(false),
|
|
|
m_hostname(hostname),
|
|
|
m_username(username),
|
|
|
m_password(password),
|
|
|
m_cookieFile(cookieFile),
|
|
|
- m_target(target)
|
|
|
+ m_inventoryURL(inventoryURL),
|
|
|
+ m_target(target),
|
|
|
+ m_offlineTimeout(120)
|
|
|
{
|
|
|
- m_offlineTimeout = 120;
|
|
|
+ UpdateDevicesFromInventory();
|
|
|
Start();
|
|
|
}
|
|
|
|
|
|
@@ -27,17 +28,18 @@ Device::~Device()
|
|
|
|
|
|
void Device::Start()
|
|
|
{
|
|
|
- m_timer.StartContinuous(5000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
|
|
|
+ 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)));
|
|
|
}
|
|
|
|
|
|
void Device::Stop()
|
|
|
{
|
|
|
- m_timer.Stop();
|
|
|
+ m_deviceTimer.Stop();
|
|
|
}
|
|
|
|
|
|
void Device::Wait()
|
|
|
{
|
|
|
- m_timer.Wait();
|
|
|
+ m_deviceTimer.Wait();
|
|
|
}
|
|
|
|
|
|
bool Device::Login()
|
|
|
@@ -45,23 +47,18 @@ 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);
|
|
|
+ Util::JSON json;
|
|
|
+ json["password"] = m_password;
|
|
|
+ json["username"] = m_username;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
std::stringstream output;
|
|
|
- output << m_httpClient.GetUrlPostContents(url.str(), m_cookieFile, json.str(), "application/json");
|
|
|
+ output << m_httpClient.GetUrlPostContents(url.str(), m_cookieFile, json.dump(), "application/json");
|
|
|
|
|
|
- boost::property_tree::ptree pt;
|
|
|
- boost::property_tree::read_json(output, pt);
|
|
|
+ Util::JSON outputJSON = Util::JSON::parse(output);
|
|
|
|
|
|
- std::string result = pt.get_child("meta").get<std::string>("rc");
|
|
|
-
|
|
|
- if (result != "ok")
|
|
|
+ if (outputJSON["meta"]["rc"] != "ok")
|
|
|
{
|
|
|
std::stringstream error;
|
|
|
error << "Login Failed - " << output.str();
|
|
|
@@ -103,6 +100,30 @@ void Device::Logout()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void Device::UpdateDevicesFromInventory()
|
|
|
+{
|
|
|
+ try
|
|
|
+ {
|
|
|
+ std::string devices = m_httpClient.GetUrlContents(m_inventoryURL);
|
|
|
+ Util::JSON json = Util::JSON::parse(devices);
|
|
|
+
|
|
|
+ m_devices.clear();
|
|
|
+ for (auto& element : json)
|
|
|
+ if (element["macaddress"] != "")
|
|
|
+ {
|
|
|
+ std::string macAddress = element["macaddress"];
|
|
|
+ std::transform(macAddress.begin(), macAddress.end(), macAddress.begin(), ::tolower);
|
|
|
+ m_devices.push_back(macAddress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (const std::exception& e)
|
|
|
+ {
|
|
|
+ std::stringstream ss;
|
|
|
+ ss << "UniFi::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
|
|
|
+ syslog(LOG_ERR, "%s", ss.str().c_str());
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
void Device::UpdatePresentDevices()
|
|
|
{
|
|
|
bool loggedIn;
|
|
|
@@ -128,28 +149,28 @@ void Device::UpdatePresentDevices()
|
|
|
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");
|
|
|
+ Util::JSON json = Util::JSON::parse(output);
|
|
|
|
|
|
- if (result != "ok")
|
|
|
+ if (json["meta"]["rc"] != "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"))
|
|
|
+ for (auto& device : json["data"])
|
|
|
{
|
|
|
- std::string macAddress = v.second.get<std::string>("mac");
|
|
|
- int lastSeen = v.second.get<int>("last_seen");
|
|
|
+ std::string macAddress = device["mac"];
|
|
|
+ int lastSeen = device["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);
|
|
|
+ if (std::find(m_devices.begin(), m_devices.end(), macAddress) != m_devices.end())
|
|
|
+ {
|
|
|
+ if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
|
|
|
+ addedDevices.push_back(macAddress);
|
|
|
+ presentDevices.push_back(macAddress);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|