first draft - use it as a library
This commit is contained in:
parent
6924775e02
commit
b4461d629b
54
src/webserver.h
Normal file
54
src/webserver.h
Normal file
@ -0,0 +1,54 @@
|
||||
#ifdef ESP32
|
||||
#include <AsyncTCP.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESPAsyncTCP.h>
|
||||
#endif
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include "ha.h"
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
namespace WebServer {
|
||||
void notFound(AsyncWebServerRequest *request) {
|
||||
request->send(404, "text/plain", "Not found");
|
||||
}
|
||||
|
||||
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");
|
||||
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());
|
||||
} 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.onNotFound(notFound);
|
||||
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
server.end();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user