Add Static Devices
This commit is contained in:
@@ -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 <clipp.h>
|
||||
#include <Logging.h>
|
||||
@@ -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<PresenceDetection::Util::Device> devices;
|
||||
std::vector<PresenceDetection::Util::StaticDevice> 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<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;
|
||||
@@ -153,7 +158,7 @@ int main(int argc, char** argv)
|
||||
|
||||
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;
|
||||
@@ -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();
|
||||
|
||||
@@ -14,85 +14,6 @@ void print()
|
||||
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()
|
||||
{
|
||||
PresenceDetection::Util::INIReader reader("PresenceDetection.ini");
|
||||
|
||||
Reference in New Issue
Block a user