implement getting commands and changing the state

This commit is contained in:
Nicu Hodos 2025-09-06 21:41:45 +02:00
parent b4461d629b
commit 234649cdd2

View File

@ -15,32 +15,27 @@ namespace WebServer {
void setup() {
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hello, world");
});
server.on("/switch", HTTP_PUT, [](AsyncWebServerRequest *request) {
if (request->hasParam("id") && request->hasParam("state")) {
AsyncWebParameter* switchId = request->getParam("id");
AsyncWebParameter* switchState = request->getParam("state");
server.on("/command", HTTP_PUT, [](AsyncWebServerRequest *request) {
if (request->hasParam("id", true) && request->hasParam("state", true)) {
AsyncWebParameter* switchId = request->getParam("id", true);
AsyncWebParameter* switchState = request->getParam("state", true);
auto cmd = Command::mapCommandIds[string(switchId->value().c_str())];
if (cmd) cmd->onCommand(switchState->value().c_str());
request->send(200, "text/plain", switchState->value().c_str());
if (cmd) {
cmd->onCommand(switchState->value().c_str());
request->send(200, "text/plain", switchState->value().c_str());
} else {
request->send(200, "text/plain", "Command not found");
}
} else {
request->send(200, "text/plain", "No parameters provided");
}
});
server.on("/switch", HTTP_GET, [](AsyncWebServerRequest *request) {
char switchList[1024] = {};
for ( auto it = Command::mapCommandIds.begin(); it != Command::mapCommandIds.end(); ++it )
snprintf(switchList, sizeof(switchList), "%s ", it->first.c_str());
request->send(200, "text/plain", switchList);
});
server.on("/restart", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain");
ESP.restart();
server.on("/command", HTTP_GET, [](AsyncWebServerRequest *request) {
string list;
for (auto it = Command::mapCommandIds.begin(); it != Command::mapCommandIds.end(); ++it)
list.append(it->first.c_str()).append("\n");
request->send(200, "text/plain", list.c_str());
});
server.onNotFound(notFound);