mirror of
https://gitlab.com/wirelos/sprocket-plugin-web.git
synced 2025-12-14 14:01:27 +01:00
separate declaration and implementation
This commit is contained in:
@@ -23,6 +23,10 @@
|
|||||||
<span class="heading">System</span>
|
<span class="heading">System</span>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<div><label>Free Heap: </label><span class="js-heap"></span><span> bytes</span><br><br></div>
|
<div><label>Free Heap: </label><span class="js-heap"></span><span> bytes</span><br><br></div>
|
||||||
|
<form method='POST' action='/update' enctype='multipart/form-data'>
|
||||||
|
<input type='file' name='update'><input type='submit' value='Update'>
|
||||||
|
</form>
|
||||||
|
<br>
|
||||||
<button class="js-restart">Restart</button>
|
<button class="js-restart">Restart</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
.sui label {
|
.sui label {
|
||||||
color: #b3b2b2;
|
color: #b3b2b2;
|
||||||
}
|
}
|
||||||
.sui button {
|
.sui button, .sui input[type=file] {
|
||||||
background: #097479;
|
background: #097479;
|
||||||
color: #eeeeee;
|
color: #eeeeee;
|
||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
|
|||||||
@@ -1,126 +1,98 @@
|
|||||||
#ifndef __WEBAPI_PLUGIN__
|
#include "WebApiPlugin.h"
|
||||||
#define __WEBAPI_PLUGIN__
|
|
||||||
|
|
||||||
#include <TaskSchedulerDeclarations.h>
|
WebApiPlugin::WebApiPlugin(AsyncWebServer *_server)
|
||||||
#include <Sprocket.h>
|
|
||||||
#include <Updater.h>
|
|
||||||
|
|
||||||
#include "config.h"
|
|
||||||
#include "utils/utils_print.h"
|
|
||||||
#include "WebUtils.h"
|
|
||||||
#include "WebServerPlugin.cpp"
|
|
||||||
#include "WebConfigPlugin.cpp"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace std::placeholders;
|
|
||||||
|
|
||||||
// TODO headerfile
|
|
||||||
// FIXME constants
|
|
||||||
|
|
||||||
class WebApiPlugin : public Plugin
|
|
||||||
{
|
{
|
||||||
|
server = _server;
|
||||||
|
Update.runAsync(true);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
void WebApiPlugin::activate(Scheduler *_scheduler)
|
||||||
AsyncWebServer *server;
|
{
|
||||||
AsyncWebSocket *ws;
|
ws = new AsyncWebSocket("/ws");
|
||||||
SprocketMessage currentMessage;
|
ws->onEvent(bind(&WebApiPlugin::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
||||||
|
server->addHandler(ws);
|
||||||
|
server->on("/api", HTTP_POST, bind(&WebApiPlugin::postRequestHandler, this, _1));
|
||||||
|
server->on("/update", HTTP_GET, bind(&WebApiPlugin::simpleFirmwareUploadForm, this, _1));
|
||||||
|
server->on("/update", HTTP_POST, bind(&WebApiPlugin::onFirmwareUpdateRequest, this, _1), bind(&WebApiPlugin::onFirmwareUpload, this, _1, _2, _3, _4, _5, _6));
|
||||||
|
subscribe("ws/broadcast", bind(&WebApiPlugin::wsBroadcast, this, _1));
|
||||||
|
PRINT_MSG(Serial, "WEB", "API activated");
|
||||||
|
}
|
||||||
|
|
||||||
WebApiPlugin(AsyncWebServer *_server)
|
void WebApiPlugin::wsBroadcast(String msg)
|
||||||
|
{
|
||||||
|
ws->textAll(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebApiPlugin::postRequestHandler(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
PRINT_MSG(Serial, "WEB", "POST WebApiPlugin");
|
||||||
|
currentMessage.topic = WebUtils::getRequestParameterOrDefault(request, "topic", "");
|
||||||
|
currentMessage.payload = WebUtils::getRequestParameterOrDefault(request, "payload", "");
|
||||||
|
currentMessage.broadcast = atoi(WebUtils::getRequestParameterOrDefault(request, "broadcast", "0").c_str());
|
||||||
|
String msg = currentMessage.toJsonString();
|
||||||
|
publish(currentMessage.topic, currentMessage.payload);
|
||||||
|
request->send(200, "text/plain", msg);
|
||||||
|
}
|
||||||
|
void WebApiPlugin::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||||
|
{
|
||||||
|
// FIXME to limitted
|
||||||
|
if (type == WS_EVT_DATA)
|
||||||
{
|
{
|
||||||
server = _server;
|
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||||
|
dispatch(frame);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebApiPlugin::simpleFirmwareUploadForm(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
request->send(200, "text/html", "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>");
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebApiPlugin::onFirmwareUpdateRequest(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
bool hasError = !Update.hasError();
|
||||||
|
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", hasError ? "OK" : "FAIL");
|
||||||
|
response->addHeader("Connection", "close");
|
||||||
|
request->send(response);
|
||||||
|
publish("esp/reboot", String(hasError));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebApiPlugin::onFirmwareUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
|
||||||
|
{
|
||||||
|
if (!index)
|
||||||
|
{
|
||||||
|
PRINT_MSG(Serial, "OTA", "Update Start %s", filename.c_str());
|
||||||
Update.runAsync(true);
|
Update.runAsync(true);
|
||||||
|
if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000))
|
||||||
|
{
|
||||||
|
Update.printError(Serial);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (!Update.hasError())
|
||||||
void activate(Scheduler *_scheduler)
|
|
||||||
{
|
{
|
||||||
ws = new AsyncWebSocket("/ws");
|
if (Update.write(data, len) != len)
|
||||||
ws->onEvent(bind(&WebApiPlugin::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
{
|
||||||
server->addHandler(ws);
|
Update.printError(Serial);
|
||||||
server->on("/api", HTTP_POST, bind(&WebApiPlugin::postRequestHandler, this, _1));
|
}
|
||||||
server->on("/update", HTTP_GET, bind(&WebApiPlugin::simpleFirmwareUploadFormvoid, this, _1));
|
|
||||||
server->on("/update", HTTP_POST, bind(&WebApiPlugin::onFirmwareUpdateRequest, this, _1), bind(&WebApiPlugin::onFirmwareUpload, this, _1, _2, _3, _4, _5, _6));
|
|
||||||
subscribe("ws/broadcast", bind(&WebApiPlugin::wsBroadcast, this, _1));
|
|
||||||
PRINT_MSG(Serial, "WEB", "API activated");
|
|
||||||
}
|
}
|
||||||
|
if (final)
|
||||||
void wsBroadcast(String msg)
|
|
||||||
{
|
{
|
||||||
ws->textAll(msg);
|
if (Update.end(true))
|
||||||
|
{
|
||||||
|
PRINT_MSG(Serial, "OTA", "Update Success with %uB", index + len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Update.printError(Serial);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void postRequestHandler(AsyncWebServerRequest *request)
|
void WebApiPlugin::dispatch(String &msg)
|
||||||
|
{
|
||||||
|
currentMessage.fromJsonString(msg);
|
||||||
|
if (currentMessage.valid)
|
||||||
{
|
{
|
||||||
PRINT_MSG(Serial, "WEB", "POST WebApiPlugin");
|
|
||||||
currentMessage.topic = WebUtils::getRequestParameterOrDefault(request, "topic", "");
|
|
||||||
currentMessage.payload = WebUtils::getRequestParameterOrDefault(request, "payload", "");
|
|
||||||
currentMessage.broadcast = atoi(WebUtils::getRequestParameterOrDefault(request, "broadcast", "0").c_str());
|
|
||||||
String msg = currentMessage.toJsonString();
|
|
||||||
publish(currentMessage.topic, currentMessage.payload);
|
publish(currentMessage.topic, currentMessage.payload);
|
||||||
request->send(200, "text/plain", msg);
|
|
||||||
}
|
}
|
||||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
}
|
||||||
{
|
|
||||||
// FIXME to limitted
|
|
||||||
if (type == WS_EVT_DATA)
|
|
||||||
{
|
|
||||||
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
|
||||||
dispatch(frame);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void simpleFirmwareUploadFormvoid(AsyncWebServerRequest *request)
|
|
||||||
{
|
|
||||||
request->send(200, "text/html", "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>");
|
|
||||||
}
|
|
||||||
|
|
||||||
void onFirmwareUpdateRequest(AsyncWebServerRequest *request)
|
|
||||||
{
|
|
||||||
bool hasError = !Update.hasError();
|
|
||||||
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", hasError ? "OK" : "FAIL");
|
|
||||||
response->addHeader("Connection", "close");
|
|
||||||
request->send(response);
|
|
||||||
publish("esp/reboot", String(hasError));
|
|
||||||
}
|
|
||||||
|
|
||||||
void onFirmwareUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
|
|
||||||
{
|
|
||||||
if (!index)
|
|
||||||
{
|
|
||||||
PRINT_MSG(Serial, "OTA", "Update Start %s", filename.c_str());
|
|
||||||
Update.runAsync(true);
|
|
||||||
if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000))
|
|
||||||
{
|
|
||||||
Update.printError(Serial);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!Update.hasError())
|
|
||||||
{
|
|
||||||
if (Update.write(data, len) != len)
|
|
||||||
{
|
|
||||||
Update.printError(Serial);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (final)
|
|
||||||
{
|
|
||||||
if (Update.end(true))
|
|
||||||
{
|
|
||||||
PRINT_MSG(Serial, "OTA", "Update Success with %uB", index + len);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Update.printError(Serial);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void dispatch(String &msg)
|
|
||||||
{
|
|
||||||
currentMessage.fromJsonString(msg);
|
|
||||||
if (currentMessage.valid)
|
|
||||||
{
|
|
||||||
publish(currentMessage.topic, currentMessage.payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
36
src/WebApiPlugin.h
Normal file
36
src/WebApiPlugin.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef __WEBAPI_PLUGIN__
|
||||||
|
#define __WEBAPI_PLUGIN__
|
||||||
|
|
||||||
|
#include <TaskSchedulerDeclarations.h>
|
||||||
|
#include <Sprocket.h>
|
||||||
|
#include <Updater.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "utils/utils_print.h"
|
||||||
|
#include "WebUtils.h"
|
||||||
|
#include "WebServerPlugin.h"
|
||||||
|
#include "WebConfigPlugin.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
class WebApiPlugin : public Plugin
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
AsyncWebServer *server;
|
||||||
|
AsyncWebSocket *ws;
|
||||||
|
SprocketMessage currentMessage;
|
||||||
|
|
||||||
|
WebApiPlugin(AsyncWebServer *_server);
|
||||||
|
void activate(Scheduler *_scheduler);
|
||||||
|
void wsBroadcast(String msg);
|
||||||
|
void postRequestHandler(AsyncWebServerRequest *request);
|
||||||
|
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len);
|
||||||
|
void simpleFirmwareUploadForm(AsyncWebServerRequest *request);
|
||||||
|
void onFirmwareUpdateRequest(AsyncWebServerRequest *request);
|
||||||
|
void onFirmwareUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final);
|
||||||
|
void dispatch(String &msg);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,56 +1,36 @@
|
|||||||
#ifndef __WEB_CONFIG_PLUGIN_H__
|
#include "WebConfigPlugin.h"
|
||||||
#define __WEB_CONFIG_PLUGIN_H__
|
|
||||||
|
|
||||||
#include <FS.h>
|
WebConfigPlugin::WebConfigPlugin(AsyncWebServer *webServer)
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
#include <TaskSchedulerDeclarations.h>
|
|
||||||
#include "Plugin.h"
|
|
||||||
#include "WebServerConfig.h"
|
|
||||||
#include "utils/utils_print.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace std::placeholders;
|
|
||||||
|
|
||||||
class WebConfigPlugin : public Plugin
|
|
||||||
{
|
{
|
||||||
private:
|
server = webServer;
|
||||||
AsyncWebServer *server;
|
server->serveStatic("/config.json", SPIFFS, "config.json");
|
||||||
|
}
|
||||||
public:
|
void WebConfigPlugin::activate(Scheduler *userScheduler)
|
||||||
WebConfigPlugin(AsyncWebServer *webServer)
|
{
|
||||||
{
|
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||||
server = webServer;
|
PRINT_MSG(Serial, "WEB", "GET /heap");
|
||||||
server->serveStatic("/config.json", SPIFFS, "config.json");
|
request->send(200, "text/plain", String(ESP.getFreeHeap()));
|
||||||
}
|
});
|
||||||
void activate(Scheduler *userScheduler)
|
server->on("/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
|
||||||
{
|
PRINT_MSG(Serial, "WEB", "POST /restart");
|
||||||
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
|
ESP.restart();
|
||||||
PRINT_MSG(Serial, "WEB", "GET /heap");
|
});
|
||||||
request->send(200, "text/plain", String(ESP.getFreeHeap()));
|
server->on("/config", HTTP_POST, [](AsyncWebServerRequest *request) {
|
||||||
});
|
PRINT_MSG(Serial, "WEB", "POST /config");
|
||||||
server->on("/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
|
if (request->hasParam("config", true) && request->hasParam("fileName", true))
|
||||||
PRINT_MSG(Serial, "WEB", "POST /restart");
|
{
|
||||||
ESP.restart();
|
String inStr = request->getParam("config", true)->value();
|
||||||
});
|
String fileName = request->getParam("fileName", true)->value();
|
||||||
server->on("/config", HTTP_POST, [](AsyncWebServerRequest *request) {
|
File f = SPIFFS.open(fileName, "w");
|
||||||
PRINT_MSG(Serial, "WEB", "POST /config");
|
if (!f)
|
||||||
if (request->hasParam("config", true) && request->hasParam("fileName", true))
|
|
||||||
{
|
{
|
||||||
String inStr = request->getParam("config", true)->value();
|
PRINT_MSG(Serial, "WEB", String(String("file open for read failed: ") + fileName).c_str());
|
||||||
String fileName = request->getParam("fileName", true)->value();
|
|
||||||
File f = SPIFFS.open(fileName, "w");
|
|
||||||
if (!f)
|
|
||||||
{
|
|
||||||
PRINT_MSG(Serial, "WEB", String(String("file open for read failed: ") + fileName).c_str());
|
|
||||||
}
|
|
||||||
PRINT_MSG(Serial, "WEB", String(String("writing to SPIFFS file: ") + fileName).c_str());
|
|
||||||
f.print(inStr);
|
|
||||||
f.close();
|
|
||||||
}
|
}
|
||||||
request->redirect("/");
|
PRINT_MSG(Serial, "WEB", String(String("writing to SPIFFS file: ") + fileName).c_str());
|
||||||
});
|
f.print(inStr);
|
||||||
PRINT_MSG(Serial, "WEB", "WebConfig activated");
|
f.close();
|
||||||
}
|
}
|
||||||
};
|
request->redirect("/");
|
||||||
|
});
|
||||||
#endif
|
PRINT_MSG(Serial, "WEB", "WebConfig activated");
|
||||||
|
}
|
||||||
24
src/WebConfigPlugin.h
Normal file
24
src/WebConfigPlugin.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef __WEB_CONFIG_PLUGIN_H__
|
||||||
|
#define __WEB_CONFIG_PLUGIN_H__
|
||||||
|
|
||||||
|
#include <FS.h>
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
|
#include <TaskSchedulerDeclarations.h>
|
||||||
|
#include "Plugin.h"
|
||||||
|
#include "WebServerConfig.h"
|
||||||
|
#include "utils/utils_print.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
class WebConfigPlugin : public Plugin
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
AsyncWebServer *server;
|
||||||
|
|
||||||
|
public:
|
||||||
|
WebConfigPlugin(AsyncWebServer *webServer);
|
||||||
|
void activate(Scheduler *userScheduler);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,43 +1,22 @@
|
|||||||
#ifndef __WEB_SERVER_PLUGIN__
|
#include "WebServerPlugin.h"
|
||||||
#define __WEB_SERVER_PLUGIN__
|
|
||||||
|
|
||||||
#include <FS.h>
|
WebServerPlugin::WebServerPlugin(WebServerConfig cfg)
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
#include "TaskSchedulerDeclarations.h"
|
|
||||||
#include "Plugin.h"
|
|
||||||
#include "WebServerConfig.h"
|
|
||||||
#include "utils/utils_print.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace std::placeholders;
|
|
||||||
|
|
||||||
class WebServerPlugin : public Plugin
|
|
||||||
{
|
{
|
||||||
private:
|
config = cfg;
|
||||||
WebServerConfig config;
|
server = new AsyncWebServer(config.port);
|
||||||
|
}
|
||||||
public:
|
WebServerPlugin::WebServerPlugin(WebServerConfig cfg, AsyncWebServer *webServer)
|
||||||
AsyncWebServer *server;
|
{
|
||||||
WebServerPlugin(WebServerConfig cfg)
|
config = cfg;
|
||||||
|
server = webServer;
|
||||||
|
}
|
||||||
|
void WebServerPlugin::activate(Scheduler *userScheduler)
|
||||||
|
{
|
||||||
|
AsyncStaticWebHandler &staticWebHandler = server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
|
||||||
|
if (config.useBasicAuth)
|
||||||
{
|
{
|
||||||
config = cfg;
|
staticWebHandler.setAuthentication(config.user, config.password);
|
||||||
server = new AsyncWebServer(config.port);
|
|
||||||
}
|
}
|
||||||
WebServerPlugin(WebServerConfig cfg, AsyncWebServer *webServer)
|
server->begin();
|
||||||
{
|
PRINT_MSG(Serial, "WEB", "Server activated");
|
||||||
config = cfg;
|
}
|
||||||
server = webServer;
|
|
||||||
}
|
|
||||||
void activate(Scheduler *userScheduler)
|
|
||||||
{
|
|
||||||
AsyncStaticWebHandler &staticWebHandler = server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
|
|
||||||
if (config.useBasicAuth)
|
|
||||||
{
|
|
||||||
staticWebHandler.setAuthentication(config.user, config.password);
|
|
||||||
}
|
|
||||||
server->begin();
|
|
||||||
PRINT_MSG(Serial, "WEB", "Server activated");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
26
src/WebServerPlugin.h
Normal file
26
src/WebServerPlugin.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef __WEB_SERVER_PLUGIN__
|
||||||
|
#define __WEB_SERVER_PLUGIN__
|
||||||
|
|
||||||
|
#include <FS.h>
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
|
#include "TaskSchedulerDeclarations.h"
|
||||||
|
#include "Plugin.h"
|
||||||
|
#include "WebServerConfig.h"
|
||||||
|
#include "utils/utils_print.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
class WebServerPlugin : public Plugin
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
WebServerConfig config;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AsyncWebServer *server;
|
||||||
|
WebServerPlugin(WebServerConfig cfg);
|
||||||
|
WebServerPlugin(WebServerConfig cfg, AsyncWebServer *webServer);
|
||||||
|
void activate(Scheduler *userScheduler);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
160
src/WebUtils.cpp
Normal file
160
src/WebUtils.cpp
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
#include "WebUtils.h"
|
||||||
|
|
||||||
|
String WebUtils::getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost)
|
||||||
|
{
|
||||||
|
if (request->hasParam(param, isPost))
|
||||||
|
{
|
||||||
|
return request->getParam(param, isPost)->value();
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
String WebUtils::parseFrame(AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||||
|
{
|
||||||
|
String msg = "";
|
||||||
|
if (type == WS_EVT_DATA)
|
||||||
|
{
|
||||||
|
AwsFrameInfo *info = (AwsFrameInfo *)arg;
|
||||||
|
if (info->opcode == WS_TEXT)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < info->len; i++)
|
||||||
|
{
|
||||||
|
msg += (char)data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char buff[3];
|
||||||
|
for (size_t i = 0; i < info->len; i++)
|
||||||
|
{
|
||||||
|
sprintf(buff, "%02x ", (uint8_t)data[i]);
|
||||||
|
msg += buff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
String WebUtils::parseFrameAsString(AwsEventType type, void *arg, uint8_t *data, size_t len, int start)
|
||||||
|
{
|
||||||
|
String msg = "";
|
||||||
|
if (type == WS_EVT_DATA)
|
||||||
|
{
|
||||||
|
AwsFrameInfo *info = (AwsFrameInfo *)arg;
|
||||||
|
//if(info->final && info->index == 0 && info->len == len){
|
||||||
|
if (info->opcode == WS_TEXT)
|
||||||
|
{
|
||||||
|
for (size_t i = start; i < info->len; i++)
|
||||||
|
{
|
||||||
|
msg += (char)data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char buff[3];
|
||||||
|
for (size_t i = start; i < info->len; i++)
|
||||||
|
{
|
||||||
|
sprintf(buff, "%02x ", (uint8_t)data[i]);
|
||||||
|
msg += buff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
void WebUtils::wsEventPrint(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||||
|
{
|
||||||
|
if (type == WS_EVT_CONNECT)
|
||||||
|
{
|
||||||
|
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
|
||||||
|
client->printf("Hello Client %u :)", client->id());
|
||||||
|
client->ping();
|
||||||
|
}
|
||||||
|
else if (type == WS_EVT_DISCONNECT)
|
||||||
|
{
|
||||||
|
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
|
||||||
|
}
|
||||||
|
else if (type == WS_EVT_ERROR)
|
||||||
|
{
|
||||||
|
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t *)arg), (char *)data);
|
||||||
|
}
|
||||||
|
else if (type == WS_EVT_PONG)
|
||||||
|
{
|
||||||
|
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len) ? (char *)data : "");
|
||||||
|
}
|
||||||
|
else if (type == WS_EVT_DATA)
|
||||||
|
{
|
||||||
|
AwsFrameInfo *info = (AwsFrameInfo *)arg;
|
||||||
|
String msg = "";
|
||||||
|
//the whole message is in a single frame and we got all of it's data
|
||||||
|
if (info->final && info->index == 0 && info->len == len)
|
||||||
|
{
|
||||||
|
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT) ? "text" : "binary", info->len);
|
||||||
|
|
||||||
|
if (info->opcode == WS_TEXT)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < info->len; i++)
|
||||||
|
{
|
||||||
|
msg += (char)data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char buff[3];
|
||||||
|
for (size_t i = 0; i < info->len; i++)
|
||||||
|
{
|
||||||
|
sprintf(buff, "%02x ", (uint8_t)data[i]);
|
||||||
|
msg += buff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.printf("%s\n", msg.c_str());
|
||||||
|
|
||||||
|
if (info->opcode == WS_TEXT)
|
||||||
|
client->text("I got your text message");
|
||||||
|
else
|
||||||
|
client->binary("I got your binary message");
|
||||||
|
}
|
||||||
|
//message is comprised of multiple frames or the frame is split into multiple packets
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (info->index == 0)
|
||||||
|
{
|
||||||
|
if (info->num == 0)
|
||||||
|
Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary");
|
||||||
|
Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len);
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT) ? "text" : "binary", info->index, info->index + len);
|
||||||
|
|
||||||
|
if (info->opcode == WS_TEXT)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < info->len; i++)
|
||||||
|
{
|
||||||
|
msg += (char)data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char buff[3];
|
||||||
|
for (size_t i = 0; i < info->len; i++)
|
||||||
|
{
|
||||||
|
sprintf(buff, "%02x ", (uint8_t)data[i]);
|
||||||
|
msg += buff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.printf("%s\n", msg.c_str());
|
||||||
|
|
||||||
|
if ((info->index + len) == info->len)
|
||||||
|
{
|
||||||
|
Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len);
|
||||||
|
if (info->final)
|
||||||
|
{
|
||||||
|
Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary");
|
||||||
|
if (info->message_opcode == WS_TEXT)
|
||||||
|
client->text("I got your text message");
|
||||||
|
else
|
||||||
|
client->binary("I got your binary message");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
137
src/WebUtils.h
137
src/WebUtils.h
@@ -9,139 +9,10 @@
|
|||||||
class WebUtils
|
class WebUtils
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true)
|
static String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true);
|
||||||
{
|
static String parseFrame(AwsEventType type, void *arg, uint8_t *data, size_t len);
|
||||||
if (request->hasParam(param, isPost))
|
static String parseFrameAsString(AwsEventType type, void *arg, uint8_t *data, size_t len, int start = 0);
|
||||||
{
|
static void wsEventPrint(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
|
||||||
return request->getParam(param, isPost)->value();
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
static String parseFrame(AwsEventType type, void *arg, uint8_t *data, size_t len)
|
|
||||||
{
|
|
||||||
String msg = "";
|
|
||||||
if (type == WS_EVT_DATA)
|
|
||||||
{
|
|
||||||
AwsFrameInfo *info = (AwsFrameInfo *)arg;
|
|
||||||
if (info->opcode == WS_TEXT)
|
|
||||||
{
|
|
||||||
for (size_t i = 0; i < info->len; i++)
|
|
||||||
{
|
|
||||||
msg += (char)data[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char buff[3];
|
|
||||||
for (size_t i = 0; i < info->len; i++)
|
|
||||||
{
|
|
||||||
sprintf(buff, "%02x ", (uint8_t)data[i]);
|
|
||||||
msg += buff;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
static String parseFrameAsString(AwsEventType type, void *arg, uint8_t *data, size_t len, int start = 0)
|
|
||||||
{
|
|
||||||
String msg = "";
|
|
||||||
if (type == WS_EVT_DATA)
|
|
||||||
{
|
|
||||||
AwsFrameInfo *info = (AwsFrameInfo *)arg;
|
|
||||||
//if(info->final && info->index == 0 && info->len == len){
|
|
||||||
if (info->opcode == WS_TEXT)
|
|
||||||
{
|
|
||||||
for (size_t i = start; i < info->len; i++)
|
|
||||||
{
|
|
||||||
msg += (char)data[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char buff[3];
|
|
||||||
for (size_t i = start; i < info->len; i++)
|
|
||||||
{
|
|
||||||
sprintf(buff, "%02x ", (uint8_t)data[i]);
|
|
||||||
msg += buff;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
/* static void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
|
||||||
if(type == WS_EVT_CONNECT){
|
|
||||||
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
|
|
||||||
client->printf("Hello Client %u :)", client->id());
|
|
||||||
client->ping();
|
|
||||||
} else if(type == WS_EVT_DISCONNECT){
|
|
||||||
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
|
|
||||||
} else if(type == WS_EVT_ERROR){
|
|
||||||
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
|
|
||||||
} else if(type == WS_EVT_PONG){
|
|
||||||
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
|
|
||||||
} else if(type == WS_EVT_DATA){
|
|
||||||
AwsFrameInfo * info = (AwsFrameInfo*)arg;
|
|
||||||
String msg = "";
|
|
||||||
//the whole message is in a single frame and we got all of it's data
|
|
||||||
if(info->final && info->index == 0 && info->len == len){
|
|
||||||
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len);
|
|
||||||
|
|
||||||
if(info->opcode == WS_TEXT){
|
|
||||||
for(size_t i=0; i < info->len; i++) {
|
|
||||||
msg += (char) data[i];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
char buff[3];
|
|
||||||
for(size_t i=0; i < info->len; i++) {
|
|
||||||
sprintf(buff, "%02x ", (uint8_t) data[i]);
|
|
||||||
msg += buff ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Serial.printf("%s\n",msg.c_str());
|
|
||||||
|
|
||||||
if(info->opcode == WS_TEXT)
|
|
||||||
client->text("I got your text message");
|
|
||||||
else
|
|
||||||
client->binary("I got your binary message");
|
|
||||||
}
|
|
||||||
//message is comprised of multiple frames or the frame is split into multiple packets
|
|
||||||
else {
|
|
||||||
if(info->index == 0){
|
|
||||||
if(info->num == 0)
|
|
||||||
Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
|
|
||||||
Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len);
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len);
|
|
||||||
|
|
||||||
if(info->opcode == WS_TEXT){
|
|
||||||
for(size_t i=0; i < info->len; i++) {
|
|
||||||
msg += (char) data[i];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
char buff[3];
|
|
||||||
for(size_t i=0; i < info->len; i++) {
|
|
||||||
sprintf(buff, "%02x ", (uint8_t) data[i]);
|
|
||||||
msg += buff ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Serial.printf("%s\n",msg.c_str());
|
|
||||||
|
|
||||||
if((info->index + len) == info->len){
|
|
||||||
Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len);
|
|
||||||
if(info->final){
|
|
||||||
Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
|
|
||||||
if(info->message_opcode == WS_TEXT)
|
|
||||||
client->text("I got your text message");
|
|
||||||
else
|
|
||||||
client->binary("I got your binary message");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -4,9 +4,9 @@
|
|||||||
#include "ESPAsyncWebServer.h"
|
#include "ESPAsyncWebServer.h"
|
||||||
|
|
||||||
#include "WebServerConfig.h"
|
#include "WebServerConfig.h"
|
||||||
#include "WebServerPlugin.cpp"
|
#include "WebServerPlugin.h"
|
||||||
#include "WebConfigPlugin.cpp"
|
#include "WebConfigPlugin.h"
|
||||||
#include "WebApiPlugin.cpp"
|
#include "WebApiPlugin.h"
|
||||||
|
|
||||||
WiFiNet *network;
|
WiFiNet *network;
|
||||||
Sprocket *sprocket;
|
Sprocket *sprocket;
|
||||||
|
|||||||
Reference in New Issue
Block a user