diff --git a/Application/PresenceDetection.cc b/Application/PresenceDetection.cc index 060f69e..c74ee31 100644 --- a/Application/PresenceDetection.cc +++ b/Application/PresenceDetection.cc @@ -1,6 +1,6 @@ #include "Bluetooth/Device.h" #include "UniFi/Device.h" -#include "Util/Device.h" +#include "Util/StaticDevice.h" #include "Util/INIh.h" #include #include @@ -41,7 +41,7 @@ int main(int argc, char** argv) if (daemon) { - Logging::Log(Logging::Severity::Info, "Starting Daemon"); + Logging::Log(Logging::Severity::Info, "Starting PresenceDetection Daemon"); pid_t pid, sid; pid = fork(); @@ -62,6 +62,11 @@ int main(int argc, char** argv) close(STDOUT_FILENO); close(STDERR_FILENO); } + else + { + Logging::Log(Logging::Severity::Info, "Starting PresenceDetection"); + } + std::string target = iniReader.Get("PresenceDetection", "Target", ""); @@ -69,7 +74,7 @@ int main(int argc, char** argv) bool bluetooth = iniReader.GetBoolean("PresenceDetection", "Bluetooth", false); - std::vector devices; + std::vector staticDevices; auto sections = iniReader.Sections(); for (auto section : sections) @@ -88,33 +93,33 @@ int main(int argc, char** argv) 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", ""); if (!OnlineURL.empty()) - device.SetOnlineURL(OnlineURL); + staticDevice.SetOnlineURL(OnlineURL); std::string WifiOnlineURL = iniReader.Get(section, "WifiOnlineURL", ""); if (!WifiOnlineURL.empty()) - device.SetWifiOnlineURL(WifiOnlineURL); + staticDevice.SetWifiOnlineURL(WifiOnlineURL); std::string BluetoothOnlineURL = iniReader.Get(section, "BluetoothOnlineURL", ""); if (!BluetoothOnlineURL.empty()) - device.SetBluetoothOnlineURL(BluetoothOnlineURL); + staticDevice.SetBluetoothOnlineURL(BluetoothOnlineURL); std::string OfflineURL = iniReader.Get(section, "OfflineURL", ""); if (!OfflineURL.empty()) - device.SetOfflineURL(OfflineURL); + staticDevice.SetOfflineURL(OfflineURL); std::string WifiOfflineURL = iniReader.Get(section, "WifiOfflineURL", ""); if (!WifiOfflineURL.empty()) - device.SetWifiOfflineURL(WifiOfflineURL); + staticDevice.SetWifiOfflineURL(WifiOfflineURL); std::string BluetoothOfflineURL = iniReader.Get(section, "BluetoothOfflineURL", ""); 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", ""); - pUniFiDevice = std::make_shared(hostname, port, username, password, cookiefile, timeout, inventoryURL, target, devices); + pUniFiDevice = std::make_shared(hostname, port, username, password, cookiefile, timeout, inventoryURL, target, staticDevices); } std::shared_ptr pBluetoothDevice; @@ -153,7 +158,7 @@ int main(int argc, char** argv) std::string inventoryURL = iniReader.Get("Bluetooth", "Inventory", ""); - pBluetoothDevice = std::make_shared(timeout, inventoryURL, target, devices); + pBluetoothDevice = std::make_shared(timeout, inventoryURL, target, staticDevices); } sigset_t wset; @@ -164,7 +169,7 @@ int main(int argc, char** argv) int sig; sigwait(&wset,&sig); - Logging::Log(Logging::Severity::Info, "Stopping..."); + Logging::Log(Logging::Severity::Info, "Stopping PresenceDetection..."); if (pUniFiDevice) pUniFiDevice->Stop(); diff --git a/Application/Test.cc b/Application/Test.cc index bda1986..5b38fc9 100644 --- a/Application/Test.cc +++ b/Application/Test.cc @@ -14,85 +14,6 @@ void print() std::cout << "Print" << std::endl; } -void TestTimerAbort() -{ - Timer::Timer timer; - std::function 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::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::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::steady_clock::now() - start); - std::cout << "Elapsed: " << elapsed.count() << std::endl; - std::cout << "---" << std::endl << std::endl; -} - -typedef std::unique_ptr Pointer; - -template -std::unique_ptr make_unique(Args&&... args) { - return std::unique_ptr(new T(std::forward(args)...)); -} - -void OnTimerEnd(const std::string& type, int id) -{ - std::cout << "OnTimerEnd() " << type << " " << id <& timers) -{ - int delay(2000); - std::string identifier("Blabla"); - int id(10); - - // Protect! - auto timer = make_unique(); - timer->StartSingle(delay, static_cast>(std::bind(&OnTimerEnd, std::placeholders::_1, std::placeholders::_2)), identifier, id); - timers.push_back(std::move(timer)); -} - -void Cleanup(std::deque& timers) -{ - timers.erase(std::remove_if(timers.begin(), timers.end(), [](Pointer& t){return !t->IsRunning();}), timers.end()); -} - -void TestTimerDeque() -{ - std::deque 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() { PresenceDetection::Util::INIReader reader("PresenceDetection.ini"); diff --git a/Bluetooth/Device.cpp b/Bluetooth/Device.cpp index bd409e1..78f2f33 100644 --- a/Bluetooth/Device.cpp +++ b/Bluetooth/Device.cpp @@ -9,11 +9,12 @@ namespace PresenceDetection { namespace Bluetooth { -Device::Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector& devices) : +Device::Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector& staticDevices) : m_timeout(timeout), + m_checkInterval(3), m_inventoryURL(inventoryURL), m_target(target), - m_staticDevices(devices) + m_staticDevices(staticDevices) { if (!m_inventoryURL.empty()) UpdateDevicesFromInventory(); @@ -27,36 +28,33 @@ Device::~Device() void Device::Start() { - int timeout = m_timeout * 1000; - m_onlineDeviceTimer.StartContinuous(timeout, static_cast>(std::bind(&Device::UpdateOnlineDevices, this))); - m_offlineDeviceTimer.StartContinuous(3000, static_cast>(std::bind(&Device::UpdateOfflineDevices, this))); + int checkInterval = m_checkInterval * 1000; + m_deviceTimer.StartContinuous(checkInterval, static_cast>(std::bind(&Device::UpdatePresentDevices, this))); if (!m_inventoryURL.empty()) m_inventoryTimer.StartContinuous(600000, static_cast>(std::bind(&Device::UpdateDevicesFromInventory, this))); } void Device::Stop() { - m_onlineDeviceTimer.Stop(); - m_offlineDeviceTimer.Stop(); + m_deviceTimer.Stop(); if (!m_inventoryURL.empty()) m_inventoryTimer.Stop(); } void Device::Wait() { - m_onlineDeviceTimer.Wait(); - m_offlineDeviceTimer.Wait(); + m_deviceTimer.Wait(); if (!m_inventoryURL.empty()) m_inventoryTimer.Wait(); } void Device::ClearDevices() { - for (std::vector::iterator it = m_devices.begin(); it != m_devices.end(); ++it) + for (std::vector::iterator it = m_dynamicDevices.begin(); it != m_dynamicDevices.end(); ++it) { try { - SendStateChange(false, *it); + SendStateChange(false, it->MacAddress()); } catch (const std::exception& e) { @@ -66,7 +64,7 @@ void Device::ClearDevices() } } - for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) + for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) { if (it->HasBluetoothMac()) { @@ -91,13 +89,13 @@ void Device::UpdateDevicesFromInventory() std::string devices = m_httpClient.GetUrlContents(m_inventoryURL); Util::JSON json = Util::JSON::parse(devices); - m_devices.clear(); + m_dynamicDevices.clear(); for (auto& element : json) if (element["bluetooth"] != "") { std::string bluetooth = element["bluetooth"]; 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) @@ -108,40 +106,44 @@ void Device::UpdateDevicesFromInventory() } } -void Device::UpdateOnlineDevices() -{ - UpdatePresentDevices(true); -} - -void Device::UpdateOfflineDevices() -{ - UpdatePresentDevices(false); -} - -void Device::UpdatePresentDevices(bool updateOnlineDevices) +void Device::UpdatePresentDevices() { std::vector presentDevices; std::vector ignoredDevices; - for (std::vector::iterator it = m_devices.begin(); it != m_devices.end(); ++it) + for (std::vector::iterator it = m_dynamicDevices.begin(); it != m_dynamicDevices.end(); ++it) { try { bool pollDevice = false; - if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end()) - pollDevice = true; - else - pollDevice = updateOnlineDevices; - - if (pollDevice && Functions::Ping(*it)) + if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->MacAddress()) == m_presentDevices.end()) { - if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end()) - SendStateChange(true, *it); - presentDevices.push_back(*it); + pollDevice = true; + } + else + { + it->IncrementProbeCounter(); + if (it->ProbeCounter() * m_checkInterval >= m_timeout) + { + pollDevice = true; + it->ResetProbeCounter(); + } } - if (!pollDevice) - ignoredDevices.push_back(*it); + if (pollDevice && Functions::Ping(it->MacAddress())) + { + 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) { @@ -151,7 +153,7 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices) } } - for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) + for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) { if (it->HasBluetoothMac()) { @@ -159,9 +161,18 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices) { bool pollDevice = false; if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end()) + { pollDevice = true; + } else - pollDevice = updateOnlineDevices; + { + it->IncrementProbeCounter(); + if (it->ProbeCounter() * m_checkInterval >= m_timeout) + { + pollDevice = true; + it->ResetProbeCounter(); + } + } if (pollDevice && Functions::Ping(it->BluetoothMac())) { @@ -169,9 +180,14 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices) SendStateChange(true, it->BluetoothMac()); presentDevices.push_back(it->BluetoothMac()); } - - if (!pollDevice) + else if (pollDevice) + { + it->ResetProbeCounter(); + } + else + { ignoredDevices.push_back(it->BluetoothMac()); + } } catch (const std::exception& e) { @@ -223,7 +239,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress) } } - for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) + for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) { if (it->HasBluetoothMac() && it->BluetoothMac() == macAddress) { diff --git a/Bluetooth/Device.h b/Bluetooth/Device.h index 6e199d9..b956589 100644 --- a/Bluetooth/Device.h +++ b/Bluetooth/Device.h @@ -1,7 +1,8 @@ #ifndef BLUETOOTH_DEVICE_H #define BLUETOOTH_DEVICE_H -#include "Util/Device.h" +#include "Util/DynamicDevice.h" +#include "Util/StaticDevice.h" #include #include #include @@ -14,7 +15,7 @@ namespace Bluetooth { class Device { public: - Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector& devices); + Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector& staticDevices); ~Device(); void Start(); @@ -24,23 +25,21 @@ public: private: void ClearDevices(); void UpdateDevicesFromInventory(); - void UpdateOnlineDevices(); - void UpdateOfflineDevices(); - void UpdatePresentDevices(bool updateOnlineDevices); + void UpdatePresentDevices(); void SendStateChange(bool present, const std::string& macAddress); private: - Timer::Timer m_onlineDeviceTimer; - Timer::Timer m_offlineDeviceTimer; + Timer::Timer m_deviceTimer; Timer::Timer m_inventoryTimer; Http::HttpClient m_httpClient; - std::vector m_devices; + std::vector m_dynamicDevices; int m_timeout; + int m_checkInterval; std::string m_inventoryURL; std::string m_target; - std::vector m_staticDevices; + std::vector m_staticDevices; std::vector m_presentDevices; }; diff --git a/UniFi/Device.cpp b/UniFi/Device.cpp index d2af0db..9394340 100644 --- a/UniFi/Device.cpp +++ b/UniFi/Device.cpp @@ -8,7 +8,7 @@ namespace PresenceDetection { 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& 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& staticDevices) : m_loggedIn(false), m_hostname(hostname), m_port(port), @@ -16,9 +16,10 @@ Device::Device(const std::string& hostname, int port, const std::string& usernam m_password(password), m_cookieFile(cookieFile), m_timeout(timeout), + m_checkInterval(5), m_inventoryURL(inventoryURL), m_target(target), - m_staticDevices(devices) + m_staticDevices(staticDevices) { if (!m_inventoryURL.empty()) UpdateDevicesFromInventory(); @@ -33,7 +34,8 @@ Device::~Device() void Device::Start() { - m_deviceTimer.StartContinuous(5000, static_cast>(std::bind(&Device::UpdatePresentDevices, this))); + int checkInterval = m_checkInterval * 1000; + m_deviceTimer.StartContinuous(checkInterval, static_cast>(std::bind(&Device::UpdatePresentDevices, this))); if (!m_inventoryURL.empty()) m_inventoryTimer.StartContinuous(300000, static_cast>(std::bind(&Device::UpdateDevicesFromInventory, this))); } @@ -129,7 +131,7 @@ void Device::ClearDevices() } } - for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) + for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) { if (it->HasWifiMac()) { @@ -219,9 +221,18 @@ void Device::UpdatePresentDevices() addedDevices.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::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) + for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) { if ((timeStamp - lastSeen) < m_timeout) { @@ -232,6 +243,15 @@ void Device::UpdatePresentDevices() 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::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) + for (std::vector::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it) { if (it->HasWifiMac() && it->WifiMac() == macAddress) { diff --git a/UniFi/Device.h b/UniFi/Device.h index 02242fc..d2878d2 100644 --- a/UniFi/Device.h +++ b/UniFi/Device.h @@ -1,7 +1,7 @@ #ifndef UNIFI_DEVICE_H #define UNIFI_DEVICE_H -#include "Util/Device.h" +#include "Util/StaticDevice.h" #include #include #include @@ -15,7 +15,7 @@ namespace UniFi { class Device { 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& 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& staticDevices); ~Device(); void Start(); @@ -47,9 +47,10 @@ private: std::vector m_devices; int m_timeout; + int m_checkInterval; std::string m_inventoryURL; std::string m_target; - std::vector m_staticDevices; + std::vector m_staticDevices; std::vector m_presentDevices; }; diff --git a/Util/Device.cpp b/Util/Device.cpp deleted file mode 100644 index a6d3ebb..0000000 --- a/Util/Device.cpp +++ /dev/null @@ -1,167 +0,0 @@ -#include "Device.h" -#include - - -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 diff --git a/Util/DynamicDevice.cpp b/Util/DynamicDevice.cpp new file mode 100644 index 0000000..a34a91b --- /dev/null +++ b/Util/DynamicDevice.cpp @@ -0,0 +1,35 @@ +#include "DynamicDevice.h" +#include + + +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 diff --git a/Util/DynamicDevice.h b/Util/DynamicDevice.h new file mode 100644 index 0000000..18e6196 --- /dev/null +++ b/Util/DynamicDevice.h @@ -0,0 +1,31 @@ +#ifndef UTIL_DYNAMICDEVICE_H +#define UTIL_DYNAMICDEVICE_H + +#include "ProbeableDevice.h" +#include + + +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 diff --git a/Util/ProbeableDevice.h b/Util/ProbeableDevice.h new file mode 100644 index 0000000..103a267 --- /dev/null +++ b/Util/ProbeableDevice.h @@ -0,0 +1,21 @@ +#ifndef UTIL_PROBEABLEDEVICE_H +#define UTIL_PROBEABLEDEVICE_H + +#include + + +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 diff --git a/Util/StaticDevice.cpp b/Util/StaticDevice.cpp new file mode 100644 index 0000000..475d837 --- /dev/null +++ b/Util/StaticDevice.cpp @@ -0,0 +1,184 @@ +#include "StaticDevice.h" +#include + + +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 diff --git a/Util/Device.h b/Util/StaticDevice.h similarity index 75% rename from Util/Device.h rename to Util/StaticDevice.h index f46b348..220a36d 100644 --- a/Util/Device.h +++ b/Util/StaticDevice.h @@ -1,18 +1,23 @@ -#ifndef UTIL_DEVICE_H -#define UTIL_DEVICE_H +#ifndef UTIL_STATICDEVICE_H +#define UTIL_STATICDEVICE_H +#include "ProbeableDevice.h" #include namespace PresenceDetection { namespace Util { -class Device +class StaticDevice : public ProbeableDevice { public: - Device(const std::string& wifiMac, const std::string& bluetoothMac); - Device(const Device &other); - ~Device(); + StaticDevice(const std::string& wifiMac, const std::string& bluetoothMac); + StaticDevice(const StaticDevice &other); + ~StaticDevice(); + + virtual int ProbeCounter() const override; + virtual void IncrementProbeCounter() override; + virtual void ResetProbeCounter() override; bool HasWifiMac() const; std::string WifiMac() const; @@ -45,6 +50,8 @@ public: std::string BluetoothOfflineURL() const; private: + int m_probeCounter; + std::string m_wifiMac; bool m_wifiState; @@ -62,4 +69,4 @@ private: } // namespace Util } // namespace PresenceDetection -#endif // UTIL_DEVICE_H +#endif // UTIL_STATICDEVICE_H