refactor: harmonize method names
This commit is contained in:
@@ -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