move webserver stuff to plugin

This commit is contained in:
2018-08-29 09:03:38 +02:00
parent 4c32af3e1b
commit 52fdf0e5e0
15 changed files with 156 additions and 24 deletions

View File

@@ -0,0 +1,57 @@
#ifndef __WEB_CONFIG_PLUGIN_H__
#define __WEB_CONFIG_PLUGIN_H__
#include "TaskSchedulerDeclarations.h"
#include "ArduinoOTA.h"
#include "MeshNet.h"
#include "Plugin.h"
#include <plugins/WebSO.h>
#include <base/MeshSprocketConfig.h>
using namespace std;
using namespace std::placeholders;
class WebConfigPlugin : public Plugin {
private:
MeshNet* net;
AsyncWebServer* server;
public:
WebConfigPlugin(AsyncWebServer* webServer){
server = webServer;
}
void activate(Scheduler* userScheduler, Network* network){
net = static_cast<MeshNet*>(network);
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("GET /heap");
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
server->on("/config", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("GET /config");
MeshSprocketConfig config;
config.fromFile("/config.json");
config.meshPassword = "";
config.stationPassword = "";
request->send(200, "text/plain", config.toJsonString());
});
// TODO needs testing
server->on("/config", HTTP_POST, [](AsyncWebServerRequest *request){
Serial.println("POST /config");
// read existing config
MeshSprocketConfig config;
config.fromFile("/config.json");
if(request->hasParam("mesh", true)) {
String inStr = request->getParam("mesh", true)->value();
config.fromJsonString(inStr);
Serial.println(config.toJsonString());
// config.saveFile("/config.json");
}
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
}
};
#endif