mirror of
https://gitlab.com/wirelos/sprocket-plugin-web.git
synced 2025-12-15 14:18:22 +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,55 +1,30 @@
|
|||||||
#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
|
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
|
||||||
AsyncWebServer *server;
|
|
||||||
AsyncWebSocket *ws;
|
|
||||||
SprocketMessage currentMessage;
|
|
||||||
|
|
||||||
WebApiPlugin(AsyncWebServer *_server)
|
|
||||||
{
|
|
||||||
server = _server;
|
server = _server;
|
||||||
Update.runAsync(true);
|
Update.runAsync(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void activate(Scheduler *_scheduler)
|
void WebApiPlugin::activate(Scheduler *_scheduler)
|
||||||
{
|
{
|
||||||
ws = new AsyncWebSocket("/ws");
|
ws = new AsyncWebSocket("/ws");
|
||||||
ws->onEvent(bind(&WebApiPlugin::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
ws->onEvent(bind(&WebApiPlugin::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
||||||
server->addHandler(ws);
|
server->addHandler(ws);
|
||||||
server->on("/api", HTTP_POST, bind(&WebApiPlugin::postRequestHandler, this, _1));
|
server->on("/api", HTTP_POST, bind(&WebApiPlugin::postRequestHandler, this, _1));
|
||||||
server->on("/update", HTTP_GET, bind(&WebApiPlugin::simpleFirmwareUploadFormvoid, 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));
|
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));
|
subscribe("ws/broadcast", bind(&WebApiPlugin::wsBroadcast, this, _1));
|
||||||
PRINT_MSG(Serial, "WEB", "API activated");
|
PRINT_MSG(Serial, "WEB", "API activated");
|
||||||
}
|
}
|
||||||
|
|
||||||
void wsBroadcast(String msg)
|
void WebApiPlugin::wsBroadcast(String msg)
|
||||||
{
|
{
|
||||||
ws->textAll(msg);
|
ws->textAll(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void postRequestHandler(AsyncWebServerRequest *request)
|
void WebApiPlugin::postRequestHandler(AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
PRINT_MSG(Serial, "WEB", "POST WebApiPlugin");
|
PRINT_MSG(Serial, "WEB", "POST WebApiPlugin");
|
||||||
currentMessage.topic = WebUtils::getRequestParameterOrDefault(request, "topic", "");
|
currentMessage.topic = WebUtils::getRequestParameterOrDefault(request, "topic", "");
|
||||||
currentMessage.payload = WebUtils::getRequestParameterOrDefault(request, "payload", "");
|
currentMessage.payload = WebUtils::getRequestParameterOrDefault(request, "payload", "");
|
||||||
@@ -57,33 +32,33 @@ class WebApiPlugin : public Plugin
|
|||||||
String msg = currentMessage.toJsonString();
|
String msg = currentMessage.toJsonString();
|
||||||
publish(currentMessage.topic, currentMessage.payload);
|
publish(currentMessage.topic, currentMessage.payload);
|
||||||
request->send(200, "text/plain", msg);
|
request->send(200, "text/plain", msg);
|
||||||
}
|
}
|
||||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
void WebApiPlugin::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||||
{
|
{
|
||||||
// FIXME to limitted
|
// FIXME to limitted
|
||||||
if (type == WS_EVT_DATA)
|
if (type == WS_EVT_DATA)
|
||||||
{
|
{
|
||||||
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||||
dispatch(frame);
|
dispatch(frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void simpleFirmwareUploadFormvoid(AsyncWebServerRequest *request)
|
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>");
|
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)
|
void WebApiPlugin::onFirmwareUpdateRequest(AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
bool hasError = !Update.hasError();
|
bool hasError = !Update.hasError();
|
||||||
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", hasError ? "OK" : "FAIL");
|
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", hasError ? "OK" : "FAIL");
|
||||||
response->addHeader("Connection", "close");
|
response->addHeader("Connection", "close");
|
||||||
request->send(response);
|
request->send(response);
|
||||||
publish("esp/reboot", String(hasError));
|
publish("esp/reboot", String(hasError));
|
||||||
}
|
}
|
||||||
|
|
||||||
void onFirmwareUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
|
void WebApiPlugin::onFirmwareUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
|
||||||
{
|
{
|
||||||
if (!index)
|
if (!index)
|
||||||
{
|
{
|
||||||
PRINT_MSG(Serial, "OTA", "Update Start %s", filename.c_str());
|
PRINT_MSG(Serial, "OTA", "Update Start %s", filename.c_str());
|
||||||
@@ -111,16 +86,13 @@ class WebApiPlugin : public Plugin
|
|||||||
Update.printError(Serial);
|
Update.printError(Serial);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispatch(String &msg)
|
void WebApiPlugin::dispatch(String &msg)
|
||||||
{
|
{
|
||||||
currentMessage.fromJsonString(msg);
|
currentMessage.fromJsonString(msg);
|
||||||
if (currentMessage.valid)
|
if (currentMessage.valid)
|
||||||
{
|
{
|
||||||
publish(currentMessage.topic, currentMessage.payload);
|
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,29 +1,12 @@
|
|||||||
#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:
|
|
||||||
AsyncWebServer *server;
|
|
||||||
|
|
||||||
public:
|
|
||||||
WebConfigPlugin(AsyncWebServer *webServer)
|
|
||||||
{
|
|
||||||
server = webServer;
|
server = webServer;
|
||||||
server->serveStatic("/config.json", SPIFFS, "config.json");
|
server->serveStatic("/config.json", SPIFFS, "config.json");
|
||||||
}
|
}
|
||||||
void activate(Scheduler *userScheduler)
|
void WebConfigPlugin::activate(Scheduler *userScheduler)
|
||||||
{
|
{
|
||||||
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
|
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||||
PRINT_MSG(Serial, "WEB", "GET /heap");
|
PRINT_MSG(Serial, "WEB", "GET /heap");
|
||||||
request->send(200, "text/plain", String(ESP.getFreeHeap()));
|
request->send(200, "text/plain", String(ESP.getFreeHeap()));
|
||||||
@@ -50,7 +33,4 @@ class WebConfigPlugin : public Plugin
|
|||||||
request->redirect("/");
|
request->redirect("/");
|
||||||
});
|
});
|
||||||
PRINT_MSG(Serial, "WEB", "WebConfig activated");
|
PRINT_MSG(Serial, "WEB", "WebConfig activated");
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
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,35 +1,17 @@
|
|||||||
#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:
|
|
||||||
WebServerConfig config;
|
|
||||||
|
|
||||||
public:
|
|
||||||
AsyncWebServer *server;
|
|
||||||
WebServerPlugin(WebServerConfig cfg)
|
|
||||||
{
|
|
||||||
config = cfg;
|
config = cfg;
|
||||||
server = new AsyncWebServer(config.port);
|
server = new AsyncWebServer(config.port);
|
||||||
}
|
}
|
||||||
WebServerPlugin(WebServerConfig cfg, AsyncWebServer *webServer)
|
WebServerPlugin::WebServerPlugin(WebServerConfig cfg, AsyncWebServer *webServer)
|
||||||
{
|
{
|
||||||
config = cfg;
|
config = cfg;
|
||||||
server = webServer;
|
server = webServer;
|
||||||
}
|
}
|
||||||
void activate(Scheduler *userScheduler)
|
void WebServerPlugin::activate(Scheduler *userScheduler)
|
||||||
{
|
{
|
||||||
AsyncStaticWebHandler &staticWebHandler = server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
|
AsyncStaticWebHandler &staticWebHandler = server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
|
||||||
if (config.useBasicAuth)
|
if (config.useBasicAuth)
|
||||||
{
|
{
|
||||||
@@ -37,7 +19,4 @@ class WebServerPlugin : public Plugin
|
|||||||
}
|
}
|
||||||
server->begin();
|
server->begin();
|
||||||
PRINT_MSG(Serial, "WEB", "Server activated");
|
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