Update Naming and Tests

This commit is contained in:
2021-07-28 16:23:27 +02:00
parent f03e90f5b9
commit 03b00fa6de
17 changed files with 566 additions and 104 deletions
+28 -28
View File
@@ -1,6 +1,6 @@
#include "Bluetooth/Device.h" #include "Bluetooth/Driver.h"
#include "UniFi/Device.h"
#include "Util/StaticDevice.h" #include "Util/StaticDevice.h"
#include "WiFi/Driver.h"
#include <clipp.h> #include <clipp.h>
#include <INIReader.h> #include <INIReader.h>
#include <Logging.h> #include <Logging.h>
@@ -70,7 +70,7 @@ int main(int argc, char** argv)
std::string target = iniReader.Get("PresenceDetection", "Target", ""); std::string target = iniReader.Get("PresenceDetection", "Target", "");
bool unifi = iniReader.GetBoolean("PresenceDetection", "UniFi", false); bool wifi = iniReader.GetBoolean("PresenceDetection", "WiFi", false);
bool bluetooth = iniReader.GetBoolean("PresenceDetection", "Bluetooth", false); bool bluetooth = iniReader.GetBoolean("PresenceDetection", "Bluetooth", false);
@@ -80,7 +80,7 @@ int main(int argc, char** argv)
for (auto section : sections) for (auto section : sections)
{ {
if (section != "PresenceDetection" && if (section != "PresenceDetection" &&
section != "UniFi" && section != "WiFi" &&
section != "Bluetooth") section != "Bluetooth")
{ {
std::string WifiMac = iniReader.Get(section, "WifiMac", ""); std::string WifiMac = iniReader.Get(section, "WifiMac", "");
@@ -123,42 +123,42 @@ int main(int argc, char** argv)
} }
} }
std::shared_ptr<PresenceDetection::UniFi::Device> pUniFiDevice; std::shared_ptr<PresenceDetection::WiFi::Driver> pWiFiDriver;
if (unifi) if (wifi)
{ {
std::string hostname = iniReader.Get("UniFi", "Hostname", ""); std::string hostname = iniReader.Get("WiFi", "Hostname", "");
if (hostname.empty()) if (hostname.empty())
throw std::runtime_error("UniFi Hostname directive missing in ini file."); throw std::runtime_error("WiFi Hostname directive missing in ini file.");
int port = iniReader.GetInteger("UniFi", "Port", 8443); int port = iniReader.GetInteger("WiFi", "Port", 8443);
std::string username = iniReader.Get("UniFi", "Username", ""); std::string username = iniReader.Get("WiFi", "Username", "");
if (username.empty()) if (username.empty())
throw std::runtime_error("UniFi Username directive missing in ini file."); throw std::runtime_error("WiFi Username directive missing in ini file.");
std::string password = iniReader.Get("UniFi", "Password", ""); std::string password = iniReader.Get("WiFi", "Password", "");
if (password.empty()) if (password.empty())
throw std::runtime_error("UniFi Password directive missing in ini file."); throw std::runtime_error("WiFi Password directive missing in ini file.");
std::string cookiefile = iniReader.Get("UniFi", "CookieFile", ""); std::string cookiefile = iniReader.Get("WiFi", "CookieFile", "");
if (cookiefile.empty()) if (cookiefile.empty())
throw std::runtime_error("UniFi CookieFile directive missing in ini file."); throw std::runtime_error("WiFi CookieFile directive missing in ini file.");
int timeout = iniReader.GetInteger("UniFi", "Timeout", 150); int timeout = iniReader.GetInteger("WiFi", "Timeout", 150);
std::string inventoryURL = iniReader.Get("UniFi", "Inventory", ""); std::string inventoryURL = iniReader.Get("WiFi", "Inventory", "");
pUniFiDevice = std::make_shared<PresenceDetection::UniFi::Device>(hostname, port, username, password, cookiefile, timeout, inventoryURL, target, staticDevices); pWiFiDriver = std::make_shared<PresenceDetection::WiFi::Driver>(hostname, port, username, password, cookiefile, timeout, inventoryURL, target, staticDevices);
} }
std::shared_ptr<PresenceDetection::Bluetooth::Device> pBluetoothDevice; std::shared_ptr<PresenceDetection::Bluetooth::Driver> pBluetoothDriver;
if (bluetooth) if (bluetooth)
{ {
int timeout = iniReader.GetInteger("Bluetooth", "Timeout", 150); int timeout = iniReader.GetInteger("Bluetooth", "Timeout", 150);
std::string inventoryURL = iniReader.Get("Bluetooth", "Inventory", ""); std::string inventoryURL = iniReader.Get("Bluetooth", "Inventory", "");
pBluetoothDevice = std::make_shared<PresenceDetection::Bluetooth::Device>(timeout, inventoryURL, target, staticDevices); pBluetoothDriver = std::make_shared<PresenceDetection::Bluetooth::Driver>(timeout, inventoryURL, target, staticDevices);
} }
sigset_t wset; sigset_t wset;
@@ -171,17 +171,17 @@ int main(int argc, char** argv)
Logging::Log(Logging::Severity::Info, "Stopping PresenceDetection..."); Logging::Log(Logging::Severity::Info, "Stopping PresenceDetection...");
if (pUniFiDevice) if (pWiFiDriver)
pUniFiDevice->Stop(); pWiFiDriver->Stop();
if (pBluetoothDevice) if (pBluetoothDriver)
pBluetoothDevice->Stop(); pBluetoothDriver->Stop();
if (pUniFiDevice) if (pWiFiDriver)
pUniFiDevice->Wait(); pWiFiDriver->Wait();
if (pBluetoothDevice) if (pBluetoothDriver)
pBluetoothDevice->Wait(); pBluetoothDriver->Wait();
Logging::CloseLog(); Logging::CloseLog();
} }
+62 -10
View File
@@ -1,11 +1,9 @@
#include "Bluetooth/Functions.h"
#include "Logic/Logic.h"
#include "WiFi/Functions.h"
#include <INIReader.h> #include <INIReader.h>
#include <Timer.h>
#include <unistd.h>
#include <algorithm> #include <algorithm>
#include <chrono>
#include <deque>
#include <iostream> #include <iostream>
#include <memory>
#include <string> #include <string>
@@ -16,6 +14,9 @@ void print()
void TestINIReader() void TestINIReader()
{ {
std::cout << "--------------------------------" << std::endl;
std::cout << "TestINIReader" << std::endl << std::endl;
INIReader reader("PresenceDetection.ini"); INIReader reader("PresenceDetection.ini");
std::string target = reader.Get("PresenceDetection", "Target", ""); std::string target = reader.Get("PresenceDetection", "Target", "");
@@ -23,22 +24,73 @@ void TestINIReader()
std::cout << "Error" << std::endl; std::cout << "Error" << std::endl;
std::cout << target.size() << " " << target << std::endl; std::cout << target.size() << " " << target << std::endl;
bool unifi = reader.GetBoolean("PresenceDetection", "UniFi", false); bool wifi = reader.GetBoolean("PresenceDetection", "WiFi", false);
if (unifi) if (wifi)
std::cout << "UniFi On" << std::endl; std::cout << "WiFi On" << std::endl;
else else
std::cout << "UniFi Off" << std::endl; std::cout << "WiFi Off" << std::endl;
bool test = reader.GetBoolean("PresenceDetection", "Test", false); bool test = reader.GetBoolean("PresenceDetection", "Test", false);
if (test) if (test)
std::cout << "Test On" << std::endl; std::cout << "Test On" << std::endl;
else else
std::cout << "Test Off" << std::endl; std::cout << "Test Off" << std::endl;
std::cout << "--------------------------------" << std::endl << std::endl;
}
void TestLogic()
{
std::cout << "--------------------------------" << std::endl;
std::cout << "TestLogic" << std::endl << std::endl;
PresenceDetection::Logic::Logic logic("http://DomoticaPi:8080/api/inventory/WirelessDevice");
logic.LoadDevicesFromConfiguration();
logic.UpdateDevicesFromInventory();
logic.DumpDevices();
std::cout << "--------------------------------" << std::endl << std::endl;
}
void TestPing()
{
std::cout << "--------------------------------" << std::endl;
std::cout << "TestPing" << std::endl << std::endl;
bool wifi = PresenceDetection::WiFi::Functions::Ping("192.168.1.174");
if (wifi)
std::cout << "192.168.1.174 Online" << std::endl;
else
std::cout << "192.168.1.174 Offline" << std::endl;
bool bluetooth = PresenceDetection::Bluetooth::Functions::Ping("64:C7:53:77:2A:32");
if (bluetooth)
std::cout << "64:C7:53:77:2A:32 Online" << std::endl;
else
std::cout << "64:C7:53:77:2A:32 Offline" << std::endl;
bluetooth = PresenceDetection::Bluetooth::Functions::Ping("C8:BC:C8:22:94:68");
if (bluetooth)
std::cout << "C8:BC:C8:22:94:68 Online" << std::endl;
else
std::cout << "C8:BC:C8:22:94:68 Offline" << std::endl;
bluetooth = PresenceDetection::Bluetooth::Functions::Ping("E8:9E:B4:25:E1:E8");
if (bluetooth)
std::cout << "E8:9E:B4:25:E1:E8 Online" << std::endl;
else
std::cout << "E8:9E:B4:25:E1:E8 Offline" << std::endl;
std::cout << "--------------------------------" << std::endl << std::endl;
} }
int main(int /*argc*/, char** /*argv[]*/) int main(int /*argc*/, char** /*argv[]*/)
{ {
TestINIReader(); //TestINIReader();
//TestLogic();
TestPing();
return 0; return 0;
} }
+19 -19
View File
@@ -1,4 +1,4 @@
#include "Device.h" #include "Driver.h"
#include "Functions.h" #include "Functions.h"
#include <json.hpp> #include <json.hpp>
#include <Logging.h> #include <Logging.h>
@@ -9,7 +9,7 @@
namespace PresenceDetection { namespace PresenceDetection {
namespace Bluetooth { namespace Bluetooth {
Device::Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::StaticDevice>& staticDevices) : Driver::Driver(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_checkInterval(3),
m_inventoryURL(inventoryURL), m_inventoryURL(inventoryURL),
@@ -22,33 +22,33 @@ Device::Device(int timeout, const std::string& inventoryURL, const std::string&
Start(); Start();
} }
Device::~Device() Driver::~Driver()
{ {
} }
void Device::Start() void Driver::Start()
{ {
int checkInterval = m_checkInterval * 1000; int checkInterval = m_checkInterval * 1000;
m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this))); m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Driver::UpdatePresentDevices, 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(&Driver::UpdateDevicesFromInventory, this)));
} }
void Device::Stop() void Driver::Stop()
{ {
m_deviceTimer.Stop(); m_deviceTimer.Stop();
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
m_inventoryTimer.Stop(); m_inventoryTimer.Stop();
} }
void Device::Wait() void Driver::Wait()
{ {
m_deviceTimer.Wait(); m_deviceTimer.Wait();
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
m_inventoryTimer.Wait(); m_inventoryTimer.Wait();
} }
void Device::ClearDevices() void Driver::ClearDevices()
{ {
for (std::vector<Util::DynamicDevice>::iterator it = m_dynamicDevices.begin(); it != m_dynamicDevices.end(); ++it) for (std::vector<Util::DynamicDevice>::iterator it = m_dynamicDevices.begin(); it != m_dynamicDevices.end(); ++it)
{ {
@@ -59,7 +59,7 @@ void Device::ClearDevices()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl; ss << "Bluetooth::Driver::ClearDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
@@ -75,14 +75,14 @@ void Device::ClearDevices()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl; ss << "Bluetooth::Driver::ClearDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
} }
} }
void Device::UpdateDevicesFromInventory() void Driver::UpdateDevicesFromInventory()
{ {
try try
{ {
@@ -102,12 +102,12 @@ void Device::UpdateDevicesFromInventory()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Bluetooth::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl; ss << "Bluetooth::Driver::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
void Device::UpdatePresentDevices() void Driver::UpdatePresentDevices()
{ {
std::vector<std::string> presentDevices; std::vector<std::string> presentDevices;
std::vector<std::string> ignoredDevices; std::vector<std::string> ignoredDevices;
@@ -149,7 +149,7 @@ void Device::UpdatePresentDevices()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl; ss << "Bluetooth::Driver::UpdatePresentDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
@@ -193,7 +193,7 @@ void Device::UpdatePresentDevices()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl; ss << "Bluetooth::Driver::UpdatePresentDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
@@ -211,7 +211,7 @@ void Device::UpdatePresentDevices()
m_presentDevices.assign(presentDevices.begin(), presentDevices.end()); m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
} }
void Device::SendStateChange(bool present, const std::string& macAddress) void Driver::SendStateChange(bool present, const std::string& macAddress)
{ {
char sign; char sign;
if (present) if (present)
@@ -237,7 +237,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl; ss << "Bluetooth::Driver::SendStateChange() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
@@ -276,7 +276,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl; ss << "Bluetooth::Driver::SendStateChange() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
+6 -6
View File
@@ -1,5 +1,5 @@
#ifndef BLUETOOTH_DEVICE_H #ifndef BLUETOOTH_DRIVER_H
#define BLUETOOTH_DEVICE_H #define BLUETOOTH_DRIVER_H
#include "Util/DynamicDevice.h" #include "Util/DynamicDevice.h"
#include "Util/StaticDevice.h" #include "Util/StaticDevice.h"
@@ -12,11 +12,11 @@
namespace PresenceDetection { namespace PresenceDetection {
namespace Bluetooth { namespace Bluetooth {
class Device class Driver
{ {
public: public:
Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::StaticDevice>& staticDevices); Driver(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::StaticDevice>& staticDevices);
~Device(); ~Driver();
void Start(); void Start();
void Stop(); void Stop();
@@ -47,4 +47,4 @@ private:
} // namespace Bluetooth } // namespace Bluetooth
} // namespace PresenceDetection } // namespace PresenceDetection
#endif // BLUETOOTH_DEVICE_H #endif // BLUETOOTH_DRIVER_H
+4 -4
View File
@@ -43,8 +43,8 @@ bool Functions::Ping(const std::string& macAddress)
str2ba(macAddress.c_str(), &socketAddress.l2_bdaddr); str2ba(macAddress.c_str(), &socketAddress.l2_bdaddr);
struct timeval timeoutStruct; struct timeval timeoutStruct;
timeoutStruct.tv_sec = 3; timeoutStruct.tv_sec = 1;
timeoutStruct.tv_usec = 0; timeoutStruct.tv_usec = 0; //500000;
fd_set writeDescriptor, errorDescriptor; fd_set writeDescriptor, errorDescriptor;
FD_ZERO(&writeDescriptor); FD_ZERO(&writeDescriptor);
@@ -68,7 +68,7 @@ bool Functions::Ping(const std::string& macAddress)
if (ret != 0) if (ret != 0)
{ {
if (select(socketDescriptor + 1, &errorDescriptor, &writeDescriptor, NULL, &timeoutStruct) < 0) if (select(socketDescriptor + 1, &errorDescriptor, &writeDescriptor, NULL, &timeoutStruct) <= 0)
return false; return false;
int error = 0; int error = 0;
@@ -125,7 +125,7 @@ bool Functions::Ping(const std::string& macAddress)
pf[0].events = POLLIN; pf[0].events = POLLIN;
int err; int err;
int timeout = 500; int timeout = 200;
if ((err = poll(pf, 1, timeout)) < 0) if ((err = poll(pf, 1, timeout)) < 0)
throw std::runtime_error("Poll failed"); throw std::runtime_error("Poll failed");
+55
View File
@@ -0,0 +1,55 @@
#include "Device.h"
namespace PresenceDetection {
namespace Device {
Device::Device(int id, const std::string& wifiMacAddress, const std::string& bluetoothAddress) :
m_id(id),
m_wifiMacAddress(wifiMacAddress),
m_bluetoothMacAddress(bluetoothAddress),
m_online(false)
{
}
Device::~Device()
{
}
Device::Device(const Device& other)
{
m_id = other.m_id;
m_wifiMacAddress = other.m_wifiMacAddress;
m_bluetoothMacAddress = other.m_bluetoothMacAddress;
m_online = other.m_online;
}
int Device::ID() const
{
return m_id;
}
std::string Device::WifiMacAddress() const
{
return m_wifiMacAddress;
}
std::string Device::BluetoothMacAddress() const
{
return m_bluetoothMacAddress;
}
bool Device::Online() const
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_online;
}
void Device::Online(bool online)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_online = online;
}
} // namespace Device
} // namespace PresenceDetection
+38
View File
@@ -0,0 +1,38 @@
#ifndef DEVICE_DEVICE_H
#define DEVICE_DEVICE_H
#include <mutex>
#include <string>
namespace PresenceDetection {
namespace Device {
class Device
{
public:
Device(int id, const std::string& wifiMacAddress, const std::string& bluetoothAddress);
~Device();
Device(const Device& other);
int ID() const;
std::string WifiMacAddress() const;
std::string BluetoothMacAddress() const;
bool Online() const;
void Online(bool online);
private:
int m_id;
std::string m_wifiMacAddress;
std::string m_bluetoothMacAddress;
mutable std::mutex m_mutex;
bool m_online;
};
} // namespace Device
} // namespace PresenceDetection
#endif // DEVICE_DEVICE_H
View File
+125
View File
@@ -0,0 +1,125 @@
/*
Logic ->
Contain all Configured Devices
-> Retrieve Devices from Domotica API
-> Retrieve Devices from Configuration
WiFi -> Check for Devices
Change to Offline
-> Ping (Internal to WiFi)
Fail
-> Update Device to Offline
Success
-> NOOP
Change to Online
-> Ping (Internal to WiFi)
Fail
-> NOOP
Success
-> Update Device to Online
Bluetooth -> Check for Devices
Change to Offline
Change to Online
API -> Poll Devices
- Who keeps track of the devices?
Logic?
Driver?
//*/
#include "Logic.h"
#include <Logging.h>
#include <json.hpp>
#include <algorithm>
#include <iostream>
#include <sstream>
namespace PresenceDetection {
namespace Logic {
Logic::Logic(const std::string& inventoryURL) :
m_inventoryURL(inventoryURL)
{
}
Logic::~Logic()
{
}
void Logic::LoadDevicesFromConfiguration()
{
}
void Logic::UpdateDevicesFromInventory()
{
try
{
Http::HttpRequest request(m_inventoryURL);
std::string devices = m_httpClient.Open(request);
nlohmann::json json = nlohmann::json::parse(devices);
m_dynamicDevices.clear();
for (auto& element : json)
{
int id = std::stoi(element["id"].get<std::string>());
std::string wifi;
std::string bluetooth;
//if (element["wifi"] != "")
if (element["macaddress"] != "")
{
//wifi = element["wifi"];
wifi = element["macaddress"];
std::transform(wifi.begin(), wifi.end(), wifi.begin(), ::tolower);
}
if (element["bluetooth"] != "")
{
bluetooth = element["bluetooth"];
std::transform(bluetooth.begin(), bluetooth.end(), bluetooth.begin(), ::tolower);
}
m_dynamicDevices.push_back(Device::Device(id, wifi, bluetooth));
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Logic::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
void Logic::DumpDevices()
{
std::cout << "Static Devices:" << std::endl;
for (auto& device : m_staticDevices)
{
std::cout << "ID: " << device.ID() << std::endl;
std::cout << "Wifi: " << device.WifiMacAddress() << std::endl;
std::cout << "Bluetooth: " << device.BluetoothMacAddress() << std::endl;
std::cout << "Online: " << device.Online() << std::endl;
std::cout << std::endl;
}
std::cout << "Dynamic Devices:" << std::endl;
for (auto& device : m_dynamicDevices)
{
std::cout << "ID: " << device.ID() << std::endl;
std::cout << "Wifi: " << device.WifiMacAddress() << std::endl;
std::cout << "Bluetooth: " << device.BluetoothMacAddress() << std::endl;
std::cout << "Online: " << device.Online() << std::endl;
std::cout << std::endl;
}
}
} // namespace Logic
} // namespace PresenceDetection
+41
View File
@@ -0,0 +1,41 @@
#ifndef LOGIC_LOGIC_H
#define LOGIC_LOGIC_H
#include "Device/Device.h"
#include <HttpClient.h>
#include <string>
#include <vector>
namespace PresenceDetection {
namespace Logic {
class Logic
{
public:
Logic(const std::string& inventoryURL);
~Logic();
void LoadDevicesFromConfiguration();
void UpdateDevicesFromInventory();
void DumpDevices();
private:
//void UpdatePresentDevices();
void PollBluetoothDevices();
void PollWiFiDevices();
private:
Http::HttpClient m_httpClient;
std::string m_inventoryURL;
std::vector<Device::Device> m_dynamicDevices;
std::vector<Device::Device> m_staticDevices;
};
} // namespace Logic
} // namespace PresenceDetection
#endif // LOGIC_LOGIC_H
+1
View File
@@ -0,0 +1 @@
../Makefile
+1 -1
View File
@@ -3,7 +3,7 @@ Target =
UniFi = UniFi =
Bluetooth = Bluetooth =
[UniFi] [WiFi]
Hostname = UniFi Hostname = UniFi
Port = Port =
Username = Username =
+33 -28
View File
@@ -1,4 +1,4 @@
#include "Device.h" #include "Driver.h"
#include <json.hpp> #include <json.hpp>
#include <Logging.h> #include <Logging.h>
#include <algorithm> #include <algorithm>
@@ -6,9 +6,9 @@
namespace PresenceDetection { namespace PresenceDetection {
namespace UniFi { namespace WiFi {
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) : Driver::Driver(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),
@@ -27,34 +27,34 @@ Device::Device(const std::string& hostname, int port, const std::string& usernam
Start(); Start();
} }
Device::~Device() Driver::~Driver()
{ {
Logout(); Logout();
} }
void Device::Start() void Driver::Start()
{ {
int checkInterval = m_checkInterval * 1000; int checkInterval = m_checkInterval * 1000;
m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this))); m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Driver::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(&Driver::UpdateDevicesFromInventory, this)));
} }
void Device::Stop() void Driver::Stop()
{ {
m_deviceTimer.Stop(); m_deviceTimer.Stop();
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
m_inventoryTimer.Stop(); m_inventoryTimer.Stop();
} }
void Device::Wait() void Driver::Wait()
{ {
m_deviceTimer.Wait(); m_deviceTimer.Wait();
if (!m_inventoryURL.empty()) if (!m_inventoryURL.empty())
m_inventoryTimer.Stop(); m_inventoryTimer.Stop();
} }
bool Device::Login() bool Driver::Login()
{ {
std::stringstream url; std::stringstream url;
url << "https://" << m_hostname << ":" << m_port << "/api/login"; url << "https://" << m_hostname << ":" << m_port << "/api/login";
@@ -88,7 +88,7 @@ bool Device::Login()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::Login() - Error: " << e.what() << std::endl; ss << "WiFi::Driver::Login() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
std::lock_guard<std::mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
m_loggedIn = false; m_loggedIn = false;
@@ -100,7 +100,7 @@ bool Device::Login()
return m_loggedIn; return m_loggedIn;
} }
void Device::Logout() void Driver::Logout()
{ {
std::stringstream url; std::stringstream url;
url << "https://" << m_hostname << ":" << m_port << "/logout"; url << "https://" << m_hostname << ":" << m_port << "/logout";
@@ -118,12 +118,12 @@ void Device::Logout()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::Logout() - Error: " << e.what() << std::endl; ss << "WiFi::Driver::Logout() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
void Device::ClearDevices() void Driver::ClearDevices()
{ {
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it) for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
{ {
@@ -134,7 +134,7 @@ void Device::ClearDevices()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::ClearDevices() - Error: " << e.what() << std::endl; ss << "WiFi::Driver::ClearDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
@@ -150,14 +150,14 @@ void Device::ClearDevices()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::ClearDevices() - Error: " << e.what() << std::endl; ss << "WiFi::Driver::ClearDevices() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
} }
} }
void Device::UpdateDevicesFromInventory() void Driver::UpdateDevicesFromInventory()
{ {
try try
{ {
@@ -177,12 +177,12 @@ void Device::UpdateDevicesFromInventory()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl; ss << "WiFi::Driver::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
void Device::UpdatePresentDevices() void Driver::UpdatePresentDevices()
{ {
bool loggedIn; bool loggedIn;
{ {
@@ -229,7 +229,12 @@ void Device::UpdatePresentDevices()
if ((timeStamp - lastSeen) < m_timeout) if ((timeStamp - lastSeen) < m_timeout)
{ {
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end()) if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
{
addedDevices.push_back(macAddress); addedDevices.push_back(macAddress);
std::stringstream ss;
ss << "Device Added: " << device.dump() << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
}
presentDevices.push_back(macAddress); presentDevices.push_back(macAddress);
} }
else else
@@ -238,7 +243,7 @@ void Device::UpdatePresentDevices()
{ {
std::stringstream ss; std::stringstream ss;
ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl; ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl;
Logging::Log(Logging::Severity::Debug, ss.str()); Logging::Log(Logging::Severity::Info, ss.str());
} }
} }
} }
@@ -260,7 +265,7 @@ void Device::UpdatePresentDevices()
{ {
std::stringstream ss; std::stringstream ss;
ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl; ss << "TimeOut (" << m_timeout << "): " << macAddress << std::endl;
Logging::Log(Logging::Severity::Debug, ss.str()); Logging::Log(Logging::Severity::Info, ss.str());
} }
} }
} }
@@ -269,7 +274,7 @@ void Device::UpdatePresentDevices()
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::IsDevicePresent() - Error: " << e.what() << std::endl; ss << "WiFi::Driver::IsDevicePresent() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
Logout(); Logout();
return; return;
@@ -288,7 +293,7 @@ void Device::UpdatePresentDevices()
m_presentDevices.assign(presentDevices.begin(), presentDevices.end()); m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
} }
void Device::SendStateChange(bool present, const std::string& macAddress) void Driver::SendStateChange(bool present, const std::string& macAddress)
{ {
char sign; char sign;
if (present) if (present)
@@ -297,13 +302,13 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
sign = '-'; sign = '-';
std::stringstream ss; std::stringstream ss;
ss << "UniFi: " << sign << " " << macAddress; ss << "WiFi: " << sign << " " << macAddress;
Logging::Log(Logging::Severity::Info, ss.str()); Logging::Log(Logging::Severity::Info, ss.str());
if (!m_target.empty()) if (!m_target.empty())
{ {
std::stringstream url; std::stringstream url;
url << m_target << "/UniFi/" << sign << "/" << macAddress; url << m_target << "/WiFi/" << sign << "/" << macAddress;
try try
{ {
@@ -314,7 +319,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl; ss << "WiFi::Driver::SendStateChange() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
@@ -353,7 +358,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl; ss << "WiFi::Driver::SendStateChange() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
} }
} }
@@ -361,5 +366,5 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
} }
} }
} // namespace UniFi } // namespace WiFi
} // namespace PresenceDetection } // namespace PresenceDetection
+8 -8
View File
@@ -1,5 +1,5 @@
#ifndef UNIFI_DEVICE_H #ifndef WIFI_DRIVER_H
#define UNIFI_DEVICE_H #define WIFI_DRIVER_H
#include "Util/StaticDevice.h" #include "Util/StaticDevice.h"
#include <HttpClient.h> #include <HttpClient.h>
@@ -10,13 +10,13 @@
namespace PresenceDetection { namespace PresenceDetection {
namespace UniFi { namespace WiFi {
class Device class Driver
{ {
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::StaticDevice>& staticDevices); Driver(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(); ~Driver();
void Start(); void Start();
void Stop(); void Stop();
@@ -55,7 +55,7 @@ private:
std::vector<std::string> m_presentDevices; std::vector<std::string> m_presentDevices;
}; };
} // namespace UniFi } // namespace WiFi
} // namespace PresenceDetection } // namespace PresenceDetection
#endif // UNIFI_DEVICE_H #endif // WIFI_DRIVER_H
+122
View File
@@ -0,0 +1,122 @@
#include "Functions.h"
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <unistd.h>
#include <string>
#include <stdexcept>
namespace PresenceDetection {
namespace WiFi {
#define DEFDATALEN (64-ICMP_MINLEN)
#define MAXIPLEN 60
#define MAXICMPLEN 76
#define MAXPACKET (65536 - 60 - ICMP_MINLEN)
bool Functions::Ping(const std::string& ipAddress)
{
int s, i, cc, packlen, datalen = DEFDATALEN;
struct hostent *hp;
struct sockaddr_in to, from;
struct ip *ip;
u_char *packet, outpack[MAXPACKET];
char hnamebuf[MAXHOSTNAMELEN];
std::string hostname = ipAddress;
struct icmp *icp;
int ret, fromlen, hlen;
fd_set rfds;
struct timeval tv;
int retval;
to.sin_family = AF_INET;
to.sin_addr.s_addr = inet_addr(ipAddress.c_str());
packlen = datalen + MAXIPLEN + MAXICMPLEN;
if ( (packet = (u_char *)malloc((u_int)packlen)) == NULL)
throw std::runtime_error("Can't allocate Packet");
if ( (s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
throw std::runtime_error("Can't send on ICMP Socket");
icp = (struct icmp*)outpack;
icp->icmp_type = ICMP_ECHO;
icp->icmp_code = 0;
icp->icmp_cksum = 0;
icp->icmp_seq = 12345;
icp->icmp_id = getpid();
cc = datalen + ICMP_MINLEN;
icp->icmp_cksum = ICMPChecksum((unsigned short*)icp,cc);
i = sendto(s, (char*)outpack, cc, 0, (struct sockaddr*)&to, (socklen_t)sizeof(struct sockaddr_in));
if (i < 0)
throw std::runtime_error("Can't send on ICMP Packet");
FD_ZERO(&rfds);
FD_SET(s, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 500000;
while(1)
{
retval = select(s+1, &rfds, NULL, NULL, &tv);
if (retval == -1)
throw std::runtime_error("Can't select file descriptor");
if (!retval)
return false;
fromlen = sizeof(sockaddr_in);
if ((ret = recvfrom(s, (char*)packet, packlen, 0,(struct sockaddr*)&from, (socklen_t*)&fromlen)) < 0)
throw std::runtime_error("Can't receive message from socket");
ip = (struct ip*)((char*)packet);
hlen = sizeof(struct ip);
if (ret < (hlen + ICMP_MINLEN))
throw std::runtime_error("Can't allocate memory for receiving packets");
icp = (struct icmp*)(packet + hlen);
if (icp->icmp_type != ICMP_ECHOREPLY)
continue;
if (icp->icmp_seq != 12345 || icp->icmp_id != getpid())
continue;
return true;
}
return false;
}
uint16_t Functions::ICMPChecksum(uint16_t* addr, unsigned len)
{
uint16_t answer = 0;
uint32_t sum = 0;
while (len > 1)
{
sum += *addr++;
len -= 2;
}
if (len == 1)
{
*(unsigned char*)&answer = *(unsigned char*)addr ;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return answer;
}
} // namespace WiFi
} // namespace PresenceDetection
+22
View File
@@ -0,0 +1,22 @@
#ifndef WIFI_FUNCTIONS_H
#define WIFI_FUNCTIONS_H
#include <string>
namespace PresenceDetection {
namespace WiFi {
class Functions
{
public:
static bool Ping(const std::string& ipAddress);
private:
static uint16_t ICMPChecksum(uint16_t* addr, unsigned len);
};
} // namespace WiFi
} // namespace PresenceDetection
#endif // WIFI_FUNCTIONS_H
+1
View File
@@ -0,0 +1 @@
../Makefile