feat: implement Spore framework class as main orchestration layer
- Add Spore class as unified interface for all core framework functionality - Implement setup() and begin() methods for flexible initialization pattern - Add service registration with addService() for both raw and smart pointers - Provide accessor methods for core components (getTaskManager, getContext, etc.) - Automatically include core services (Node, Network, Cluster, Task) - Update all examples to use simplified Spore framework approach - Fix circular dependency issues in include structure - Remove setHostname and setApiPort configuration methods - Add comprehensive documentation and usage examples The Spore class encapsulates all core functionality from the base example and provides a clean, unified API for the entire framework. Examples now use just spore.setup() -> add services -> spore.begin() -> spore.loop().
This commit is contained in:
@@ -1,10 +1,48 @@
|
||||
# Relay Service Example
|
||||
|
||||
A minimal example that uses the framework's `NodeContext`, `NetworkManager`, `TaskManager`, and `ApiServer` to control a relay via REST and log status periodically as a task.
|
||||
A minimal example that demonstrates the Spore framework with a custom RelayService. The Spore framework automatically handles all core functionality (WiFi, clustering, API server, task management) while allowing easy registration of custom services.
|
||||
|
||||
- Default relay pin: `GPIO0` (ESP-01). Override with `-DRELAY_PIN=<pin>`.
|
||||
- WiFi and API port are configured in `src/Config.cpp`.
|
||||
|
||||
## Spore Framework Usage
|
||||
|
||||
This example demonstrates the simplified Spore framework approach:
|
||||
|
||||
```cpp
|
||||
#include <Arduino.h>
|
||||
#include "Spore.h"
|
||||
#include "RelayService.h"
|
||||
|
||||
Spore spore({
|
||||
{"app", "relay"},
|
||||
{"device", "actuator"},
|
||||
{"pin", String(RELAY_PIN)}
|
||||
});
|
||||
|
||||
RelayService* relayService = nullptr;
|
||||
|
||||
void setup() {
|
||||
spore.setup();
|
||||
|
||||
relayService = new RelayService(spore.getTaskManager(), RELAY_PIN);
|
||||
spore.addService(relayService);
|
||||
|
||||
spore.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
spore.loop();
|
||||
}
|
||||
```
|
||||
|
||||
The Spore framework automatically provides:
|
||||
- WiFi connectivity management
|
||||
- Cluster discovery and management
|
||||
- REST API server with core endpoints
|
||||
- Task scheduling and execution
|
||||
- Node status monitoring
|
||||
|
||||
## Build & Upload
|
||||
|
||||
- ESP‑01S:
|
||||
@@ -32,17 +70,17 @@ curl http://192.168.1.50/api/relay/status
|
||||
|
||||
- Turn relay ON
|
||||
```bash
|
||||
curl -X POST http://192.168.1.50/api/relay/set -d state=on
|
||||
curl -X POST http://192.168.1.50/api/relay -d state=on
|
||||
```
|
||||
|
||||
- Turn relay OFF
|
||||
```bash
|
||||
curl -X POST http://192.168.1.50/api/relay/set -d state=off
|
||||
curl -X POST http://192.168.1.50/api/relay -d state=off
|
||||
```
|
||||
|
||||
- Toggle relay
|
||||
```bash
|
||||
curl -X POST http://192.168.1.50/api/relay/set -d state=toggle
|
||||
curl -X POST http://192.168.1.50/api/relay -d state=toggle
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
@@ -1,65 +1,37 @@
|
||||
#include <Arduino.h>
|
||||
#include <functional>
|
||||
#include "Globals.h"
|
||||
#include "NodeContext.h"
|
||||
#include "NetworkManager.h"
|
||||
#include "ClusterManager.h"
|
||||
#include "ApiServer.h"
|
||||
#include "TaskManager.h"
|
||||
|
||||
// Services
|
||||
#include "services/NodeService.h"
|
||||
#include "services/NetworkService.h"
|
||||
#include "services/ClusterService.h"
|
||||
#include "services/TaskService.h"
|
||||
#include "Spore.h"
|
||||
#include "RelayService.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Choose a default relay pin. For ESP-01 this is GPIO0. Adjust as needed for your board.
|
||||
#ifndef RELAY_PIN
|
||||
#define RELAY_PIN 0
|
||||
#endif
|
||||
|
||||
NodeContext ctx({
|
||||
// Create Spore instance with custom labels
|
||||
Spore spore({
|
||||
{"app", "relay"},
|
||||
{"device", "actuator"},
|
||||
{"pin", String(RELAY_PIN)}
|
||||
});
|
||||
NetworkManager network(ctx);
|
||||
TaskManager taskManager(ctx);
|
||||
ClusterManager cluster(ctx, taskManager);
|
||||
ApiServer apiServer(ctx, taskManager, ctx.config.api_server_port);
|
||||
|
||||
// Create services
|
||||
NodeService nodeService(ctx, apiServer);
|
||||
NetworkService networkService(network);
|
||||
ClusterService clusterService(ctx);
|
||||
TaskService taskService(taskManager);
|
||||
RelayService relayService(taskManager, RELAY_PIN);
|
||||
// Create custom service
|
||||
RelayService* relayService = nullptr;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
// Setup WiFi first
|
||||
network.setupWiFi();
|
||||
|
||||
// Initialize and start all tasks
|
||||
taskManager.initialize();
|
||||
|
||||
// Register services and start API server
|
||||
apiServer.addService(nodeService);
|
||||
apiServer.addService(networkService);
|
||||
apiServer.addService(clusterService);
|
||||
apiServer.addService(taskService);
|
||||
apiServer.addService(relayService);
|
||||
apiServer.begin();
|
||||
|
||||
// Print initial task status
|
||||
taskManager.printTaskStatus();
|
||||
// Initialize the Spore framework
|
||||
spore.setup();
|
||||
|
||||
// Create and add custom service
|
||||
relayService = new RelayService(spore.getTaskManager(), RELAY_PIN);
|
||||
spore.addService(relayService);
|
||||
|
||||
// Start the API server and complete initialization
|
||||
spore.begin();
|
||||
|
||||
Serial.println("[Main] Relay service registered and ready!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
taskManager.execute();
|
||||
yield();
|
||||
// Run the Spore framework loop
|
||||
spore.loop();
|
||||
}
|
||||
Reference in New Issue
Block a user