| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include <iostream>
- #include <string>
- #include <memory>
- #include <algorithm>
- #include <unistd.h>
- #include <chrono>
- #include <deque>
- #include "Util/Timer.h"
- #include "Util/INIh.h"
- void print()
- {
- std::cout << "Print" << std::endl;
- }
- void TestTimerAbort()
- {
- PresenceDetection::Util::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<PresenceDetection::Util::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<PresenceDetection::Util::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;
- }
|