mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-16 13:25:03 +01:00
move webserver stuff to plugin
This commit is contained in:
@@ -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();
|
||||
|
||||
57
src/plugins/WebConfigPlugin.cpp
Normal file
57
src/plugins/WebConfigPlugin.cpp
Normal 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
13
src/plugins/WebSO.h
Normal 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
|
||||
33
src/plugins/WebServerPlugin.cpp
Normal file
33
src/plugins/WebServerPlugin.cpp
Normal 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
|
||||
Reference in New Issue
Block a user