Advertisement
Guest User

notify_time.cpp

a guest
Apr 16th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <thread>
  6. #include <chrono>
  7.  
  8. #include "notify_time.h"
  9. #include "event.h"
  10.  
  11. void load_conf(std::vector<event_t>& events, const std::string& path)
  12. {
  13.     std::ifstream times(path);
  14.     std::string line;
  15.     while (std::getline(times, line))
  16.     {
  17.         std::istringstream stream(line);
  18.         std::vector<std::string> event;
  19.         std::string str;
  20.         while (stream >> str)
  21.             event.push_back(str);
  22.         events.push_back(event);
  23.     }
  24.     times.close();
  25. }
  26.  
  27. void event_loop(const std::vector<event_t>& events, const bool loop)
  28. {
  29.     do
  30.     {
  31.         std::string weekday = current_weekday();
  32.         const unsigned daily_time = current_time();
  33.  
  34.         for (const event_t& event : events)
  35.         {
  36.             if (event.day() == weekday)
  37.             {
  38.                 if (loop)
  39.                 {
  40.                     if (daily_time == event.time(time_constant::warn_time))
  41.                         send_notif(event, time_constant::warn_notif);
  42.                     else if (daily_time == event.time(time_constant::start_time))
  43.                         send_notif(event, time_constant::start_notif);
  44.                     else if (daily_time == event.time(time_constant::end_time))
  45.                         send_notif(event, time_constant::end_notif);
  46.                 }
  47.                 else
  48.                 {
  49.                     if (daily_time >= event.time(time_constant::warn_time) && daily_time < event.time(time_constant::start_time))
  50.                         send_notif(event, time_constant::about_to_start_notif);
  51.                     else if (daily_time >= event.time(time_constant::start_time) && daily_time < event.time(time_constant::end_time))
  52.                         send_notif(event, time_constant::in_progress_notif);
  53.                 }
  54.             }
  55.         }
  56.         pause(1000);
  57.     }
  58.     while (loop);
  59. }
  60.  
  61. const unsigned current_time()
  62. {
  63.     std::time_t t = std::time(NULL);
  64.     struct tm *now = std::localtime(&t);
  65.     return now->tm_hour * 3600 + now->tm_min * 60 + now->tm_sec;
  66. }
  67.  
  68. const std::string current_weekday()
  69. {
  70.     std::time_t t = std::time(NULL);
  71.     struct tm *now = std::localtime(&t);
  72.     return time_constant::days[now->tm_wday];
  73. }
  74.  
  75. void pause(const unsigned milli)
  76. {
  77.     std::this_thread::sleep_for(std::chrono::milliseconds(milli));
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement