Add Static Devices

This commit is contained in:
2020-05-01 11:39:46 +02:00
parent 157b1ceb51
commit 21dbdd41d9
12 changed files with 400 additions and 327 deletions
+19 -14
View File
@@ -1,6 +1,6 @@
#include "Bluetooth/Device.h" #include "Bluetooth/Device.h"
#include "UniFi/Device.h" #include "UniFi/Device.h"
#include "Util/Device.h" #include "Util/StaticDevice.h"
#include "Util/INIh.h" #include "Util/INIh.h"
#include <clipp.h> #include <clipp.h>
#include <Logging.h> #include <Logging.h>
@@ -41,7 +41,7 @@ int main(int argc, char** argv)
if (daemon) if (daemon)
{ {
Logging::Log(Logging::Severity::Info, "Starting Daemon"); Logging::Log(Logging::Severity::Info, "Starting PresenceDetection Daemon");
pid_t pid, sid; pid_t pid, sid;
pid = fork(); pid = fork();
@@ -62,6 +62,11 @@ int main(int argc, char** argv)
close(STDOUT_FILENO); close(STDOUT_FILENO);
close(STDERR_FILENO); close(STDERR_FILENO);
} }
else
{
Logging::Log(Logging::Severity::Info, "Starting PresenceDetection");
}
std::string target = iniReader.Get("PresenceDetection", "Target", ""); std::string target = iniReader.Get("PresenceDetection", "Target", "");
@@ -69,7 +74,7 @@ int main(int argc, char** argv)
bool bluetooth = iniReader.GetBoolean("PresenceDetection", "Bluetooth", false); bool bluetooth = iniReader.GetBoolean("PresenceDetection", "Bluetooth", false);
std::vector<PresenceDetection::Util::Device> devices; std::vector<PresenceDetection::Util::StaticDevice> staticDevices;
auto sections = iniReader.Sections(); auto sections = iniReader.Sections();
for (auto section : sections) for (auto section : sections)
@@ -88,33 +93,33 @@ int main(int argc, char** argv)
throw std::runtime_error(ss.str()); throw std::runtime_error(ss.str());
} }
auto device = PresenceDetection::Util::Device(WifiMac, BluetoothMac); auto staticDevice = PresenceDetection::Util::StaticDevice(WifiMac, BluetoothMac);
std::string OnlineURL = iniReader.Get(section, "OnlineURL", ""); std::string OnlineURL = iniReader.Get(section, "OnlineURL", "");
if (!OnlineURL.empty()) if (!OnlineURL.empty())
device.SetOnlineURL(OnlineURL); staticDevice.SetOnlineURL(OnlineURL);
std::string WifiOnlineURL = iniReader.Get(section, "WifiOnlineURL", ""); std::string WifiOnlineURL = iniReader.Get(section, "WifiOnlineURL", "");
if (!WifiOnlineURL.empty()) if (!WifiOnlineURL.empty())
device.SetWifiOnlineURL(WifiOnlineURL); staticDevice.SetWifiOnlineURL(WifiOnlineURL);
std::string BluetoothOnlineURL = iniReader.Get(section, "BluetoothOnlineURL", ""); std::string BluetoothOnlineURL = iniReader.Get(section, "BluetoothOnlineURL", "");
if (!BluetoothOnlineURL.empty()) if (!BluetoothOnlineURL.empty())
device.SetBluetoothOnlineURL(BluetoothOnlineURL); staticDevice.SetBluetoothOnlineURL(BluetoothOnlineURL);
std::string OfflineURL = iniReader.Get(section, "OfflineURL", ""); std::string OfflineURL = iniReader.Get(section, "OfflineURL", "");
if (!OfflineURL.empty()) if (!OfflineURL.empty())
device.SetOfflineURL(OfflineURL); staticDevice.SetOfflineURL(OfflineURL);
std::string WifiOfflineURL = iniReader.Get(section, "WifiOfflineURL", ""); std::string WifiOfflineURL = iniReader.Get(section, "WifiOfflineURL", "");
if (!WifiOfflineURL.empty()) if (!WifiOfflineURL.empty())
device.SetWifiOfflineURL(WifiOfflineURL); staticDevice.SetWifiOfflineURL(WifiOfflineURL);
std::string BluetoothOfflineURL = iniReader.Get(section, "BluetoothOfflineURL", ""); std::string BluetoothOfflineURL = iniReader.Get(section, "BluetoothOfflineURL", "");
if (!BluetoothOfflineURL.empty()) if (!BluetoothOfflineURL.empty())
device.SetBluetoothOfflineURL(BluetoothOfflineURL); staticDevice.SetBluetoothOfflineURL(BluetoothOfflineURL);
devices.push_back(device); staticDevices.push_back(staticDevice);
} }
} }
@@ -143,7 +148,7 @@ int main(int argc, char** argv)
std::string inventoryURL = iniReader.Get("UniFi", "Inventory", ""); std::string inventoryURL = iniReader.Get("UniFi", "Inventory", "");
pUniFiDevice = std::make_shared<PresenceDetection::UniFi::Device>(hostname, port, username, password, cookiefile, timeout, inventoryURL, target, devices); pUniFiDevice = std::make_shared<PresenceDetection::UniFi::Device>(hostname, port, username, password, cookiefile, timeout, inventoryURL, target, staticDevices);
} }
std::shared_ptr<PresenceDetection::Bluetooth::Device> pBluetoothDevice; std::shared_ptr<PresenceDetection::Bluetooth::Device> pBluetoothDevice;
@@ -153,7 +158,7 @@ int main(int argc, char** argv)
std::string inventoryURL = iniReader.Get("Bluetooth", "Inventory", ""); std::string inventoryURL = iniReader.Get("Bluetooth", "Inventory", "");
pBluetoothDevice = std::make_shared<PresenceDetection::Bluetooth::Device>(timeout, inventoryURL, target, devices); pBluetoothDevice = std::make_shared<PresenceDetection::Bluetooth::Device>(timeout, inventoryURL, target, staticDevices);
} }
sigset_t wset; sigset_t wset;
@@ -164,7 +169,7 @@ int main(int argc, char** argv)
int sig; int sig;
sigwait(&wset,&sig); sigwait(&wset,&sig);
Logging::Log(Logging::Severity::Info, "Stopping..."); Logging::Log(Logging::Severity::Info, "Stopping PresenceDetection...");
if (pUniFiDevice) if (pUniFiDevice)
pUniFiDevice->Stop(); pUniFiDevice->Stop();
-79
View File
@@ -14,85 +14,6 @@ void print()
std::cout << "Print" << std::endl; std::cout << "Print" << std::endl;
} }
void TestTimerAbort()
{
Timer::Timer timer;
std::function<void()> f_print = print;
std::cout << "---" << std::endl;
auto start = std::chrono::steady_clock::now();
std::clock_t begin = std::clock();
timer.StartContinuous(1000, f_print);
timer.Stop();
timer.Wait();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
std::cout << "Elapsed: " << elapsed.count() << std::endl;
std::cout << "---" << std::endl << std::endl;
std::cout << "---" << std::endl;
start = std::chrono::steady_clock::now();
timer.StartContinuous(3000, f_print);
timer.Abort();
timer.Wait();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
std::cout << "Elapsed: " << elapsed.count() << std::endl;
std::cout << "---" << std::endl << std::endl;
std::cout << "---" << std::endl;
start = std::chrono::steady_clock::now();
timer.StartContinuous(3000, f_print);
sleep(1);
timer.Abort();
timer.Wait();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
std::cout << "Elapsed: " << elapsed.count() << std::endl;
std::cout << "---" << std::endl << std::endl;
}
typedef std::unique_ptr<Timer::Timer> Pointer;
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
void OnTimerEnd(const std::string& type, int id)
{
std::cout << "OnTimerEnd() " << type << " " << id <<std::endl;
}
void StartNewTimer(std::deque<Pointer>& timers)
{
int delay(2000);
std::string identifier("Blabla");
int id(10);
// Protect!
auto timer = make_unique<Timer::Timer>();
timer->StartSingle(delay, static_cast<std::function<void(const std::string&, int)>>(std::bind(&OnTimerEnd, std::placeholders::_1, std::placeholders::_2)), identifier, id);
timers.push_back(std::move(timer));
}
void Cleanup(std::deque<Pointer>& timers)
{
timers.erase(std::remove_if(timers.begin(), timers.end(), [](Pointer& t){return !t->IsRunning();}), timers.end());
}
void TestTimerDeque()
{
std::deque<Pointer> timers;
StartNewTimer(timers);
std::cout << "Count: " << timers.size() << std::endl;
StartNewTimer(timers);
std::cout << "Count: " << timers.size() << std::endl;
sleep(5);
std::cout << "Count: " << timers.size() << std::endl;
Cleanup(timers);
std::cout << "Count: " << timers.size() << std::endl;
}
void TestINIReader() void TestINIReader()
{ {
PresenceDetection::Util::INIReader reader("PresenceDetection.ini"); PresenceDetection::Util::INIReader reader("PresenceDetection.ini");
+58 -42
View File
@@ -9,11 +9,12 @@
namespace PresenceDetection { namespace PresenceDetection {
namespace Bluetooth { namespace Bluetooth {
Device::Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::Device>& devices) : Device::Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::StaticDevice>& staticDevices) :
m_timeout(timeout), m_timeout(timeout),
m_checkInterval(3),
m_inventoryURL(inventoryURL), m_inventoryURL(inventoryURL),
m_target(target), m_target(target),
m_staticDevices(devices) m_staticDevices(staticDevices)
{ {
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
UpdateDevicesFromInventory(); UpdateDevicesFromInventory();
@@ -27,36 +28,33 @@ Device::~Device()
void Device::Start() void Device::Start()
{ {
int timeout = m_timeout * 1000; int checkInterval = m_checkInterval * 1000;
m_onlineDeviceTimer.StartContinuous(timeout, static_cast<std::function<void()>>(std::bind(&Device::UpdateOnlineDevices, this))); m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
m_offlineDeviceTimer.StartContinuous(3000, static_cast<std::function<void()>>(std::bind(&Device::UpdateOfflineDevices, this)));
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
m_inventoryTimer.StartContinuous(600000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this))); m_inventoryTimer.StartContinuous(600000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
} }
void Device::Stop() void Device::Stop()
{ {
m_onlineDeviceTimer.Stop(); m_deviceTimer.Stop();
m_offlineDeviceTimer.Stop();
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
m_inventoryTimer.Stop(); m_inventoryTimer.Stop();
} }
void Device::Wait() void Device::Wait()
{ {
m_onlineDeviceTimer.Wait(); m_deviceTimer.Wait();
m_offlineDeviceTimer.Wait();
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
m_inventoryTimer.Wait(); m_inventoryTimer.Wait();
} }
void Device::ClearDevices() void Device::ClearDevices()
{ {
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it) for (std::vector<Util::DynamicDevice>::iterator it = m_dynamicDevices.begin(); it != m_dynamicDevices.end(); ++it)
{ {
try try
{ {
SendStateChange(false, *it); SendStateChange(false, it->MacAddress());
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
@@ -66,7 +64,7 @@ void Device::ClearDevices()
} }
} }
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{ {
if (it->HasBluetoothMac()) if (it->HasBluetoothMac())
{ {
@@ -91,13 +89,13 @@ void Device::UpdateDevicesFromInventory()
std::string devices = m_httpClient.GetUrlContents(m_inventoryURL); std::string devices = m_httpClient.GetUrlContents(m_inventoryURL);
Util::JSON json = Util::JSON::parse(devices); Util::JSON json = Util::JSON::parse(devices);
m_devices.clear(); m_dynamicDevices.clear();
for (auto& element : json) for (auto& element : json)
if (element["bluetooth"] != "") if (element["bluetooth"] != "")
{ {
std::string bluetooth = element["bluetooth"]; std::string bluetooth = element["bluetooth"];
std::transform(bluetooth.begin(), bluetooth.end(), bluetooth.begin(), ::tolower); std::transform(bluetooth.begin(), bluetooth.end(), bluetooth.begin(), ::tolower);
m_devices.push_back(bluetooth); m_dynamicDevices.push_back(Util::DynamicDevice(bluetooth));
} }
} }
catch (const std::exception& e) catch (const std::exception& e)
@@ -108,40 +106,44 @@ void Device::UpdateDevicesFromInventory()
} }
} }
void Device::UpdateOnlineDevices() void Device::UpdatePresentDevices()
{
UpdatePresentDevices(true);
}
void Device::UpdateOfflineDevices()
{
UpdatePresentDevices(false);
}
void Device::UpdatePresentDevices(bool updateOnlineDevices)
{ {
std::vector<std::string> presentDevices; std::vector<std::string> presentDevices;
std::vector<std::string> ignoredDevices; std::vector<std::string> ignoredDevices;
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it) for (std::vector<Util::DynamicDevice>::iterator it = m_dynamicDevices.begin(); it != m_dynamicDevices.end(); ++it)
{ {
try try
{ {
bool pollDevice = false; bool pollDevice = false;
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end()) if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->MacAddress()) == m_presentDevices.end())
pollDevice = true;
else
pollDevice = updateOnlineDevices;
if (pollDevice && Functions::Ping(*it))
{ {
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end()) pollDevice = true;
SendStateChange(true, *it); }
presentDevices.push_back(*it); else
{
it->IncrementProbeCounter();
if (it->ProbeCounter() * m_checkInterval >= m_timeout)
{
pollDevice = true;
it->ResetProbeCounter();
}
} }
if (!pollDevice) if (pollDevice && Functions::Ping(it->MacAddress()))
ignoredDevices.push_back(*it); {
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->MacAddress()) == m_presentDevices.end())
SendStateChange(true, it->MacAddress());
presentDevices.push_back(it->MacAddress());
}
else if (pollDevice)
{
it->ResetProbeCounter();
}
else
{
ignoredDevices.push_back(it->MacAddress());
}
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
@@ -151,7 +153,7 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices)
} }
} }
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{ {
if (it->HasBluetoothMac()) if (it->HasBluetoothMac())
{ {
@@ -159,9 +161,18 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices)
{ {
bool pollDevice = false; bool pollDevice = false;
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end()) if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end())
{
pollDevice = true; pollDevice = true;
}
else else
pollDevice = updateOnlineDevices; {
it->IncrementProbeCounter();
if (it->ProbeCounter() * m_checkInterval >= m_timeout)
{
pollDevice = true;
it->ResetProbeCounter();
}
}
if (pollDevice && Functions::Ping(it->BluetoothMac())) if (pollDevice && Functions::Ping(it->BluetoothMac()))
{ {
@@ -169,10 +180,15 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices)
SendStateChange(true, it->BluetoothMac()); SendStateChange(true, it->BluetoothMac());
presentDevices.push_back(it->BluetoothMac()); presentDevices.push_back(it->BluetoothMac());
} }
else if (pollDevice)
if (!pollDevice) {
it->ResetProbeCounter();
}
else
{
ignoredDevices.push_back(it->BluetoothMac()); ignoredDevices.push_back(it->BluetoothMac());
} }
}
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
@@ -223,7 +239,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
} }
} }
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{ {
if (it->HasBluetoothMac() && it->BluetoothMac() == macAddress) if (it->HasBluetoothMac() && it->BluetoothMac() == macAddress)
{ {
+8 -9
View File
@@ -1,7 +1,8 @@
#ifndef BLUETOOTH_DEVICE_H #ifndef BLUETOOTH_DEVICE_H
#define BLUETOOTH_DEVICE_H #define BLUETOOTH_DEVICE_H
#include "Util/Device.h" #include "Util/DynamicDevice.h"
#include "Util/StaticDevice.h"
#include <HttpClient.h> #include <HttpClient.h>
#include <Timer.h> #include <Timer.h>
#include <string> #include <string>
@@ -14,7 +15,7 @@ namespace Bluetooth {
class Device class Device
{ {
public: public:
Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::Device>& devices); Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::StaticDevice>& staticDevices);
~Device(); ~Device();
void Start(); void Start();
@@ -24,23 +25,21 @@ public:
private: private:
void ClearDevices(); void ClearDevices();
void UpdateDevicesFromInventory(); void UpdateDevicesFromInventory();
void UpdateOnlineDevices(); void UpdatePresentDevices();
void UpdateOfflineDevices();
void UpdatePresentDevices(bool updateOnlineDevices);
void SendStateChange(bool present, const std::string& macAddress); void SendStateChange(bool present, const std::string& macAddress);
private: private:
Timer::Timer m_onlineDeviceTimer; Timer::Timer m_deviceTimer;
Timer::Timer m_offlineDeviceTimer;
Timer::Timer m_inventoryTimer; Timer::Timer m_inventoryTimer;
Http::HttpClient m_httpClient; Http::HttpClient m_httpClient;
std::vector<std::string> m_devices; std::vector<Util::DynamicDevice> m_dynamicDevices;
int m_timeout; int m_timeout;
int m_checkInterval;
std::string m_inventoryURL; std::string m_inventoryURL;
std::string m_target; std::string m_target;
std::vector<Util::Device> m_staticDevices; std::vector<Util::StaticDevice> m_staticDevices;
std::vector<std::string> m_presentDevices; std::vector<std::string> m_presentDevices;
}; };
+26 -6
View File
@@ -8,7 +8,7 @@
namespace PresenceDetection { namespace PresenceDetection {
namespace UniFi { namespace UniFi {
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) : 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::StaticDevice>& staticDevices) :
m_loggedIn(false), m_loggedIn(false),
m_hostname(hostname), m_hostname(hostname),
m_port(port), m_port(port),
@@ -16,9 +16,10 @@ Device::Device(const std::string& hostname, int port, const std::string& usernam
m_password(password), m_password(password),
m_cookieFile(cookieFile), m_cookieFile(cookieFile),
m_timeout(timeout), m_timeout(timeout),
m_checkInterval(5),
m_inventoryURL(inventoryURL), m_inventoryURL(inventoryURL),
m_target(target), m_target(target),
m_staticDevices(devices) m_staticDevices(staticDevices)
{ {
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
UpdateDevicesFromInventory(); UpdateDevicesFromInventory();
@@ -33,7 +34,8 @@ Device::~Device()
void Device::Start() void Device::Start()
{ {
m_deviceTimer.StartContinuous(5000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this))); int checkInterval = m_checkInterval * 1000;
m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this))); m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Device::UpdateDevicesFromInventory, this)));
} }
@@ -129,7 +131,7 @@ void Device::ClearDevices()
} }
} }
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{ {
if (it->HasWifiMac()) if (it->HasWifiMac())
{ {
@@ -219,9 +221,18 @@ void Device::UpdatePresentDevices()
addedDevices.push_back(macAddress); addedDevices.push_back(macAddress);
presentDevices.push_back(macAddress); presentDevices.push_back(macAddress);
} }
else
{
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) != m_presentDevices.end())
{
std::stringstream ss;
ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl;
Logging::Log(Logging::Severity::Debug, ss.str());
}
}
} }
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{ {
if ((timeStamp - lastSeen) < m_timeout) if ((timeStamp - lastSeen) < m_timeout)
{ {
@@ -232,6 +243,15 @@ void Device::UpdatePresentDevices()
presentDevices.push_back(macAddress); presentDevices.push_back(macAddress);
} }
} }
else
{
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) != m_presentDevices.end())
{
std::stringstream ss;
ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl;
Logging::Log(Logging::Severity::Debug, ss.str());
}
}
} }
} }
} }
@@ -286,7 +306,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
} }
} }
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{ {
if (it->HasWifiMac() && it->WifiMac() == macAddress) if (it->HasWifiMac() && it->WifiMac() == macAddress)
{ {
+4 -3
View File
@@ -1,7 +1,7 @@
#ifndef UNIFI_DEVICE_H #ifndef UNIFI_DEVICE_H
#define UNIFI_DEVICE_H #define UNIFI_DEVICE_H
#include "Util/Device.h" #include "Util/StaticDevice.h"
#include <HttpClient.h> #include <HttpClient.h>
#include <Timer.h> #include <Timer.h>
#include <mutex> #include <mutex>
@@ -15,7 +15,7 @@ namespace UniFi {
class Device class Device
{ {
public: public:
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(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);
~Device(); ~Device();
void Start(); void Start();
@@ -47,9 +47,10 @@ private:
std::vector<std::string> m_devices; std::vector<std::string> m_devices;
int m_timeout; int m_timeout;
int m_checkInterval;
std::string m_inventoryURL; std::string m_inventoryURL;
std::string m_target; std::string m_target;
std::vector<Util::Device> m_staticDevices; std::vector<Util::StaticDevice> m_staticDevices;
std::vector<std::string> m_presentDevices; std::vector<std::string> m_presentDevices;
}; };
-167
View File
@@ -1,167 +0,0 @@
#include "Device.h"
#include <algorithm>
namespace PresenceDetection {
namespace Util {
Device::Device(const std::string& wifiMac, const std::string& bluetoothMac) :
m_wifiMac(wifiMac),
m_wifiState(false),
m_bluetoothMac(bluetoothMac),
m_bluetoothState(false)
{
std::transform(m_wifiMac.begin(), m_wifiMac.end(), m_wifiMac.begin(), ::tolower);
std::transform(m_bluetoothMac.begin(), m_bluetoothMac.end(), m_bluetoothMac.begin(), ::tolower);
}
Device::Device(const Device &other) :
m_wifiMac(other.m_wifiMac),
m_wifiState(other.m_wifiState),
m_bluetoothMac(other.m_bluetoothMac),
m_bluetoothState(other.m_bluetoothState),
m_onlineURL(other.m_onlineURL),
m_wifiOnlineURL(other.m_wifiOnlineURL),
m_bluetoothOnlineURL(other.m_bluetoothOnlineURL),
m_offlineURL(other.m_offlineURL),
m_wifiOfflineURL(other.m_wifiOfflineURL),
m_bluetoothOfflineURL(other.m_bluetoothOfflineURL)
{
}
Device::~Device()
{
}
bool Device::HasWifiMac() const
{
return !m_wifiMac.empty();
}
std::string Device::WifiMac() const
{
return m_wifiMac;
}
void Device::SetWifiState(bool present)
{
m_wifiState = present;
}
bool Device::GetWifiState() const
{
return m_wifiState;
}
bool Device::HasBluetoothMac() const
{
return !m_bluetoothMac.empty();
}
std::string Device::BluetoothMac() const
{
return m_bluetoothMac;
}
void Device::SetBluetoothState(bool present)
{
m_bluetoothState = present;
}
bool Device::GetBluetoothState() const
{
return m_bluetoothState;
}
bool Device::HasOnlineURL() const
{
return !m_onlineURL.empty();
}
void Device::SetOnlineURL(const std::string& url)
{
m_onlineURL = url;
}
std::string Device::OnlineURL() const
{
return m_onlineURL;
}
bool Device::HasWifiOnlineURL() const
{
return !m_wifiOnlineURL.empty();
}
void Device::SetWifiOnlineURL(const std::string& url)
{
m_wifiOnlineURL = url;
}
std::string Device::WifiOnlineURL() const
{
return m_wifiOnlineURL;
}
bool Device::HasBluetoothOnlineURL() const
{
return !m_bluetoothOnlineURL.empty();
}
void Device::SetBluetoothOnlineURL(const std::string& url)
{
m_bluetoothOnlineURL = url;
}
std::string Device::BluetoothOnlineURL() const
{
return m_bluetoothOnlineURL;
}
bool Device::HasOfflineURL() const
{
return !m_offlineURL.empty();
}
void Device::SetOfflineURL(const std::string& url)
{
m_offlineURL = url;
}
std::string Device::OfflineURL() const
{
return m_offlineURL;
}
bool Device::HasWifiOfflineURL() const
{
return !m_wifiOfflineURL.empty();
}
void Device::SetWifiOfflineURL(const std::string& url)
{
m_wifiOfflineURL = url;
}
std::string Device::WifiOfflineURL() const
{
return m_wifiOfflineURL;
}
bool Device::HasBluetoothOfflineURL() const
{
return !m_bluetoothOfflineURL.empty();
}
void Device::SetBluetoothOfflineURL(const std::string& url)
{
m_bluetoothOfflineURL = url;
}
std::string Device::BluetoothOfflineURL() const
{
return m_bluetoothOfflineURL;
}
} // namespace Util
} // namespace PresenceDetection
+35
View File
@@ -0,0 +1,35 @@
#include "DynamicDevice.h"
#include <StringAlgorithm.h>
namespace PresenceDetection {
namespace Util {
DynamicDevice::DynamicDevice(const std::string& macAddress) :
m_probeCounter(0),
m_macAddress(macAddress)
{
}
std::string DynamicDevice::MacAddress()
{
return m_macAddress;
}
int DynamicDevice::ProbeCounter() const
{
return m_probeCounter;
}
void DynamicDevice::IncrementProbeCounter()
{
++m_probeCounter;
}
void DynamicDevice::ResetProbeCounter()
{
m_probeCounter = 0;
}
} // namespace Util
} // namespace PresenceDetection
+31
View File
@@ -0,0 +1,31 @@
#ifndef UTIL_DYNAMICDEVICE_H
#define UTIL_DYNAMICDEVICE_H
#include "ProbeableDevice.h"
#include <string>
namespace PresenceDetection {
namespace Util {
class DynamicDevice : public ProbeableDevice
{
public:
DynamicDevice(const std::string& macAddress);
virtual int ProbeCounter() const override;
virtual void IncrementProbeCounter() override;
virtual void ResetProbeCounter() override;
std::string MacAddress();
private:
int m_probeCounter;
std::string m_macAddress;
};
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_DYNAMICDEVICE_H
+21
View File
@@ -0,0 +1,21 @@
#ifndef UTIL_PROBEABLEDEVICE_H
#define UTIL_PROBEABLEDEVICE_H
#include <string>
namespace PresenceDetection {
namespace Util {
class ProbeableDevice
{
public:
virtual int ProbeCounter() const = 0;
virtual void IncrementProbeCounter() = 0;
virtual void ResetProbeCounter() = 0;
};
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_PROBEABLEDEVICE_H
+184
View File
@@ -0,0 +1,184 @@
#include "StaticDevice.h"
#include <algorithm>
namespace PresenceDetection {
namespace Util {
StaticDevice::StaticDevice(const std::string& wifiMac, const std::string& bluetoothMac) :
m_probeCounter(0),
m_wifiMac(wifiMac),
m_wifiState(false),
m_bluetoothMac(bluetoothMac),
m_bluetoothState(false)
{
std::transform(m_wifiMac.begin(), m_wifiMac.end(), m_wifiMac.begin(), ::tolower);
std::transform(m_bluetoothMac.begin(), m_bluetoothMac.end(), m_bluetoothMac.begin(), ::tolower);
}
StaticDevice::StaticDevice(const StaticDevice &other) :
m_probeCounter(other.m_probeCounter),
m_wifiMac(other.m_wifiMac),
m_wifiState(other.m_wifiState),
m_bluetoothMac(other.m_bluetoothMac),
m_bluetoothState(other.m_bluetoothState),
m_onlineURL(other.m_onlineURL),
m_wifiOnlineURL(other.m_wifiOnlineURL),
m_bluetoothOnlineURL(other.m_bluetoothOnlineURL),
m_offlineURL(other.m_offlineURL),
m_wifiOfflineURL(other.m_wifiOfflineURL),
m_bluetoothOfflineURL(other.m_bluetoothOfflineURL)
{
}
StaticDevice::~StaticDevice()
{
}
int StaticDevice::ProbeCounter() const
{
return m_probeCounter;
}
void StaticDevice::IncrementProbeCounter()
{
++m_probeCounter;
}
void StaticDevice::ResetProbeCounter()
{
m_probeCounter = 0;
}
bool StaticDevice::HasWifiMac() const
{
return !m_wifiMac.empty();
}
std::string StaticDevice::WifiMac() const
{
return m_wifiMac;
}
void StaticDevice::SetWifiState(bool present)
{
m_wifiState = present;
}
bool StaticDevice::GetWifiState() const
{
return m_wifiState;
}
bool StaticDevice::HasBluetoothMac() const
{
return !m_bluetoothMac.empty();
}
std::string StaticDevice::BluetoothMac() const
{
return m_bluetoothMac;
}
void StaticDevice::SetBluetoothState(bool present)
{
m_bluetoothState = present;
}
bool StaticDevice::GetBluetoothState() const
{
return m_bluetoothState;
}
bool StaticDevice::HasOnlineURL() const
{
return !m_onlineURL.empty();
}
void StaticDevice::SetOnlineURL(const std::string& url)
{
m_onlineURL = url;
}
std::string StaticDevice::OnlineURL() const
{
return m_onlineURL;
}
bool StaticDevice::HasWifiOnlineURL() const
{
return !m_wifiOnlineURL.empty();
}
void StaticDevice::SetWifiOnlineURL(const std::string& url)
{
m_wifiOnlineURL = url;
}
std::string StaticDevice::WifiOnlineURL() const
{
return m_wifiOnlineURL;
}
bool StaticDevice::HasBluetoothOnlineURL() const
{
return !m_bluetoothOnlineURL.empty();
}
void StaticDevice::SetBluetoothOnlineURL(const std::string& url)
{
m_bluetoothOnlineURL = url;
}
std::string StaticDevice::BluetoothOnlineURL() const
{
return m_bluetoothOnlineURL;
}
bool StaticDevice::HasOfflineURL() const
{
return !m_offlineURL.empty();
}
void StaticDevice::SetOfflineURL(const std::string& url)
{
m_offlineURL = url;
}
std::string StaticDevice::OfflineURL() const
{
return m_offlineURL;
}
bool StaticDevice::HasWifiOfflineURL() const
{
return !m_wifiOfflineURL.empty();
}
void StaticDevice::SetWifiOfflineURL(const std::string& url)
{
m_wifiOfflineURL = url;
}
std::string StaticDevice::WifiOfflineURL() const
{
return m_wifiOfflineURL;
}
bool StaticDevice::HasBluetoothOfflineURL() const
{
return !m_bluetoothOfflineURL.empty();
}
void StaticDevice::SetBluetoothOfflineURL(const std::string& url)
{
m_bluetoothOfflineURL = url;
}
std::string StaticDevice::BluetoothOfflineURL() const
{
return m_bluetoothOfflineURL;
}
} // namespace Util
} // namespace PresenceDetection
+14 -7
View File
@@ -1,18 +1,23 @@
#ifndef UTIL_DEVICE_H #ifndef UTIL_STATICDEVICE_H
#define UTIL_DEVICE_H #define UTIL_STATICDEVICE_H
#include "ProbeableDevice.h"
#include <string> #include <string>
namespace PresenceDetection { namespace PresenceDetection {
namespace Util { namespace Util {
class Device class StaticDevice : public ProbeableDevice
{ {
public: public:
Device(const std::string& wifiMac, const std::string& bluetoothMac); StaticDevice(const std::string& wifiMac, const std::string& bluetoothMac);
Device(const Device &other); StaticDevice(const StaticDevice &other);
~Device(); ~StaticDevice();
virtual int ProbeCounter() const override;
virtual void IncrementProbeCounter() override;
virtual void ResetProbeCounter() override;
bool HasWifiMac() const; bool HasWifiMac() const;
std::string WifiMac() const; std::string WifiMac() const;
@@ -45,6 +50,8 @@ public:
std::string BluetoothOfflineURL() const; std::string BluetoothOfflineURL() const;
private: private:
int m_probeCounter;
std::string m_wifiMac; std::string m_wifiMac;
bool m_wifiState; bool m_wifiState;
@@ -62,4 +69,4 @@ private:
} // namespace Util } // namespace Util
} // namespace PresenceDetection } // namespace PresenceDetection
#endif // UTIL_DEVICE_H #endif // UTIL_STATICDEVICE_H