cmvr-es/cmvr-es/manager/task_manager/include/task_manager.h

72 lines
2.5 KiB
C++

#ifndef CMVR_ES_TASK_MANAGER_H
#define CMVR_ES_TASK_MANAGER_H
#include <memory>
#include <mutex>
#include <string>
#include <chrono>
#include <thread>
#include <unordered_map>
#include <atomic>
#include <vector>
#include "task/task.h"
#include "task/touch_screen_task/include/touch_screen_task.h"
#include "cmvr/config/task_manager_config/task_manager_config.pb.h"
namespace cmvr::task {
class TaskManager {
public:
TaskManager(const TaskManager&) = delete;
TaskManager& operator=(const TaskManager&) = delete;
static TaskManager& getInstance(const config::TaskManagerConfig& cfg);
static TaskManager& getInstance();
static void destroyInstance();
static std::vector<std::shared_ptr<Task>>
activitySnapshotIfInitialized();
static bool stopAllActivitiesIfInitialized(
std::vector<std::string>* failures = nullptr);
~TaskManager();
bool startRunTask(double control_period_s = 0.001);
void stopRunTask();
bool running() const { return running_.load(); }
bool initialized() const noexcept { return initialized_; }
std::shared_ptr<Task> getTask(const std::string& task_id) const;
std::shared_ptr<TouchScreenTask> getTouchScreenTask(const std::string& task_id = "touch_screen") const;
// Stops command-driven operational activity without stopping the
// scheduler or destroying task/device lifecycle state.
bool stopAllActivities(std::vector<std::string>* failures = nullptr);
std::vector<std::shared_ptr<Task>> activitySnapshot() const;
private:
explicit TaskManager(const config::TaskManagerConfig& cfg);
void logTaskPlan() const;
bool initTasks();
void runTaskLoop(double control_period_s);
static TaskRunMode toTaskRunMode(config::TaskConfigEntry::TaskRunMode run_mode);
static std::mutex init_mutex_;
static std::shared_ptr<TaskManager> instance_;
config::TaskManagerConfig cfg_;
std::unordered_map<std::string, std::shared_ptr<Task>> tasks_;
std::unordered_map<std::string, double> task_period_s_;
std::unordered_map<std::string, std::chrono::steady_clock::time_point> next_step_time_;
mutable std::mutex tasks_mutex_;
std::mutex lifecycle_mutex_;
std::atomic<bool> running_{false};
std::thread run_thread_;
bool initialized_{false};
};
} // namespace cmvr::task
#endif // CMVR_ES_TASK_MANAGER_H