Update Naming and Tests
This commit is contained in:
+370
@@ -0,0 +1,370 @@
|
||||
#include "Driver.h"
|
||||
#include <json.hpp>
|
||||
#include <Logging.h>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
namespace PresenceDetection {
|
||||
namespace WiFi {
|
||||
|
||||
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_hostname(hostname),
|
||||
m_port(port),
|
||||
m_username(username),
|
||||
m_password(password),
|
||||
m_cookieFile(cookieFile),
|
||||
m_timeout(timeout),
|
||||
m_checkInterval(5),
|
||||
m_inventoryURL(inventoryURL),
|
||||
m_target(target),
|
||||
m_staticDevices(staticDevices)
|
||||
{
|
||||
if (!m_inventoryURL.empty())
|
||||
UpdateDevicesFromInventory();
|
||||
ClearDevices();
|
||||
Start();
|
||||
}
|
||||
|
||||
Driver::~Driver()
|
||||
{
|
||||
Logout();
|
||||
}
|
||||
|
||||
void Driver::Start()
|
||||
{
|
||||
int checkInterval = m_checkInterval * 1000;
|
||||
m_deviceTimer.StartContinuous(checkInterval, static_cast<std::function<void()>>(std::bind(&Driver::UpdatePresentDevices, this)));
|
||||
if (!m_inventoryURL.empty())
|
||||
m_inventoryTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Driver::UpdateDevicesFromInventory, this)));
|
||||
}
|
||||
|
||||
void Driver::Stop()
|
||||
{
|
||||
m_deviceTimer.Stop();
|
||||
if (!m_inventoryURL.empty())
|
||||
m_inventoryTimer.Stop();
|
||||
}
|
||||
|
||||
void Driver::Wait()
|
||||
{
|
||||
m_deviceTimer.Wait();
|
||||
if (!m_inventoryURL.empty())
|
||||
m_inventoryTimer.Stop();
|
||||
}
|
||||
|
||||
bool Driver::Login()
|
||||
{
|
||||
std::stringstream url;
|
||||
url << "https://" << m_hostname << ":" << m_port << "/api/login";
|
||||
|
||||
nlohmann::json json;
|
||||
json["password"] = m_password;
|
||||
json["username"] = m_username;
|
||||
|
||||
try
|
||||
{
|
||||
std::vector<std::string> headers;
|
||||
headers.push_back("Content-Type: application/json");
|
||||
|
||||
std::stringstream output;
|
||||
Http::HttpRequest request(url.str());
|
||||
request.Method(Http::HttpRequest::Method::POST);
|
||||
request.Headers(headers);
|
||||
request.CookieFile(m_cookieFile);
|
||||
request.Data(json.dump());
|
||||
output << m_httpClient.Open(request);
|
||||
|
||||
nlohmann::json outputJSON = nlohmann::json::parse(output);
|
||||
|
||||
if (outputJSON["meta"]["rc"] != "ok")
|
||||
{
|
||||
std::stringstream error;
|
||||
error << "Login Failed - " << output.str();
|
||||
throw std::runtime_error(error.str());
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WiFi::Driver::Login() - Error: " << e.what() << std::endl;
|
||||
Logging::Log(Logging::Severity::Error, ss.str());
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_loggedIn = false;
|
||||
return m_loggedIn;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_loggedIn = true;
|
||||
return m_loggedIn;
|
||||
}
|
||||
|
||||
void Driver::Logout()
|
||||
{
|
||||
std::stringstream url;
|
||||
url << "https://" << m_hostname << ":" << m_port << "/logout";
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_loggedIn = false;
|
||||
|
||||
try
|
||||
{
|
||||
Http::HttpRequest request(url.str());
|
||||
request.ReturnType(Http::HttpRequest::ReturnType::None);
|
||||
request.CookieFile(m_cookieFile);
|
||||
m_httpClient.Open(request);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WiFi::Driver::Logout() - Error: " << e.what() << std::endl;
|
||||
Logging::Log(Logging::Severity::Error, ss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void Driver::ClearDevices()
|
||||
{
|
||||
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
|
||||
{
|
||||
try
|
||||
{
|
||||
SendStateChange(false, *it);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WiFi::Driver::ClearDevices() - Error: " << e.what() << std::endl;
|
||||
Logging::Log(Logging::Severity::Error, ss.str());
|
||||
}
|
||||
}
|
||||
|
||||
for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
|
||||
{
|
||||
if (it->HasWifiMac())
|
||||
{
|
||||
try
|
||||
{
|
||||
SendStateChange(false, it->WifiMac());
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WiFi::Driver::ClearDevices() - Error: " << e.what() << std::endl;
|
||||
Logging::Log(Logging::Severity::Error, ss.str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Driver::UpdateDevicesFromInventory()
|
||||
{
|
||||
try
|
||||
{
|
||||
Http::HttpRequest request(m_inventoryURL);
|
||||
std::string devices = m_httpClient.Open(request);
|
||||
nlohmann::json json = nlohmann::json::parse(devices);
|
||||
|
||||
m_devices.clear();
|
||||
for (auto& element : json)
|
||||
if (element["macaddress"] != "")
|
||||
{
|
||||
std::string macAddress = element["macaddress"];
|
||||
std::transform(macAddress.begin(), macAddress.end(), macAddress.begin(), ::tolower);
|
||||
m_devices.push_back(macAddress);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WiFi::Driver::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
|
||||
Logging::Log(Logging::Severity::Error, ss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void Driver::UpdatePresentDevices()
|
||||
{
|
||||
bool loggedIn;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
loggedIn = m_loggedIn;
|
||||
}
|
||||
|
||||
if (!loggedIn)
|
||||
if (!Login())
|
||||
return;
|
||||
|
||||
std::stringstream url;
|
||||
url << "https://" << m_hostname << ":" << m_port << "/api/s/default/stat/sta";
|
||||
|
||||
std::time_t timeStamp = std::time(nullptr);
|
||||
std::vector<std::string> presentDevices;
|
||||
std::vector<std::string> addedDevices;
|
||||
std::vector<std::string> removedDevices;
|
||||
|
||||
try
|
||||
{
|
||||
std::stringstream output;
|
||||
Http::HttpRequest request(url.str());
|
||||
request.CookieFile(m_cookieFile);
|
||||
output << m_httpClient.Open(request);
|
||||
|
||||
nlohmann::json json = nlohmann::json::parse(output);
|
||||
|
||||
if (json["meta"]["rc"] != "ok")
|
||||
{
|
||||
std::stringstream error;
|
||||
error << "Query Failed";
|
||||
throw std::runtime_error(error.str());
|
||||
}
|
||||
|
||||
for (auto& device : json["data"])
|
||||
{
|
||||
std::string macAddress = device["mac"];
|
||||
std::transform(macAddress.begin(), macAddress.end(), macAddress.begin(), ::tolower);
|
||||
int lastSeen = device["last_seen"];
|
||||
|
||||
if (std::find(m_devices.begin(), m_devices.end(), macAddress) != m_devices.end())
|
||||
{
|
||||
if ((timeStamp - lastSeen) < m_timeout)
|
||||
{
|
||||
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
|
||||
{
|
||||
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);
|
||||
}
|
||||
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::Info, ss.str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
|
||||
{
|
||||
if ((timeStamp - lastSeen) < m_timeout)
|
||||
{
|
||||
if (it->HasWifiMac() && it->WifiMac() == macAddress)
|
||||
{
|
||||
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), macAddress) == m_presentDevices.end())
|
||||
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::Info, ss.str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WiFi::Driver::IsDevicePresent() - Error: " << e.what() << std::endl;
|
||||
Logging::Log(Logging::Severity::Error, ss.str());
|
||||
Logout();
|
||||
return;
|
||||
}
|
||||
|
||||
for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
|
||||
if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
|
||||
removedDevices.push_back(*it);
|
||||
|
||||
for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
|
||||
SendStateChange(true, *it);
|
||||
|
||||
for (std::vector<std::string>::iterator it = removedDevices.begin(); it != removedDevices.end(); ++it)
|
||||
SendStateChange(false, *it);
|
||||
|
||||
m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
|
||||
}
|
||||
|
||||
void Driver::SendStateChange(bool present, const std::string& macAddress)
|
||||
{
|
||||
char sign;
|
||||
if (present)
|
||||
sign = '+';
|
||||
else
|
||||
sign = '-';
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "WiFi: " << sign << " " << macAddress;
|
||||
Logging::Log(Logging::Severity::Info, ss.str());
|
||||
|
||||
if (!m_target.empty())
|
||||
{
|
||||
std::stringstream url;
|
||||
url << m_target << "/WiFi/" << sign << "/" << macAddress;
|
||||
|
||||
try
|
||||
{
|
||||
Http::HttpRequest request(url.str());
|
||||
request.ReturnType(Http::HttpRequest::ReturnType::None);
|
||||
m_httpClient.Open(request);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WiFi::Driver::SendStateChange() - Error: " << e.what() << std::endl;
|
||||
Logging::Log(Logging::Severity::Error, ss.str());
|
||||
}
|
||||
}
|
||||
|
||||
for (std::vector<Util::StaticDevice>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
|
||||
{
|
||||
if (it->HasWifiMac() && it->WifiMac() == macAddress)
|
||||
{
|
||||
it->SetWifiState(present);
|
||||
|
||||
std::vector<std::string> urls;
|
||||
|
||||
if (present)
|
||||
{
|
||||
if (!it->GetBluetoothState() && it->HasOnlineURL())
|
||||
urls.push_back(it->OnlineURL());
|
||||
if (it->HasWifiOnlineURL())
|
||||
urls.push_back(it->WifiOnlineURL());
|
||||
}
|
||||
else if (!present)
|
||||
{
|
||||
if (!it->GetBluetoothState() && it->HasOfflineURL())
|
||||
urls.push_back(it->OfflineURL());
|
||||
if (it->HasWifiOfflineURL())
|
||||
urls.push_back(it->WifiOfflineURL());
|
||||
}
|
||||
|
||||
for (auto& url : urls)
|
||||
{
|
||||
try
|
||||
{
|
||||
Http::HttpRequest request(url);
|
||||
request.ReturnType(Http::HttpRequest::ReturnType::None);
|
||||
m_httpClient.Open(request);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WiFi::Driver::SendStateChange() - Error: " << e.what() << std::endl;
|
||||
Logging::Log(Logging::Severity::Error, ss.str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace WiFi
|
||||
} // namespace PresenceDetection
|
||||
@@ -0,0 +1,61 @@
|
||||
#ifndef WIFI_DRIVER_H
|
||||
#define WIFI_DRIVER_H
|
||||
|
||||
#include "Util/StaticDevice.h"
|
||||
#include <HttpClient.h>
|
||||
#include <Timer.h>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace PresenceDetection {
|
||||
namespace WiFi {
|
||||
|
||||
class Driver
|
||||
{
|
||||
public:
|
||||
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);
|
||||
~Driver();
|
||||
|
||||
void Start();
|
||||
void Stop();
|
||||
void Wait();
|
||||
|
||||
private:
|
||||
bool Login();
|
||||
void Logout();
|
||||
|
||||
void ClearDevices();
|
||||
void UpdateDevicesFromInventory();
|
||||
void UpdatePresentDevices();
|
||||
void SendStateChange(bool present, const std::string& macAddress);
|
||||
|
||||
private:
|
||||
Timer::Timer m_deviceTimer;
|
||||
Timer::Timer m_inventoryTimer;
|
||||
Http::HttpClient m_httpClient;
|
||||
|
||||
std::mutex m_mutex;
|
||||
bool m_loggedIn;
|
||||
std::string m_hostname;
|
||||
int m_port;
|
||||
std::string m_username;
|
||||
std::string m_password;
|
||||
std::string m_cookieFile;
|
||||
|
||||
std::vector<std::string> m_devices;
|
||||
|
||||
int m_timeout;
|
||||
int m_checkInterval;
|
||||
std::string m_inventoryURL;
|
||||
std::string m_target;
|
||||
std::vector<Util::StaticDevice> m_staticDevices;
|
||||
|
||||
std::vector<std::string> m_presentDevices;
|
||||
};
|
||||
|
||||
} // namespace WiFi
|
||||
} // namespace PresenceDetection
|
||||
|
||||
#endif // WIFI_DRIVER_H
|
||||
@@ -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
|
||||
@@ -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
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../Makefile
|
||||
Reference in New Issue
Block a user