feat: bind tasks instead of passing fn ptrs when registering a task

This commit is contained in:
2025-08-21 22:24:46 +02:00
parent f80b594d21
commit 0d51816bd3
5 changed files with 307 additions and 51 deletions

View File

@@ -2,6 +2,9 @@
#include <Arduino.h>
#include <TaskScheduler.h>
// Define static members
std::map<std::string, std::function<void()>> TaskManager::callbackRegistry;
TaskManager::TaskManager(NodeContext& ctx) : ctx(ctx) {}
TaskManager::~TaskManager() {
@@ -12,7 +15,7 @@ TaskManager::~TaskManager() {
tasks.clear();
}
void TaskManager::registerTask(const std::string& name, unsigned long interval, TaskCallback callback, bool enabled, bool autoStart) {
void TaskManager::registerTask(const std::string& name, unsigned long interval, TaskFunction callback, bool enabled, bool autoStart) {
TaskDefinition taskDef(name, interval, callback, enabled, autoStart);
registerTask(taskDef);
}
@@ -39,8 +42,11 @@ void TaskManager::initialize() {
}
void TaskManager::createTask(const TaskDefinition& taskDef) {
// Create new task
Task* task = new Task(0, TASK_FOREVER, taskDef.callback);
// Store the callback in the static registry
callbackRegistry[taskDef.name] = taskDef.callback;
// Create a dummy task - we'll handle execution ourselves
Task* task = new Task(0, TASK_FOREVER, []() { /* Dummy callback */ });
task->setInterval(taskDef.interval);
// Add to scheduler
@@ -149,7 +155,33 @@ void TaskManager::printTaskStatus() const {
}
void TaskManager::execute() {
ctx.scheduler->execute();
// Execute all enabled tasks by calling their stored callbacks
static unsigned long lastExecutionTimes[100] = {0}; // Simple array for timing
static int taskCount = 0;
if (taskCount == 0) {
taskCount = tasks.size();
}
unsigned long currentTime = millis();
for (size_t i = 0; i < tasks.size() && i < taskDefinitions.size(); ++i) {
Task* task = tasks[i];
const std::string& taskName = taskDefinitions[i].name;
if (task->isEnabled()) {
// Check if it's time to run this task
if (currentTime - lastExecutionTimes[i] >= task->getInterval()) {
// Execute the stored callback
if (callbackRegistry.find(taskName) != callbackRegistry.end()) {
callbackRegistry[taskName]();
}
// Update the last execution time
lastExecutionTimes[i] = currentTime;
}
}
}
}
Task* TaskManager::findTask(const std::string& name) const {

View File

@@ -1,4 +1,5 @@
#include <Arduino.h>
#include <functional>
#include "Globals.h"
#include "NodeContext.h"
#include "NetworkManager.h"
@@ -6,31 +7,34 @@
#include "ApiServer.h"
#include "TaskManager.h"
using namespace std;
NodeContext ctx;
NetworkManager network(ctx);
ClusterManager cluster(ctx);
TaskManager taskManager(ctx);
ApiServer apiServer(ctx, taskManager, ctx.config.api_server_port);
// Task callback wrapper functions
void discoverySendTask() { cluster.sendDiscovery(); }
void discoveryListenTask() { cluster.listenForDiscovery(); }
void statusUpdateTask() { cluster.updateAllNodeStatuses(); cluster.removeDeadNodes(); }
void printMembersTask() { cluster.printMemberList(); }
void heartbeatTask() { cluster.heartbeatTaskCallback(); }
void updateMembersInfoTask() { cluster.updateAllMembersInfoTaskCallback(); }
void setup() {
// Setup WiFi first
network.setupWiFi();
// Register all system tasks
taskManager.registerTask("discovery_send", ctx.config.discovery_interval_ms, discoverySendTask);
taskManager.registerTask("discovery_listen", ctx.config.discovery_interval_ms / 10, discoveryListenTask);
taskManager.registerTask("status_update", ctx.config.status_update_interval_ms, statusUpdateTask);
taskManager.registerTask("print_members", ctx.config.print_interval_ms, printMembersTask);
taskManager.registerTask("heartbeat", ctx.config.heartbeat_interval_ms, heartbeatTask);
taskManager.registerTask("update_members_info", ctx.config.member_info_update_interval_ms, updateMembersInfoTask);
// Register all system tasks using std::bind for member functions
taskManager.registerTask("discovery_send", ctx.config.discovery_interval_ms,
std::bind(&ClusterManager::sendDiscovery, &cluster));
taskManager.registerTask("discovery_listen", ctx.config.discovery_interval_ms / 10,
std::bind(&ClusterManager::listenForDiscovery, &cluster));
taskManager.registerTask("status_update", ctx.config.status_update_interval_ms,
std::bind([](ClusterManager* cm) {
cm->updateAllNodeStatuses();
cm->removeDeadNodes();
}, &cluster));
taskManager.registerTask("print_members", ctx.config.print_interval_ms,
std::bind(&ClusterManager::printMemberList, &cluster));
taskManager.registerTask("heartbeat", ctx.config.heartbeat_interval_ms,
std::bind(&ClusterManager::heartbeatTaskCallback, &cluster));
taskManager.registerTask("update_members_info", ctx.config.member_info_update_interval_ms,
std::bind(&ClusterManager::updateAllMembersInfoTaskCallback, &cluster));
// Initialize and start all tasks
taskManager.initialize();