#ifndef TASK_MANAGER_HPP #define TASK_MANAGER_HPP #include #include #include #include #include #include #include #include "easylogging++.h" using callable = std::function; enum TaskAction { TASK_ACTION_LOW = 0, TASK_ACTION_HIGH = 1, TASK_ACTION_NONE }; class Task { private: cron::cronexpr cronexpr; TaskAction action; callable action_fn; int pin; public: Task(cron::cronexpr cronexpr, std::string command, callable action_fn); Task(Task &task); void doAction(); std::time_t next(); friend std::ostream& operator<< (std::ostream &out, const Task &task); }; class TaskManager { private: std::map tasks; public: TaskManager(std::istream &stream, callable action_fn); int doWork(); }; inline TaskManager::TaskManager(std::istream &stream, callable action_fn) { std::string line; while (std::getline(stream, line).good()) { auto fields = cron::utils::split(line, ' '); fields.erase( std::remove_if(std::begin(fields), std::end(fields), [](CRONCPP_STRING_VIEW s) {return s.empty(); }), std::end(fields)); if (fields.size() != 7) throw cron::bad_cronexpr("cron expression must have 7 fields"); std::stringstream schedule; for (int i = 0; i < 6; ++i) { schedule << fields[i] << ' '; } auto cron = cron::make_cron(schedule.str()); std::time_t now = std::time(0); std::time_t next = cron::cron_next(cron, now); tasks.emplace(std::piecewise_construct, std::forward_as_tuple(next), std::forward_as_tuple(cron, fields[6], action_fn)); } LOG(INFO) << "Schedule table:"; for (const auto& [key, value] : tasks) { LOG(INFO) << key << ' ' << value; } } inline int TaskManager::doWork() { auto it = tasks.begin(); if(it == tasks.end()) return 0; std::time_t now = std::time(0); std::time_t stop = it->first; LOG(DEBUG) << "Will sleep till next action " << stop - now << 's'; std::this_thread::sleep_for(std::chrono::seconds{stop - now}); it->second.doAction(); tasks.emplace(std::piecewise_construct, std::forward_as_tuple(it->second.next()), std::forward_as_tuple(it->second)); tasks.erase(it); return 0; } inline std::ostream& operator<< (std::ostream &out, const Task &task) { out << "Action: "; switch (task.action) { case TASK_ACTION_HIGH: out << "HIGH"; break; case TASK_ACTION_LOW: out << "LOW"; break; default: break; } out << " Pin:" << task.pin; return out; } inline Task::Task(cron::cronexpr cronexpr, std::string command, callable action_fn): cronexpr{cronexpr}, action{TASK_ACTION_NONE}, pin{0}, action_fn{action_fn} { if(command.rfind("HIGH", 0) == 0) { action = TASK_ACTION_HIGH; } else if((command.rfind("LOW", 0) == 0)) { action = TASK_ACTION_LOW; } auto start = command.find('('); auto stop = command.find(')'); if(start == std::string::npos && stop == std::string::npos) { throw cron::bad_cronexpr("pin is not set in command"); } LOG(DEBUG) << "pin to parse " << command.substr(start + 1, stop - start - 1); pin = std::stoi(command.substr(start + 1, stop - start - 1)); } inline Task::Task(Task &task): cronexpr{task.cronexpr}, action{task.action}, action_fn{task.action_fn}, pin{task.pin} {} inline void Task::doAction() { if(action == TASK_ACTION_NONE) return; action_fn(pin, action); } inline std::time_t Task::next() { std::time_t now = std::time(0); return cron::cron_next(cronexpr, now); } #endif //TASK_MANAGER_HPP