124 lines
3.3 KiB
C++
124 lines
3.3 KiB
C++
#include "Util/INIh.h"
|
|
#include <Timer.h>
|
|
#include <unistd.h>
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <deque>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
|
|
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");
|
|
|
|
std::string target = reader.Get("PresenceDetection", "Target", "");
|
|
if (!target.empty())
|
|
std::cout << "Error" << std::endl;
|
|
std::cout << target.size() << " " << target << std::endl;
|
|
|
|
bool unifi = reader.GetBoolean("PresenceDetection", "UniFi", false);
|
|
if (unifi)
|
|
std::cout << "UniFi On" << std::endl;
|
|
else
|
|
std::cout << "UniFi Off" << std::endl;
|
|
|
|
bool test = reader.GetBoolean("PresenceDetection", "Test", false);
|
|
if (test)
|
|
std::cout << "Test On" << std::endl;
|
|
else
|
|
std::cout << "Test Off" << std::endl;
|
|
}
|
|
|
|
int main(int /*argc*/, char** /*argv[]*/)
|
|
{
|
|
TestINIReader();
|
|
|
|
return 0;
|
|
}
|