refactor: harmonize method names
This commit is contained in:
@@ -93,12 +93,12 @@ void setup() {
|
||||
spore.setup();
|
||||
|
||||
// Create and register custom services
|
||||
RelayService* relayService = new RelayService(spore.getTaskManager(), 2);
|
||||
spore.addService(relayService);
|
||||
RelayService* relayService = new RelayService(spore.getContext(), spore.getTaskManager(), 2);
|
||||
spore.registerService(relayService);
|
||||
|
||||
// Or using smart pointers
|
||||
auto sensorService = std::make_shared<SensorService>();
|
||||
spore.addService(sensorService);
|
||||
auto sensorService = std::make_shared<SensorService>(spore.getContext(), spore.getTaskManager());
|
||||
spore.registerService(sensorService);
|
||||
|
||||
// Start the API server and complete initialization
|
||||
spore.begin();
|
||||
|
||||
@@ -21,11 +21,13 @@ The system architecture consists of several key components working together:
|
||||
|
||||
### API Server
|
||||
- **HTTP API Server**: RESTful API for cluster management
|
||||
- **Dynamic Endpoint Registration**: Automatic API endpoint discovery
|
||||
- **Dynamic Endpoint Registration**: Services register endpoints via `registerEndpoints(ApiServer&)`
|
||||
- **Service Registry**: Track available services across the cluster
|
||||
- **Service Lifecycle**: Services register both endpoints and tasks through unified interface
|
||||
|
||||
### Task Scheduler
|
||||
- **Cooperative Multitasking**: Background task management system (`TaskManager`)
|
||||
- **Service Task Registration**: Services register tasks via `registerTasks(TaskManager&)`
|
||||
- **Task Lifecycle Management**: Enable/disable tasks and set intervals at runtime
|
||||
- **Execution Model**: Tasks run in `Spore::loop()` when their interval elapses
|
||||
|
||||
|
||||
@@ -29,13 +29,13 @@ spore/
|
||||
│ │ ├── NetworkManager.cpp # WiFi and network handling
|
||||
│ │ ├── TaskManager.cpp # Background task management
|
||||
│ │ └── NodeContext.cpp # Central context and events
|
||||
│ ├── services/ # Built-in services
|
||||
│ │ ├── NodeService.cpp
|
||||
│ │ ├── NetworkService.cpp
|
||||
│ │ ├── ClusterService.cpp
|
||||
│ │ ├── TaskService.cpp
|
||||
│ │ ├── StaticFileService.cpp
|
||||
│ │ └── MonitoringService.cpp
|
||||
│ ├── services/ # Built-in services (implement Service interface)
|
||||
│ │ ├── NodeService.cpp # registerEndpoints() + registerTasks()
|
||||
│ │ ├── NetworkService.cpp # registerEndpoints() + registerTasks()
|
||||
│ │ ├── ClusterService.cpp # registerEndpoints() + registerTasks()
|
||||
│ │ ├── TaskService.cpp # registerEndpoints() + registerTasks()
|
||||
│ │ ├── StaticFileService.cpp # registerEndpoints() + registerTasks()
|
||||
│ │ └── MonitoringService.cpp # registerEndpoints() + registerTasks()
|
||||
│ └── types/ # Shared types
|
||||
├── include/ # Header files
|
||||
├── examples/ # Example apps per env (base, relay, neopattern)
|
||||
|
||||
@@ -11,6 +11,31 @@ The TaskManager system provides:
|
||||
- **Status Monitoring**: View task status and configuration
|
||||
- **Automatic Lifecycle**: Tasks are automatically managed and executed
|
||||
|
||||
## Service Interface Integration
|
||||
|
||||
Services now implement a unified interface for both endpoint and task registration:
|
||||
|
||||
```cpp
|
||||
class MyService : public Service {
|
||||
public:
|
||||
void registerEndpoints(ApiServer& api) override {
|
||||
// Register HTTP endpoints
|
||||
api.registerEndpoint("/api/my/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatus(request); });
|
||||
}
|
||||
|
||||
void registerTasks(TaskManager& taskManager) override {
|
||||
// Register background tasks
|
||||
taskManager.registerTask("my_heartbeat", 2000,
|
||||
[this]() { sendHeartbeat(); });
|
||||
taskManager.registerTask("my_maintenance", 30000,
|
||||
[this]() { performMaintenance(); });
|
||||
}
|
||||
|
||||
const char* getName() const override { return "MyService"; }
|
||||
};
|
||||
```
|
||||
|
||||
## Basic Usage
|
||||
|
||||
```cpp
|
||||
@@ -27,6 +52,46 @@ taskManager.registerTask("maintenance", 30000, maintenanceFunction);
|
||||
taskManager.initialize();
|
||||
```
|
||||
|
||||
## Service Lifecycle
|
||||
|
||||
The Spore framework automatically manages service registration and task lifecycle:
|
||||
|
||||
### Service Registration Process
|
||||
|
||||
1. **Service Creation**: Services are created with required dependencies (NodeContext, TaskManager, etc.)
|
||||
2. **Service Registration**: Services are registered with the Spore framework via `spore.registerService()`
|
||||
3. **Endpoint Registration**: When `spore.begin()` is called, `registerEndpoints()` is called for each service
|
||||
4. **Task Registration**: Simultaneously, `registerTasks()` is called for each service
|
||||
5. **Task Initialization**: The TaskManager initializes all registered tasks
|
||||
6. **Execution**: Tasks run in the main loop when their intervals elapse
|
||||
|
||||
### Framework Integration
|
||||
|
||||
```cpp
|
||||
void setup() {
|
||||
spore.setup();
|
||||
|
||||
// Create service with dependencies
|
||||
MyService* service = new MyService(spore.getContext(), spore.getTaskManager());
|
||||
|
||||
// Register service (endpoints and tasks will be registered when begin() is called)
|
||||
spore.registerService(service);
|
||||
|
||||
// This triggers registerEndpoints() and registerTasks() for all services
|
||||
spore.begin();
|
||||
}
|
||||
```
|
||||
|
||||
### Dynamic Service Addition
|
||||
|
||||
Services can be added after the framework has started:
|
||||
|
||||
```cpp
|
||||
// Add service to running framework
|
||||
MyService* newService = new MyService(spore.getContext(), spore.getTaskManager());
|
||||
spore.registerService(newService); // Immediately registers endpoints and tasks
|
||||
```
|
||||
|
||||
## Task Registration Methods
|
||||
|
||||
### Using std::bind with Member Functions (Recommended)
|
||||
@@ -153,7 +218,62 @@ taskManager.registerTask("lambda_task", 2000,
|
||||
|
||||
## Adding Custom Tasks
|
||||
|
||||
### Method 1: Using std::bind (Recommended)
|
||||
### Method 1: Service Interface (Recommended)
|
||||
|
||||
1. **Create your service class implementing the Service interface**:
|
||||
```cpp
|
||||
class SensorService : public Service {
|
||||
public:
|
||||
SensorService(NodeContext& ctx, TaskManager& taskManager)
|
||||
: ctx(ctx), taskManager(taskManager) {}
|
||||
|
||||
void registerEndpoints(ApiServer& api) override {
|
||||
api.registerEndpoint("/api/sensor/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatus(request); });
|
||||
}
|
||||
|
||||
void registerTasks(TaskManager& taskManager) override {
|
||||
taskManager.registerTask("temp_read", 1000,
|
||||
[this]() { readTemperature(); });
|
||||
taskManager.registerTask("calibrate", 60000,
|
||||
[this]() { calibrateSensors(); });
|
||||
}
|
||||
|
||||
const char* getName() const override { return "SensorService"; }
|
||||
|
||||
private:
|
||||
NodeContext& ctx;
|
||||
TaskManager& taskManager;
|
||||
|
||||
void readTemperature() {
|
||||
// Read sensor logic
|
||||
Serial.println("Reading temperature");
|
||||
}
|
||||
|
||||
void calibrateSensors() {
|
||||
// Calibration logic
|
||||
Serial.println("Calibrating sensors");
|
||||
}
|
||||
|
||||
void handleStatus(AsyncWebServerRequest* request) {
|
||||
// Handle status request
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
2. **Register with Spore framework**:
|
||||
```cpp
|
||||
void setup() {
|
||||
spore.setup();
|
||||
|
||||
SensorService* sensorService = new SensorService(spore.getContext(), spore.getTaskManager());
|
||||
spore.registerService(sensorService);
|
||||
|
||||
spore.begin(); // This will call registerTasks() automatically
|
||||
}
|
||||
```
|
||||
|
||||
### Method 2: Direct TaskManager Registration
|
||||
|
||||
1. **Create your service class**:
|
||||
```cpp
|
||||
@@ -181,7 +301,7 @@ taskManager.registerTask("lambda_task", 2000,
|
||||
std::bind(&SensorService::calibrateSensors, &sensors));
|
||||
```
|
||||
|
||||
### Method 2: Traditional Functions
|
||||
### Method 3: Traditional Functions
|
||||
|
||||
1. **Define your task function**:
|
||||
```cpp
|
||||
@@ -308,13 +428,55 @@ curl -X POST http://192.168.1.100/api/tasks/control \
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Use std::bind for member functions**: Cleaner than wrapper functions
|
||||
2. **Group related tasks**: Register multiple related operations in a single task
|
||||
1. **Use Service Interface**: Implement the Service interface for clean integration with the framework
|
||||
2. **Group related tasks**: Register multiple related operations in a single service
|
||||
3. **Monitor task health**: Use the status API to monitor task performance
|
||||
4. **Plan intervals carefully**: Balance responsiveness with system resources
|
||||
5. **Use descriptive names**: Make task names clear and meaningful
|
||||
6. **Separate concerns**: Use registerEndpoints() for HTTP API and registerTasks() for background work
|
||||
7. **Dependency injection**: Pass required dependencies (NodeContext, TaskManager) to service constructors
|
||||
|
||||
## Migration from Wrapper Functions
|
||||
## Migration to Service Interface
|
||||
|
||||
### Before (manual task registration in constructor):
|
||||
```cpp
|
||||
class MyService : public Service {
|
||||
public:
|
||||
MyService(TaskManager& taskManager) : taskManager(taskManager) {
|
||||
// Tasks registered in constructor
|
||||
taskManager.registerTask("heartbeat", 2000, [this]() { sendHeartbeat(); });
|
||||
}
|
||||
|
||||
void registerEndpoints(ApiServer& api) override {
|
||||
// Only endpoints registered here
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### After (using Service interface):
|
||||
```cpp
|
||||
class MyService : public Service {
|
||||
public:
|
||||
MyService(TaskManager& taskManager) : taskManager(taskManager) {
|
||||
// No task registration in constructor
|
||||
}
|
||||
|
||||
void registerEndpoints(ApiServer& api) override {
|
||||
// Register HTTP endpoints
|
||||
api.registerEndpoint("/api/my/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatus(request); });
|
||||
}
|
||||
|
||||
void registerTasks(TaskManager& taskManager) override {
|
||||
// Register background tasks
|
||||
taskManager.registerTask("heartbeat", 2000, [this]() { sendHeartbeat(); });
|
||||
}
|
||||
|
||||
const char* getName() const override { return "MyService"; }
|
||||
};
|
||||
```
|
||||
|
||||
### Migration from Wrapper Functions
|
||||
|
||||
### Before (with wrapper functions):
|
||||
```cpp
|
||||
@@ -335,11 +497,11 @@ taskManager.registerTask("cluster_listen", interval,
|
||||
|
||||
## Compatibility
|
||||
|
||||
- The new `std::bind` support is fully backward compatible
|
||||
- Existing code using function pointers will continue to work
|
||||
- You can mix both approaches in the same project
|
||||
- The new Service interface is fully backward compatible
|
||||
- Existing code using direct TaskManager registration will continue to work
|
||||
- You can mix Service interface and direct registration in the same project
|
||||
- All existing TaskManager methods remain unchanged
|
||||
- New status monitoring methods are additive and don't break existing functionality
|
||||
- The Service interface provides a cleaner, more organized approach for framework integration
|
||||
|
||||
## Related Documentation
|
||||
|
||||
|
||||
@@ -35,15 +35,14 @@ MultiMatrixService::MultiMatrixService(NodeContext& ctx, TaskManager& taskManage
|
||||
} else {
|
||||
LOG_ERROR("MultiMatrixService", "Failed to initialize DFPlayer");
|
||||
}
|
||||
registerTasks();
|
||||
}
|
||||
|
||||
void MultiMatrixService::registerEndpoints(ApiServer& api) {
|
||||
api.addEndpoint(API_STATUS_ENDPOINT, HTTP_GET,
|
||||
api.registerEndpoint(API_STATUS_ENDPOINT, HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatusRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
api.addEndpoint(API_CONTROL_ENDPOINT, HTTP_POST,
|
||||
api.registerEndpoint(API_CONTROL_ENDPOINT, HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleControlRequest(request); },
|
||||
std::vector<ParamSpec>{
|
||||
ParamSpec{String("action"), true, String("body"), String("string"),
|
||||
@@ -144,8 +143,8 @@ void MultiMatrixService::setLoop(bool enabled) {
|
||||
LOG_INFO("MultiMatrixService", String("Loop ") + (enabled ? "enabled" : "disabled"));
|
||||
}
|
||||
|
||||
void MultiMatrixService::registerTasks() {
|
||||
m_taskManager.registerTask("multimatrix_potentiometer", POTENTIOMETER_SAMPLE_INTERVAL_MS,
|
||||
void MultiMatrixService::registerTasks(TaskManager& taskManager) {
|
||||
taskManager.registerTask("multimatrix_potentiometer", POTENTIOMETER_SAMPLE_INTERVAL_MS,
|
||||
[this]() { pollPotentiometer(); });
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ class MultiMatrixService : public Service {
|
||||
public:
|
||||
MultiMatrixService(NodeContext& ctx, TaskManager& taskManager, uint8_t rxPin, uint8_t txPin, uint8_t potentiometerPin);
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
void registerTasks(TaskManager& taskManager) override;
|
||||
const char* getName() const override { return "MultiMatrixAudio"; }
|
||||
|
||||
bool isReady() const;
|
||||
@@ -34,7 +35,6 @@ private:
|
||||
static constexpr uint8_t MAX_VOLUME = 30;
|
||||
static constexpr uint8_t POT_VOLUME_EPSILON = 2;
|
||||
|
||||
void registerTasks();
|
||||
void pollPotentiometer();
|
||||
void handleStatusRequest(AsyncWebServerRequest* request);
|
||||
void handleControlRequest(AsyncWebServerRequest* request);
|
||||
|
||||
@@ -42,7 +42,7 @@ void setup() {
|
||||
pixelController->begin();
|
||||
|
||||
audioService = std::make_shared<MultiMatrixService>(spore.getContext(), spore.getTaskManager(), MP3PLAYER_PIN_RX, MP3PLAYER_PIN_TX, POTENTIOMETER_PIN);
|
||||
spore.addService(audioService);
|
||||
spore.registerService(audioService);
|
||||
|
||||
spore.begin();
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ NeoPatternService::NeoPatternService(NodeContext& ctx, TaskManager& taskMgr, con
|
||||
neoPattern->Direction = static_cast<::direction>(direction);
|
||||
|
||||
registerPatterns();
|
||||
registerTasks();
|
||||
registerEventHandlers();
|
||||
initialized = true;
|
||||
|
||||
@@ -49,17 +48,17 @@ NeoPatternService::~NeoPatternService() {
|
||||
|
||||
void NeoPatternService::registerEndpoints(ApiServer& api) {
|
||||
// Status endpoint
|
||||
api.addEndpoint("/api/neopattern/status", HTTP_GET,
|
||||
api.registerEndpoint("/api/neopattern/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatusRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
// Patterns list endpoint
|
||||
api.addEndpoint("/api/neopattern/patterns", HTTP_GET,
|
||||
api.registerEndpoint("/api/neopattern/patterns", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handlePatternsRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
// Control endpoint
|
||||
api.addEndpoint("/api/neopattern", HTTP_POST,
|
||||
api.registerEndpoint("/api/neopattern", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleControlRequest(request); },
|
||||
std::vector<ParamSpec>{
|
||||
ParamSpec{String("pattern"), false, String("body"), String("string"), patternNamesVector()},
|
||||
@@ -73,7 +72,7 @@ void NeoPatternService::registerEndpoints(ApiServer& api) {
|
||||
});
|
||||
|
||||
// State endpoint for complex state updates
|
||||
api.addEndpoint("/api/neopattern/state", HTTP_POST,
|
||||
api.registerEndpoint("/api/neopattern/state", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleStateRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
}
|
||||
@@ -403,7 +402,7 @@ NeoPatternState NeoPatternService::getState() const {
|
||||
return currentState;
|
||||
}
|
||||
|
||||
void NeoPatternService::registerTasks() {
|
||||
void NeoPatternService::registerTasks(TaskManager& taskManager) {
|
||||
taskManager.registerTask("neopattern_update", updateIntervalMs, [this]() { update(); });
|
||||
taskManager.registerTask("neopattern_status_print", 10000, [this]() {
|
||||
LOG_INFO("NeoPattern", "Status update");
|
||||
|
||||
@@ -30,6 +30,7 @@ public:
|
||||
~NeoPatternService();
|
||||
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
void registerTasks(TaskManager& taskManager) override;
|
||||
const char* getName() const override { return "NeoPattern"; }
|
||||
|
||||
// Pattern control methods
|
||||
@@ -47,7 +48,6 @@ public:
|
||||
NeoPatternState getState() const;
|
||||
|
||||
private:
|
||||
void registerTasks();
|
||||
void registerPatterns();
|
||||
void update();
|
||||
void registerEventHandlers();
|
||||
|
||||
@@ -46,7 +46,7 @@ void setup() {
|
||||
|
||||
// Create and add custom service
|
||||
neoPatternService = new NeoPatternService(spore.getContext(), spore.getTaskManager(), config);
|
||||
spore.addService(neoPatternService);
|
||||
spore.registerService(neoPatternService);
|
||||
|
||||
// Start the API server and complete initialization
|
||||
spore.begin();
|
||||
|
||||
@@ -33,7 +33,7 @@ void setup() {
|
||||
spore.setup();
|
||||
|
||||
relayService = new RelayService(spore.getContext(), spore.getTaskManager(), RELAY_PIN);
|
||||
spore.addService(relayService);
|
||||
spore.registerService(relayService);
|
||||
|
||||
spore.begin();
|
||||
}
|
||||
|
||||
@@ -7,15 +7,14 @@ RelayService::RelayService(NodeContext& ctx, TaskManager& taskMgr, int pin)
|
||||
pinMode(relayPin, OUTPUT);
|
||||
// Many relay modules are active LOW. Start in OFF state (relay de-energized).
|
||||
digitalWrite(relayPin, HIGH);
|
||||
registerTasks();
|
||||
}
|
||||
|
||||
void RelayService::registerEndpoints(ApiServer& api) {
|
||||
api.addEndpoint("/api/relay/status", HTTP_GET,
|
||||
api.registerEndpoint("/api/relay/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatusRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
api.addEndpoint("/api/relay", HTTP_POST,
|
||||
api.registerEndpoint("/api/relay", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleControlRequest(request); },
|
||||
std::vector<ParamSpec>{
|
||||
ParamSpec{String("state"), true, String("body"), String("string"),
|
||||
@@ -82,7 +81,7 @@ void RelayService::toggle() {
|
||||
}
|
||||
}
|
||||
|
||||
void RelayService::registerTasks() {
|
||||
void RelayService::registerTasks(TaskManager& taskManager) {
|
||||
taskManager.registerTask("relay_status_print", 5000, [this]() {
|
||||
LOG_INFO("RelayService", "Status - pin: " + String(relayPin) + ", state: " + (relayOn ? "ON" : "OFF"));
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ class RelayService : public Service {
|
||||
public:
|
||||
RelayService(NodeContext& ctx, TaskManager& taskMgr, int pin);
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
void registerTasks(TaskManager& taskManager) override;
|
||||
const char* getName() const override { return "Relay"; }
|
||||
|
||||
void turnOn();
|
||||
@@ -15,8 +16,6 @@ public:
|
||||
void toggle();
|
||||
|
||||
private:
|
||||
void registerTasks();
|
||||
|
||||
NodeContext& ctx;
|
||||
TaskManager& taskManager;
|
||||
int relayPin;
|
||||
|
||||
@@ -23,7 +23,7 @@ void setup() {
|
||||
|
||||
// Create and add custom service
|
||||
relayService = new RelayService(spore.getContext(), spore.getTaskManager(), RELAY_PIN);
|
||||
spore.addService(relayService);
|
||||
spore.registerService(relayService);
|
||||
|
||||
// Start the API server and complete initialization
|
||||
spore.begin();
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
#pragma once
|
||||
#include "spore/core/ApiServer.h"
|
||||
|
||||
class TaskManager;
|
||||
|
||||
class Service {
|
||||
public:
|
||||
virtual ~Service() = default;
|
||||
virtual void registerEndpoints(ApiServer& api) = 0;
|
||||
virtual void registerTasks(TaskManager& taskManager) = 0;
|
||||
virtual const char* getName() const = 0;
|
||||
};
|
||||
|
||||
@@ -25,8 +25,8 @@ public:
|
||||
void loop();
|
||||
|
||||
// Service management
|
||||
void addService(std::shared_ptr<Service> service);
|
||||
void addService(Service* service);
|
||||
void registerService(std::shared_ptr<Service> service);
|
||||
void registerService(Service* service);
|
||||
|
||||
// Access to core components
|
||||
NodeContext& getContext() { return ctx; }
|
||||
|
||||
@@ -19,14 +19,14 @@ class ApiServer {
|
||||
public:
|
||||
ApiServer(NodeContext& ctx, TaskManager& taskMgr, uint16_t port = 80);
|
||||
void begin();
|
||||
void addService(Service& service);
|
||||
void addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler);
|
||||
void addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
void registerService(Service& service);
|
||||
void registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler);
|
||||
void registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
std::function<void(AsyncWebServerRequest*, const String&, size_t, uint8_t*, size_t, bool)> uploadHandler);
|
||||
|
||||
void addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
void registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
const std::vector<ParamSpec>& params);
|
||||
void addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
void registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
std::function<void(AsyncWebServerRequest*, const String&, size_t, uint8_t*, size_t, bool)> uploadHandler,
|
||||
const std::vector<ParamSpec>& params);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ class ClusterService : public Service {
|
||||
public:
|
||||
ClusterService(NodeContext& ctx);
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
void registerTasks(TaskManager& taskManager) override;
|
||||
const char* getName() const override { return "Cluster"; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -7,6 +7,7 @@ class MonitoringService : public Service {
|
||||
public:
|
||||
MonitoringService(CpuUsage& cpuUsage);
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
void registerTasks(TaskManager& taskManager) override;
|
||||
const char* getName() const override { return "Monitoring"; }
|
||||
|
||||
// System resource information
|
||||
|
||||
@@ -7,6 +7,7 @@ class NetworkService : public Service {
|
||||
public:
|
||||
NetworkService(NetworkManager& networkManager);
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
void registerTasks(TaskManager& taskManager) override;
|
||||
const char* getName() const override { return "Network"; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -8,6 +8,7 @@ class NodeService : public Service {
|
||||
public:
|
||||
NodeService(NodeContext& ctx, ApiServer& apiServer);
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
void registerTasks(TaskManager& taskManager) override;
|
||||
const char* getName() const override { return "Node"; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -9,6 +9,7 @@ class StaticFileService : public Service {
|
||||
public:
|
||||
StaticFileService(NodeContext& ctx, ApiServer& apiServer);
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
void registerTasks(TaskManager& taskManager) override;
|
||||
const char* getName() const override { return name.c_str(); }
|
||||
|
||||
private:
|
||||
|
||||
@@ -7,6 +7,7 @@ class TaskService : public Service {
|
||||
public:
|
||||
TaskService(TaskManager& taskManager);
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
void registerTasks(TaskManager& taskManager) override;
|
||||
const char* getName() const override { return "Task"; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[platformio]
|
||||
default_envs = base
|
||||
;default_envs = base
|
||||
src_dir = .
|
||||
data_dir = ${PROJECT_DIR}/examples/${PIOENV}/data
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ void Spore::loop() {
|
||||
yield();
|
||||
}
|
||||
|
||||
void Spore::addService(std::shared_ptr<Service> service) {
|
||||
void Spore::registerService(std::shared_ptr<Service> service) {
|
||||
if (!service) {
|
||||
LOG_WARN("Spore", "Attempted to add null service");
|
||||
return;
|
||||
@@ -95,21 +95,22 @@ void Spore::addService(std::shared_ptr<Service> service) {
|
||||
|
||||
if (apiServerStarted) {
|
||||
// If API server is already started, register the service immediately
|
||||
apiServer.addService(*service);
|
||||
LOG_INFO("Spore", "Added service '" + String(service->getName()) + "' to running API server");
|
||||
apiServer.registerService(*service);
|
||||
service->registerTasks(taskManager);
|
||||
LOG_INFO("Spore", "Added service '" + String(service->getName()) + "' to running API server and task manager");
|
||||
} else {
|
||||
LOG_INFO("Spore", "Registered service '" + String(service->getName()) + "' (will be added to API server when begin() is called)");
|
||||
}
|
||||
}
|
||||
|
||||
void Spore::addService(Service* service) {
|
||||
void Spore::registerService(Service* service) {
|
||||
if (!service) {
|
||||
LOG_WARN("Spore", "Attempted to add null service");
|
||||
return;
|
||||
}
|
||||
|
||||
// Wrap raw pointer in shared_ptr with no-op deleter to avoid double-delete
|
||||
addService(std::shared_ptr<Service>(service, [](Service*){}));
|
||||
registerService(std::shared_ptr<Service>(service, [](Service*){}));
|
||||
}
|
||||
|
||||
|
||||
@@ -155,11 +156,12 @@ void Spore::startApiServer() {
|
||||
|
||||
LOG_INFO("Spore", "Starting API server...");
|
||||
|
||||
// Register all services with API server
|
||||
// Register all services with API server and task manager
|
||||
for (auto& service : services) {
|
||||
if (service) {
|
||||
apiServer.addService(*service);
|
||||
LOG_INFO("Spore", "Added service '" + String(service->getName()) + "' to API server");
|
||||
apiServer.registerService(*service);
|
||||
service->registerTasks(taskManager);
|
||||
LOG_INFO("Spore", "Added service '" + String(service->getName()) + "' to API server and task manager");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ void ApiServer::registerEndpoint(const String& uri, int method,
|
||||
}
|
||||
}
|
||||
|
||||
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler) {
|
||||
void ApiServer::registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler) {
|
||||
// Get current service name if available
|
||||
String serviceName = "unknown";
|
||||
if (!services.empty()) {
|
||||
@@ -41,7 +41,7 @@ void ApiServer::addEndpoint(const String& uri, int method, std::function<void(As
|
||||
server.on(uri.c_str(), method, requestHandler);
|
||||
}
|
||||
|
||||
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
void ApiServer::registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
std::function<void(AsyncWebServerRequest*, const String&, size_t, uint8_t*, size_t, bool)> uploadHandler) {
|
||||
// Get current service name if available
|
||||
String serviceName = "unknown";
|
||||
@@ -53,7 +53,7 @@ void ApiServer::addEndpoint(const String& uri, int method, std::function<void(As
|
||||
}
|
||||
|
||||
// Overloads that also record minimal capability specs
|
||||
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
void ApiServer::registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
const std::vector<ParamSpec>& params) {
|
||||
// Get current service name if available
|
||||
String serviceName = "unknown";
|
||||
@@ -64,7 +64,7 @@ void ApiServer::addEndpoint(const String& uri, int method, std::function<void(As
|
||||
server.on(uri.c_str(), method, requestHandler);
|
||||
}
|
||||
|
||||
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
void ApiServer::registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
std::function<void(AsyncWebServerRequest*, const String&, size_t, uint8_t*, size_t, bool)> uploadHandler,
|
||||
const std::vector<ParamSpec>& params) {
|
||||
// Get current service name if available
|
||||
@@ -76,7 +76,7 @@ void ApiServer::addEndpoint(const String& uri, int method, std::function<void(As
|
||||
server.on(uri.c_str(), method, requestHandler, uploadHandler);
|
||||
}
|
||||
|
||||
void ApiServer::addService(Service& service) {
|
||||
void ApiServer::registerService(Service& service) {
|
||||
services.push_back(service);
|
||||
LOG_INFO("API", "Added service: " + String(service.getName()));
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
ClusterService::ClusterService(NodeContext& ctx) : ctx(ctx) {}
|
||||
|
||||
void ClusterService::registerEndpoints(ApiServer& api) {
|
||||
api.addEndpoint("/api/cluster/members", HTTP_GET,
|
||||
api.registerEndpoint("/api/cluster/members", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleMembersRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
// Generic cluster broadcast endpoint
|
||||
api.addEndpoint("/api/cluster/event", HTTP_POST,
|
||||
api.registerEndpoint("/api/cluster/event", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) {
|
||||
if (!request->hasParam("event", true) || !request->hasParam("payload", true)) {
|
||||
request->send(400, "application/json", "{\"error\":\"Missing 'event' or 'payload'\"}");
|
||||
@@ -32,6 +32,10 @@ void ClusterService::registerEndpoints(ApiServer& api) {
|
||||
});
|
||||
}
|
||||
|
||||
void ClusterService::registerTasks(TaskManager& taskManager) {
|
||||
// ClusterService doesn't register any tasks itself
|
||||
}
|
||||
|
||||
void ClusterService::handleMembersRequest(AsyncWebServerRequest* request) {
|
||||
JsonDocument doc;
|
||||
JsonArray arr = doc["members"].to<JsonArray>();
|
||||
|
||||
@@ -10,11 +10,15 @@ MonitoringService::MonitoringService(CpuUsage& cpuUsage)
|
||||
}
|
||||
|
||||
void MonitoringService::registerEndpoints(ApiServer& api) {
|
||||
api.addEndpoint("/api/monitoring/resources", HTTP_GET,
|
||||
api.registerEndpoint("/api/monitoring/resources", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleResourcesRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
}
|
||||
|
||||
void MonitoringService::registerTasks(TaskManager& taskManager) {
|
||||
// MonitoringService doesn't register any tasks itself
|
||||
}
|
||||
|
||||
MonitoringService::SystemResources MonitoringService::getSystemResources() const {
|
||||
SystemResources resources;
|
||||
|
||||
|
||||
@@ -6,20 +6,20 @@ NetworkService::NetworkService(NetworkManager& networkManager)
|
||||
|
||||
void NetworkService::registerEndpoints(ApiServer& api) {
|
||||
// WiFi scanning endpoints
|
||||
api.addEndpoint("/api/network/wifi/scan", HTTP_POST,
|
||||
api.registerEndpoint("/api/network/wifi/scan", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleWifiScanRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
api.addEndpoint("/api/network/wifi/scan", HTTP_GET,
|
||||
api.registerEndpoint("/api/network/wifi/scan", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleGetWifiNetworks(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
// Network status and configuration endpoints
|
||||
api.addEndpoint("/api/network/status", HTTP_GET,
|
||||
api.registerEndpoint("/api/network/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleNetworkStatus(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
api.addEndpoint("/api/network/wifi/config", HTTP_POST,
|
||||
api.registerEndpoint("/api/network/wifi/config", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleSetWifiConfig(request); },
|
||||
std::vector<ParamSpec>{
|
||||
ParamSpec{String("ssid"), true, String("body"), String("string"), {}, String("")},
|
||||
@@ -29,6 +29,10 @@ void NetworkService::registerEndpoints(ApiServer& api) {
|
||||
});
|
||||
}
|
||||
|
||||
void NetworkService::registerTasks(TaskManager& taskManager) {
|
||||
// NetworkService doesn't register any tasks itself
|
||||
}
|
||||
|
||||
void NetworkService::handleWifiScanRequest(AsyncWebServerRequest* request) {
|
||||
networkManager.scanWifi();
|
||||
|
||||
|
||||
@@ -6,12 +6,12 @@ NodeService::NodeService(NodeContext& ctx, ApiServer& apiServer) : ctx(ctx), api
|
||||
|
||||
void NodeService::registerEndpoints(ApiServer& api) {
|
||||
// Status endpoint
|
||||
api.addEndpoint("/api/node/status", HTTP_GET,
|
||||
api.registerEndpoint("/api/node/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatusRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
// Update endpoint with file upload
|
||||
api.addEndpoint("/api/node/update", HTTP_POST,
|
||||
api.registerEndpoint("/api/node/update", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleUpdateRequest(request); },
|
||||
[this](AsyncWebServerRequest* request, const String& filename, size_t index, uint8_t* data, size_t len, bool final) {
|
||||
handleUpdateUpload(request, filename, index, data, len, final);
|
||||
@@ -21,17 +21,17 @@ void NodeService::registerEndpoints(ApiServer& api) {
|
||||
});
|
||||
|
||||
// Restart endpoint
|
||||
api.addEndpoint("/api/node/restart", HTTP_POST,
|
||||
api.registerEndpoint("/api/node/restart", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleRestartRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
// Endpoints endpoint
|
||||
api.addEndpoint("/api/node/endpoints", HTTP_GET,
|
||||
api.registerEndpoint("/api/node/endpoints", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleEndpointsRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
// Generic local event endpoint
|
||||
api.addEndpoint("/api/node/event", HTTP_POST,
|
||||
api.registerEndpoint("/api/node/event", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) {
|
||||
if (!request->hasParam("event", true) || !request->hasParam("payload", true)) {
|
||||
request->send(400, "application/json", "{\"error\":\"Missing 'event' or 'payload'\"}");
|
||||
@@ -49,6 +49,10 @@ void NodeService::registerEndpoints(ApiServer& api) {
|
||||
});
|
||||
}
|
||||
|
||||
void NodeService::registerTasks(TaskManager& taskManager) {
|
||||
// NodeService doesn't register any tasks itself
|
||||
}
|
||||
|
||||
void NodeService::handleStatusRequest(AsyncWebServerRequest* request) {
|
||||
JsonDocument doc;
|
||||
doc["freeHeap"] = ESP.getFreeHeap();
|
||||
|
||||
@@ -20,3 +20,7 @@ void StaticFileService::registerEndpoints(ApiServer& api) {
|
||||
api.serveStatic("/", LittleFS, "/public", "max-age=3600");
|
||||
}
|
||||
|
||||
void StaticFileService::registerTasks(TaskManager& taskManager) {
|
||||
// StaticFileService doesn't register any tasks itself
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
TaskService::TaskService(TaskManager& taskManager) : taskManager(taskManager) {}
|
||||
|
||||
void TaskService::registerEndpoints(ApiServer& api) {
|
||||
api.addEndpoint("/api/tasks/status", HTTP_GET,
|
||||
api.registerEndpoint("/api/tasks/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatusRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
|
||||
api.addEndpoint("/api/tasks/control", HTTP_POST,
|
||||
api.registerEndpoint("/api/tasks/control", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleControlRequest(request); },
|
||||
std::vector<ParamSpec>{
|
||||
ParamSpec{
|
||||
@@ -31,6 +31,10 @@ void TaskService::registerEndpoints(ApiServer& api) {
|
||||
});
|
||||
}
|
||||
|
||||
void TaskService::registerTasks(TaskManager& taskManager) {
|
||||
// TaskService doesn't register any tasks itself - it manages other tasks
|
||||
}
|
||||
|
||||
void TaskService::handleStatusRequest(AsyncWebServerRequest* request) {
|
||||
JsonDocument scratch;
|
||||
auto taskStatuses = taskManager.getAllTaskStatuses(scratch);
|
||||
|
||||
Reference in New Issue
Block a user