Compare commits
2 Commits
594b5e3af6
...
025ac7b810
| Author | SHA1 | Date | |
|---|---|---|---|
| 025ac7b810 | |||
| a9f56c1279 |
@@ -11,6 +11,7 @@
|
||||
#include "spore/types/NodeInfo.h"
|
||||
#include "spore/core/TaskManager.h"
|
||||
#include "spore/types/ApiTypes.h"
|
||||
#include "spore/util/Logging.h"
|
||||
|
||||
class Service; // Forward declaration
|
||||
|
||||
@@ -19,15 +20,17 @@ 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,
|
||||
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,
|
||||
const std::vector<ParamSpec>& params);
|
||||
const String& serviceName = "unknown");
|
||||
void addEndpoint(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);
|
||||
const String& serviceName = "unknown");
|
||||
|
||||
void addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
const std::vector<ParamSpec>& params, const String& serviceName = "unknown");
|
||||
void addEndpoint(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, const String& serviceName = "unknown");
|
||||
|
||||
// Static file serving
|
||||
void serveStatic(const String& uri, fs::FS& fs, const String& path, const String& cache_header = "");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "spore/util/JsonSerializable.h"
|
||||
#include "spore/util/Logging.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
namespace spore {
|
||||
@@ -57,7 +58,25 @@ public:
|
||||
* @param item The serializable item to add
|
||||
*/
|
||||
void addItem(const util::JsonSerializable& item) {
|
||||
JsonArray arr = doc[collectionKey].to<JsonArray>();
|
||||
// Ensure the array exists and get a reference to it
|
||||
if (!doc[collectionKey].is<JsonArray>()) {
|
||||
doc[collectionKey] = JsonArray();
|
||||
}
|
||||
JsonArray arr = doc[collectionKey].as<JsonArray>();
|
||||
JsonObject obj = arr.add<JsonObject>();
|
||||
item.toJson(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a serializable item to the collection (move version)
|
||||
* @param item The serializable item to add
|
||||
*/
|
||||
void addItem(util::JsonSerializable&& item) {
|
||||
// Ensure the array exists and get a reference to it
|
||||
if (!doc[collectionKey].is<JsonArray>()) {
|
||||
doc[collectionKey] = JsonArray();
|
||||
}
|
||||
JsonArray arr = doc[collectionKey].as<JsonArray>();
|
||||
JsonObject obj = arr.add<JsonObject>();
|
||||
item.toJson(obj);
|
||||
}
|
||||
@@ -68,7 +87,11 @@ public:
|
||||
*/
|
||||
template<typename Container>
|
||||
void addItems(const Container& items) {
|
||||
JsonArray arr = doc[collectionKey].to<JsonArray>();
|
||||
// Ensure the array exists and get a reference to it
|
||||
if (!doc[collectionKey].is<JsonArray>()) {
|
||||
doc[collectionKey] = JsonArray();
|
||||
}
|
||||
JsonArray arr = doc[collectionKey].as<JsonArray>();
|
||||
for (const auto& item : items) {
|
||||
JsonObject obj = arr.add<JsonObject>();
|
||||
item.toJson(obj);
|
||||
|
||||
@@ -19,8 +19,7 @@ public:
|
||||
* @param node The NodeInfo to add
|
||||
*/
|
||||
void addNode(const NodeInfo& node) {
|
||||
NodeInfoSerializable serializable(const_cast<NodeInfo&>(node));
|
||||
addItem(serializable);
|
||||
addItem(NodeInfoSerializable(const_cast<NodeInfo&>(node)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "ApiResponse.h"
|
||||
#include "EndpointInfoSerializable.h"
|
||||
#include "NodeInfo.h"
|
||||
#include "spore/util/Logging.h"
|
||||
#include <vector>
|
||||
|
||||
namespace spore {
|
||||
@@ -60,8 +61,7 @@ public:
|
||||
* @param endpoint The EndpointInfo to add
|
||||
*/
|
||||
void addEndpoint(const EndpointInfo& endpoint) {
|
||||
EndpointInfoSerializable serializable(endpoint);
|
||||
addItem(serializable);
|
||||
addItem(EndpointInfoSerializable(endpoint));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,13 +12,16 @@ namespace types {
|
||||
*/
|
||||
class TaskInfoSerializable : public util::JsonSerializable {
|
||||
private:
|
||||
const String& taskName;
|
||||
String taskName;
|
||||
const JsonObject& taskData;
|
||||
|
||||
public:
|
||||
TaskInfoSerializable(const String& name, const JsonObject& data)
|
||||
: taskName(name), taskData(data) {}
|
||||
|
||||
TaskInfoSerializable(const std::string& name, const JsonObject& data)
|
||||
: taskName(name.c_str()), taskData(data) {}
|
||||
|
||||
/**
|
||||
* Serialize task info to JsonObject
|
||||
*/
|
||||
|
||||
@@ -30,7 +30,10 @@ public:
|
||||
*/
|
||||
void addTask(const String& taskName, const JsonObject& taskData) {
|
||||
TaskInfoSerializable serializable(taskName, taskData);
|
||||
JsonArray tasksArr = doc["tasks"].to<JsonArray>();
|
||||
if (!doc["tasks"].is<JsonArray>()) {
|
||||
doc["tasks"] = JsonArray();
|
||||
}
|
||||
JsonArray tasksArr = doc["tasks"].as<JsonArray>();
|
||||
JsonObject taskObj = tasksArr.add<JsonObject>();
|
||||
serializable.toJson(taskObj);
|
||||
}
|
||||
@@ -41,8 +44,11 @@ public:
|
||||
* @param taskData Task data as JsonObject
|
||||
*/
|
||||
void addTask(const std::string& taskName, const JsonObject& taskData) {
|
||||
TaskInfoSerializable serializable(String(taskName.c_str()), taskData);
|
||||
JsonArray tasksArr = doc["tasks"].to<JsonArray>();
|
||||
TaskInfoSerializable serializable(taskName, taskData);
|
||||
if (!doc["tasks"].is<JsonArray>()) {
|
||||
doc["tasks"] = JsonArray();
|
||||
}
|
||||
JsonArray tasksArr = doc["tasks"].as<JsonArray>();
|
||||
JsonObject taskObj = tasksArr.add<JsonObject>();
|
||||
serializable.toJson(taskObj);
|
||||
}
|
||||
@@ -93,6 +99,9 @@ public:
|
||||
* @param taskStatuses Vector of pairs of task name to task data
|
||||
*/
|
||||
void buildCompleteResponse(const std::vector<std::pair<std::string, JsonObject>>& taskStatuses) {
|
||||
// Clear the document first since getAllTaskStatuses creates a root array
|
||||
doc.clear();
|
||||
|
||||
// Set summary
|
||||
size_t totalTasks = taskStatuses.size();
|
||||
size_t activeTasks = 0;
|
||||
@@ -103,9 +112,23 @@ public:
|
||||
}
|
||||
setSummary(totalTasks, activeTasks);
|
||||
|
||||
// Add all tasks
|
||||
// Add all tasks - extract data before clearing to avoid invalid references
|
||||
JsonArray tasksArr = doc["tasks"].to<JsonArray>();
|
||||
for (const auto& pair : taskStatuses) {
|
||||
addTask(pair.first, pair.second);
|
||||
// Extract data from JsonObject before it becomes invalid
|
||||
String taskName = pair.first.c_str();
|
||||
unsigned long interval = pair.second["interval"];
|
||||
bool enabled = pair.second["enabled"];
|
||||
bool running = pair.second["running"];
|
||||
bool autoStart = pair.second["autoStart"];
|
||||
|
||||
// Create new JsonObject in our document
|
||||
JsonObject taskObj = tasksArr.add<JsonObject>();
|
||||
taskObj["name"] = taskName;
|
||||
taskObj["interval"] = interval;
|
||||
taskObj["enabled"] = enabled;
|
||||
taskObj["running"] = running;
|
||||
taskObj["autoStart"] = autoStart;
|
||||
}
|
||||
|
||||
// Set system info
|
||||
|
||||
@@ -21,57 +21,31 @@ void ApiServer::registerEndpoint(const String& uri, int method,
|
||||
const String& serviceName) {
|
||||
// Add to local endpoints
|
||||
endpoints.push_back(EndpointInfo{uri, method, params, serviceName, true});
|
||||
|
||||
// Update cluster if needed
|
||||
if (ctx.memberList && !ctx.memberList->empty()) {
|
||||
auto it = ctx.memberList->find(ctx.hostname);
|
||||
if (it != ctx.memberList->end()) {
|
||||
it->second.endpoints.push_back(EndpointInfo{uri, method, params, serviceName, true});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler) {
|
||||
// Get current service name if available
|
||||
String serviceName = "unknown";
|
||||
if (!services.empty()) {
|
||||
serviceName = services.back().get().getName();
|
||||
}
|
||||
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
const String& serviceName) {
|
||||
registerEndpoint(uri, method, {}, serviceName);
|
||||
server.on(uri.c_str(), method, requestHandler);
|
||||
}
|
||||
|
||||
void ApiServer::addEndpoint(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";
|
||||
if (!services.empty()) {
|
||||
serviceName = services.back().get().getName();
|
||||
}
|
||||
std::function<void(AsyncWebServerRequest*, const String&, size_t, uint8_t*, size_t, bool)> uploadHandler,
|
||||
const String& serviceName) {
|
||||
registerEndpoint(uri, method, {}, serviceName);
|
||||
server.on(uri.c_str(), method, requestHandler, uploadHandler);
|
||||
}
|
||||
|
||||
// Overloads that also record minimal capability specs
|
||||
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
||||
const std::vector<ParamSpec>& params) {
|
||||
// Get current service name if available
|
||||
String serviceName = "unknown";
|
||||
if (!services.empty()) {
|
||||
serviceName = services.back().get().getName();
|
||||
}
|
||||
const std::vector<ParamSpec>& params, const String& serviceName) {
|
||||
registerEndpoint(uri, method, params, serviceName);
|
||||
server.on(uri.c_str(), method, requestHandler);
|
||||
}
|
||||
|
||||
void ApiServer::addEndpoint(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
|
||||
String serviceName = "unknown";
|
||||
if (!services.empty()) {
|
||||
serviceName = services.back().get().getName();
|
||||
}
|
||||
const std::vector<ParamSpec>& params, const String& serviceName) {
|
||||
registerEndpoint(uri, method, params, serviceName);
|
||||
server.on(uri.c_str(), method, requestHandler, uploadHandler);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ ClusterService::ClusterService(NodeContext& ctx) : ctx(ctx) {}
|
||||
void ClusterService::registerEndpoints(ApiServer& api) {
|
||||
api.addEndpoint("/api/cluster/members", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleMembersRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
std::vector<ParamSpec>{}, "ClusterService");
|
||||
}
|
||||
|
||||
void ClusterService::handleMembersRequest(AsyncWebServerRequest* request) {
|
||||
|
||||
@@ -12,7 +12,7 @@ MonitoringService::MonitoringService(CpuUsage& cpuUsage)
|
||||
void MonitoringService::registerEndpoints(ApiServer& api) {
|
||||
api.addEndpoint("/api/monitoring/resources", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleResourcesRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
std::vector<ParamSpec>{}, "MonitoringService");
|
||||
}
|
||||
|
||||
MonitoringService::SystemResources MonitoringService::getSystemResources() const {
|
||||
|
||||
@@ -8,16 +8,16 @@ void NetworkService::registerEndpoints(ApiServer& api) {
|
||||
// WiFi scanning endpoints
|
||||
api.addEndpoint("/api/network/wifi/scan", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleWifiScanRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
std::vector<ParamSpec>{}, "NetworkService");
|
||||
|
||||
api.addEndpoint("/api/network/wifi/scan", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleGetWifiNetworks(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
std::vector<ParamSpec>{}, "NetworkService");
|
||||
|
||||
// Network status and configuration endpoints
|
||||
api.addEndpoint("/api/network/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleNetworkStatus(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
std::vector<ParamSpec>{}, "NetworkService");
|
||||
|
||||
api.addEndpoint("/api/network/wifi/config", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleSetWifiConfig(request); },
|
||||
@@ -26,7 +26,7 @@ void NetworkService::registerEndpoints(ApiServer& api) {
|
||||
ParamSpec{String("password"), true, String("body"), String("string"), {}, String("")},
|
||||
ParamSpec{String("connect_timeout_ms"), false, String("body"), String("number"), {}, String("10000")},
|
||||
ParamSpec{String("retry_delay_ms"), false, String("body"), String("number"), {}, String("500")}
|
||||
});
|
||||
}, "NetworkService");
|
||||
}
|
||||
|
||||
void NetworkService::handleWifiScanRequest(AsyncWebServerRequest* request) {
|
||||
|
||||
@@ -13,7 +13,7 @@ void NodeService::registerEndpoints(ApiServer& api) {
|
||||
// Status endpoint
|
||||
api.addEndpoint("/api/node/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatusRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
std::vector<ParamSpec>{}, "NodeService");
|
||||
|
||||
// Update endpoint with file upload
|
||||
api.addEndpoint("/api/node/update", HTTP_POST,
|
||||
@@ -23,17 +23,17 @@ void NodeService::registerEndpoints(ApiServer& api) {
|
||||
},
|
||||
std::vector<ParamSpec>{
|
||||
ParamSpec{String("firmware"), true, String("body"), String("file"), {}, String("")}
|
||||
});
|
||||
}, "NodeService");
|
||||
|
||||
// Restart endpoint
|
||||
api.addEndpoint("/api/node/restart", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleRestartRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
std::vector<ParamSpec>{}, "NodeService");
|
||||
|
||||
// Endpoints endpoint
|
||||
api.addEndpoint("/api/node/endpoints", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleEndpointsRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
std::vector<ParamSpec>{}, "NodeService");
|
||||
}
|
||||
|
||||
void NodeService::handleStatusRequest(AsyncWebServerRequest* request) {
|
||||
|
||||
@@ -11,7 +11,7 @@ TaskService::TaskService(TaskManager& taskManager) : taskManager(taskManager) {}
|
||||
void TaskService::registerEndpoints(ApiServer& api) {
|
||||
api.addEndpoint("/api/tasks/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) { handleStatusRequest(request); },
|
||||
std::vector<ParamSpec>{});
|
||||
std::vector<ParamSpec>{}, "TaskService");
|
||||
|
||||
api.addEndpoint("/api/tasks/control", HTTP_POST,
|
||||
[this](AsyncWebServerRequest* request) { handleControlRequest(request); },
|
||||
@@ -32,14 +32,17 @@ void TaskService::registerEndpoints(ApiServer& api) {
|
||||
{String("enable"), String("disable"), String("start"), String("stop"), String("status")},
|
||||
String("")
|
||||
}
|
||||
});
|
||||
}, "TaskService");
|
||||
}
|
||||
|
||||
void TaskService::handleStatusRequest(AsyncWebServerRequest* request) {
|
||||
TaskStatusResponse response;
|
||||
|
||||
// Get task statuses using a separate document to avoid reference issues
|
||||
JsonDocument scratch;
|
||||
auto taskStatuses = taskManager.getAllTaskStatuses(scratch);
|
||||
|
||||
TaskStatusResponse response;
|
||||
// Build the complete response with the task data
|
||||
response.buildCompleteResponse(taskStatuses);
|
||||
request->send(200, "application/json", response.toJsonString());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user