feat: task manager

This commit is contained in:
2025-08-21 21:52:25 +02:00
parent 953768b681
commit f80b594d21
8 changed files with 495 additions and 35 deletions

View File

@@ -228,6 +228,100 @@ The system includes automatic WiFi fallback:
2. If connection fails, creates an access point
3. Hostname is automatically generated from MAC address
## Task Management
The SPORE system includes a comprehensive TaskManager that provides a clean interface for managing system tasks. This makes it easy to add, configure, and control background tasks without cluttering the main application code.
### TaskManager Features
- **Easy Task Registration**: Simple API for adding new tasks with configurable intervals
- **Dynamic Control**: Enable/disable tasks at runtime
- **Interval Management**: Change task execution frequency on the fly
- **Status Monitoring**: View task status and configuration
- **Automatic Lifecycle**: Tasks are automatically managed and executed
### Basic Usage
```cpp
#include "TaskManager.h"
// Create task manager
TaskManager taskManager(ctx);
// Register tasks
taskManager.registerTask("heartbeat", 2000, heartbeatFunction);
taskManager.registerTask("maintenance", 30000, maintenanceFunction);
// Initialize and start all tasks
taskManager.initialize();
```
### Task Control API
```cpp
// Enable/disable tasks
taskManager.enableTask("heartbeat");
taskManager.disableTask("maintenance");
// Change intervals
taskManager.setTaskInterval("heartbeat", 5000); // 5 seconds
// Check status
bool isRunning = taskManager.isTaskEnabled("heartbeat");
unsigned long interval = taskManager.getTaskInterval("heartbeat");
// Print all task statuses
taskManager.printTaskStatus();
```
### Remote Task Management
The TaskManager integrates with the API server to provide remote task control:
```bash
# Get task status
curl http://192.168.1.100/api/tasks/status
# Control tasks
curl -X POST http://192.168.1.100/api/tasks/control \
-d "task=heartbeat&action=disable"
# Available actions: enable, disable, start, stop
```
### Adding Custom Tasks
To add custom tasks to your SPORE system:
1. **Define your task function**:
```cpp
void myCustomTask() {
// Your task logic here
Serial.println("Custom task executed");
}
```
2. **Register with TaskManager**:
```cpp
taskManager.registerTask("my_task", 10000, myCustomTask);
```
3. **The TaskManager handles the rest** - automatic execution, lifecycle management, and monitoring.
### Task Configuration Options
When registering tasks, you can specify:
- **Name**: Unique identifier for the task
- **Interval**: Execution frequency in milliseconds
- **Enabled**: Whether the task starts enabled (default: true)
- **AutoStart**: Whether to start automatically (default: true)
```cpp
taskManager.registerTask("delayed_task", 5000, taskFunction, true, false);
// Creates a task that's enabled but won't auto-start
```
## Current Limitations
- WiFi credentials are hardcoded in `Config.cpp` (should be configurable)