feat: task manager
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "ApiServer.h"
|
||||
|
||||
ApiServer::ApiServer(NodeContext& ctx, uint16_t port) : server(port), ctx(ctx) {}
|
||||
ApiServer::ApiServer(NodeContext& ctx, TaskManager& taskMgr, uint16_t port) : server(port), ctx(ctx), taskManager(taskMgr) {}
|
||||
|
||||
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler) {
|
||||
serviceRegistry.push_back(std::make_tuple(uri, method));
|
||||
@@ -37,6 +37,13 @@ void ApiServer::begin() {
|
||||
);
|
||||
addEndpoint("/api/node/restart", HTTP_POST,
|
||||
std::bind(&ApiServer::onRestartRequest, this, std::placeholders::_1));
|
||||
|
||||
// Task management endpoints
|
||||
addEndpoint("/api/tasks/status", HTTP_GET,
|
||||
std::bind(&ApiServer::onTaskStatusRequest, this, std::placeholders::_1));
|
||||
addEndpoint("/api/tasks/control", HTTP_POST,
|
||||
std::bind(&ApiServer::onTaskControlRequest, this, std::placeholders::_1));
|
||||
|
||||
server.begin();
|
||||
}
|
||||
|
||||
@@ -173,4 +180,71 @@ void ApiServer::onRestartRequest(AsyncWebServerRequest *request) {
|
||||
delay(10);
|
||||
ESP.restart();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiServer::onTaskStatusRequest(AsyncWebServerRequest *request) {
|
||||
JsonDocument doc;
|
||||
JsonArray tasksArr = doc["tasks"].to<JsonArray>();
|
||||
|
||||
// This would need to be implemented in TaskManager to expose task status
|
||||
// For now, we'll return a basic response
|
||||
JsonObject taskObj = tasksArr.add<JsonObject>();
|
||||
taskObj["message"] = "Task status endpoint - implementation pending";
|
||||
taskObj["note"] = "Task status will be available in future versions";
|
||||
|
||||
String json;
|
||||
serializeJson(doc, json);
|
||||
request->send(200, "application/json", json);
|
||||
}
|
||||
|
||||
void ApiServer::onTaskControlRequest(AsyncWebServerRequest *request) {
|
||||
// Parse the request body for task control commands
|
||||
if (request->hasParam("task", true) && request->hasParam("action", true)) {
|
||||
String taskName = request->getParam("task", true)->value();
|
||||
String action = request->getParam("action", true)->value();
|
||||
|
||||
bool success = false;
|
||||
String message = "";
|
||||
|
||||
if (action == "enable") {
|
||||
taskManager.enableTask(taskName.c_str());
|
||||
success = true;
|
||||
message = "Task enabled";
|
||||
} else if (action == "disable") {
|
||||
taskManager.disableTask(taskName.c_str());
|
||||
success = true;
|
||||
message = "Task disabled";
|
||||
} else if (action == "start") {
|
||||
taskManager.startTask(taskName.c_str());
|
||||
success = true;
|
||||
message = "Task started";
|
||||
} else if (action == "stop") {
|
||||
taskManager.stopTask(taskName.c_str());
|
||||
success = true;
|
||||
message = "Task stopped";
|
||||
} else {
|
||||
success = false;
|
||||
message = "Invalid action. Use: enable, disable, start, or stop";
|
||||
}
|
||||
|
||||
JsonDocument doc;
|
||||
doc["success"] = success;
|
||||
doc["message"] = message;
|
||||
doc["task"] = taskName;
|
||||
doc["action"] = action;
|
||||
|
||||
String json;
|
||||
serializeJson(doc, json);
|
||||
request->send(success ? 200 : 400, "application/json", json);
|
||||
} else {
|
||||
// Missing parameters
|
||||
JsonDocument doc;
|
||||
doc["success"] = false;
|
||||
doc["message"] = "Missing parameters. Required: task, action";
|
||||
doc["example"] = "{\"task\": \"discovery_send\", \"action\": \"disable\"}";
|
||||
|
||||
String json;
|
||||
serializeJson(doc, json);
|
||||
request->send(400, "application/json", json);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user