TaskScheduler

system
system/TaskScheduler.h
system

Méthodes publiques

Méthode Description Paramètres Retour
getInstance()
getInstance();
begin()
begin();
addTask()
addTask(const String& name, TaskCallback callback, // uint32_t interval = settings::scheduler::DEFAULT_INTERVAL, // TaskPriority priority = TaskPriority::NORMAL);
addOneShotTask()
addOneShotTask(const String& name, TaskCallback callback, // uint32_t delay = 0, // TaskPriority priority = TaskPriority::NORMAL);
addDelayedTask()
addDelayedTask(const String& name, TaskCallback callback, // uint32_t delay, uint32_t interval = 0, // TaskPriority priority = TaskPriority::NORMAL);
startTask()
startTask(uint8_t id);
stopTask()
stopTask(uint8_t id);
pauseTask()
pauseTask(uint8_t id);
resumeTask()
resumeTask(uint8_t id);
removeTask()
removeTask(uint8_t id);
getTask()
getTask(uint8_t id);
getTaskByName()
getTaskByName(const String& name);
update()
update();
startAll()
startAll();
stopAll()
stopAll();
pauseAll()
pauseAll();
resumeAll()
resumeAll();
removeAll()
removeAll();
getStats()
getStats() const;
getStatsJson()
getStatsJson() const;
getTaskCount()
getTaskCount() const {
size()
size();
getMaxTasks()
getMaxTasks() const {
isFull()
isFull() const {
printTaskList()
printTaskList();
printTaskDetails()
printTaskDetails(uint8_t id);
setTaskErrorCallback()
setTaskErrorCallback(uint8_t id, TaskErrorCallback callback);
saveTasks()
saveTasks();
loadTasks()
loadTasks();
validateTask()
validateTask(uint8_t id) const;
checkForDeadTasks()
checkForDeadTasks();

Méthodes privées


            TaskScheduler();;
sortTasksByPriority();;

                    

Variables membres


            uint8_t id;
String name;
TaskCallback callback;
TaskErrorCallback errorCallback;
TaskType type;
TaskPriority priority;
TaskState state;
uint32_t interval;
uint32_t delay;
uint32_t lastRun;
uint32_t nextRun;
uint32_t runCount;
uint32_t errorCount;
uint32_t maxRuns;
bool enabled;
bool autoDelete;
bool persistent;
uint32_t totalRuntime;
uint32_t maxRuntime;
uint32_t minRuntime;
uint8_t nextId;
uint32_t lastUpdate;
uint32_t totalRuntime;
uint32_t taskCount;
uint32_t errorCount;
bool initialized;
uint32_t totalTasks;
uint32_t runningTasks;
uint32_t pausedTasks;
uint32_t stoppedTasks;
uint32_t totalErrors;
uint32_t totalRuntime;
float cpuUsage;

                    

Code source


        // // utils/TaskScheduler.h
// #pragma once

// #include <Arduino.h>
// #include <functional>
// #include <vector>
// #include "Settings.h"

// namespace crepp::system {

// // Types de tâches
// enum class TaskType : uint8_t {
//     PERIODIC = 0,     // Tâche périodique
//     ONE_SHOT = 1,     // Tâche unique
//     DELAYED = 2,      // Tâche différée
//     CRITICAL = 3      // Tâche critique (toujours exécutée)
// };

// // Priorités des tâches
// enum class TaskPriority : uint8_t {
//     LOW = 0,
//     NORMAL = 1,
//     HIGH = 2,
//     CRITICAL = 3
// };

// // État d'une tâche
// enum class TaskState : uint8_t {
//     CREATED = 0,
//     SCHEDULED = 1,
//     RUNNING = 2,
//     PAUSED = 3,
//     STOPPED = 4,
//     ERROR = 5
// };

// // Callback type
// using TaskCallback = std::function<void()>;
// using TaskErrorCallback = std::function<void(int)>;

// // Structure d'une tâche
// struct Task {
//     uint8_t id;
//     String name;
//     TaskCallback callback;
//     TaskErrorCallback errorCallback;
    
//     TaskType type;
//     TaskPriority priority;
//     TaskState state;
    
//     uint32_t interval;           // Intervalle pour les tâches périodiques
//     uint32_t delay;              // Délai avant exécution
//     uint32_t lastRun;            // Dernière exécution
//     uint32_t nextRun;            // Prochaine exécution programmée
    
//     uint32_t runCount;           // Nombre d'exécutions
//     uint32_t errorCount;         // Nombre d'erreurs
//     uint32_t maxRuns;            // Nombre max d'exécutions (0 = infini)
    
//     bool enabled;                // Tâche activée
//     bool autoDelete;             // Auto-suppression après exécution
//     bool persistent;             // Persiste après redémarrage
    
//     // Statistiques
//     uint32_t totalRuntime;
//     uint32_t maxRuntime;
//     uint32_t minRuntime;
    
//     // Constructeur
//     Task(uint8_t id, const String& name, TaskCallback cb, 
//          uint32_t interval = 1000, TaskType type = TaskType::PERIODIC,
//          TaskPriority priority = TaskPriority::NORMAL);
    
//     // Vérifie si la tâche doit être exécutée
//     bool shouldRun(uint32_t currentTime) const;
    
//     // Exécute la tâche
//     void execute();
    
//     // Réinitialise la tâche
//     void reset();
    
//     // Formate les informations de la tâche en JSON
//     String toJson() const;
// };

// class TaskScheduler {
// private:
//     static TaskScheduler* instance;
    
//     std::vector<Task*> tasks;
//     uint8_t nextId;
    
//     uint32_t lastUpdate;
//     uint32_t totalRuntime;
//     uint32_t taskCount;
//     uint32_t errorCount;
    
//     bool initialized;
    
//     // Constructeur privé (singleton)
//     TaskScheduler();
    
//     // Trie les tâches par priorité
//     void sortTasksByPriority();
    
// public:
//     // Singleton
//     static TaskScheduler& getInstance();
    
//     // Initialisation
//     bool begin();
    
//     // Gestion des tâches
//     uint8_t addTask(const String& name, TaskCallback callback, 
//                     uint32_t interval = settings::scheduler::DEFAULT_INTERVAL,
//                     TaskPriority priority = TaskPriority::NORMAL);
    
//     uint8_t addOneShotTask(const String& name, TaskCallback callback,
//                           uint32_t delay = 0,
//                           TaskPriority priority = TaskPriority::NORMAL);
    
//     uint8_t addDelayedTask(const String& name, TaskCallback callback,
//                           uint32_t delay, uint32_t interval = 0,
//                           TaskPriority priority = TaskPriority::NORMAL);
    
//     // Contrôle des tâches
//     bool startTask(uint8_t id);
//     bool stopTask(uint8_t id);
//     bool pauseTask(uint8_t id);
//     bool resumeTask(uint8_t id);
//     bool removeTask(uint8_t id);
    
//     // Recherche de tâches
//     Task* getTask(uint8_t id);
//     Task* getTaskByName(const String& name);
    
//     // Mise à jour du scheduler (à appeler dans loop())
//     void update();
    
//     // Gestion globale
//     void startAll();
//     void stopAll();
//     void pauseAll();
//     void resumeAll();
//     void removeAll();
    
//     // Statistiques
//     struct Stats {
//         uint32_t totalTasks;
//         uint32_t runningTasks;
//         uint32_t pausedTasks;
//         uint32_t stoppedTasks;
//         uint32_t totalErrors;
//         uint32_t totalRuntime;
//         float cpuUsage; // Estimation de l'utilisation CPU
//     };
    
//     Stats getStats() const;
//     String getStatsJson() const;
    
//     // Informations
//     uint8_t getTaskCount() const { return tasks.size(); }
//     uint8_t getMaxTasks() const { return settings::scheduler::MAX_TASKS; }
//     bool isFull() const { return tasks.size() >= settings::scheduler::MAX_TASKS; }
    
//     // Debug
//     void printTaskList();
//     void printTaskDetails(uint8_t id);
    
//     // Callbacks d'erreur
//     void setTaskErrorCallback(uint8_t id, TaskErrorCallback callback);
    
//     // Persistance
//     bool saveTasks();
//     bool loadTasks();
    
//     // Validation
//     bool validateTask(uint8_t id) const;
//     bool checkForDeadTasks();
    
// private:
//     // Exécution des tâches
//     void executeReadyTasks();
//     void handleTaskError(Task& task, int errorCode);
    
//     // Utilitaires
//     uint32_t calculateNextRunTime(const Task& task, uint32_t currentTime) const;
//     uint8_t generateId();
    
//     // Nettoyage
//     void cleanupFinishedTasks();
// };

// } // namespace crepp::system