#ifndef CMVR_ES_TASK_MANAGER_H #define CMVR_ES_TASK_MANAGER_H #include #include #include #include #include #include #include #include #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> activitySnapshotIfInitialized(); static bool stopAllActivitiesIfInitialized( std::vector* 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 getTask(const std::string& task_id) const; std::shared_ptr 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* failures = nullptr); std::vector> 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 instance_; config::TaskManagerConfig cfg_; std::unordered_map> tasks_; std::unordered_map task_period_s_; std::unordered_map next_step_time_; mutable std::mutex tasks_mutex_; std::mutex lifecycle_mutex_; std::atomic running_{false}; std::thread run_thread_; bool initialized_{false}; }; } // namespace cmvr::task #endif // CMVR_ES_TASK_MANAGER_H