feat: task manager
This commit is contained in:
94
README.md
94
README.md
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user