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

@@ -44,7 +44,7 @@ class OtaTcpPlugin : public Plugin {
WiFi.gatewayIP().printTo(Serial);
}
}
void setup(Scheduler* userScheduler, Network* network){
void activate(Scheduler* userScheduler, Network* network){
// connect done in network class?
//connectUpdateNetwork(network);
net = static_cast<MeshNet*>(network);
@@ -79,6 +79,8 @@ class OtaTcpPlugin : public Plugin {
else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
});
enable();
}
void disable(){
otaTask.disable();

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

13
src/plugins/WebSO.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef __SHARED_PLUGINS__
#define __SHARED_PLUGINS__
#include <ESPAsyncWebServer.h>
extern AsyncWebServer WEBSERVER;
struct WebServerConfig {
const char* contextPath;
const char* docRoot;
const char* defaultFile;
};
#endif

View File

@@ -0,0 +1,33 @@
#ifndef __WEB_SERVER_PLUGIN__
#define __WEB_SERVER_PLUGIN__
#include <FS.h>
#include "TaskSchedulerDeclarations.h"
#include "ArduinoOTA.h"
#include "MeshNet.h"
#include "Plugin.h"
#include <plugins/WebSO.h>
using namespace std;
using namespace std::placeholders;
class WebServerPlugin : public Plugin {
private:
WebServerConfig config;
MeshNet* net;
AsyncWebServer* server;
public:
WebServerPlugin(WebServerConfig cfg, AsyncWebServer* webServer){
config = cfg;
server = webServer;
}
void activate(Scheduler* userScheduler, Network* network){
//connectUpdateNetwork(network);
net = static_cast<MeshNet*>(network);
server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
server->begin();
Serial.println("WebServer activated");
}
};
#endif