diff --git a/data/www/index.html b/data/www/index.html
index d344bc2..05a185c 100644
--- a/data/www/index.html
+++ b/data/www/index.html
@@ -23,6 +23,10 @@
System
bytes
+
+
diff --git a/data/www/styles.css b/data/www/styles.css
index 91868a7..fb13cdf 100644
--- a/data/www/styles.css
+++ b/data/www/styles.css
@@ -31,7 +31,7 @@
.sui label {
color: #b3b2b2;
}
-.sui button {
+.sui button, .sui input[type=file] {
background: #097479;
color: #eeeeee;
font-size: 0.9em;
diff --git a/src/WebApiPlugin.cpp b/src/WebApiPlugin.cpp
index 26ada79..7f3195d 100644
--- a/src/WebApiPlugin.cpp
+++ b/src/WebApiPlugin.cpp
@@ -1,126 +1,98 @@
-#ifndef __WEBAPI_PLUGIN__
-#define __WEBAPI_PLUGIN__
+#include "WebApiPlugin.h"
-#include
-#include
-#include
-
-#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
+WebApiPlugin::WebApiPlugin(AsyncWebServer *_server)
{
+ server = _server;
+ Update.runAsync(true);
+}
- public:
- AsyncWebServer *server;
- AsyncWebSocket *ws;
- SprocketMessage currentMessage;
+void WebApiPlugin::activate(Scheduler *_scheduler)
+{
+ ws = new AsyncWebSocket("/ws");
+ 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", "");
+}
+
+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);
+ if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000))
+ {
+ Update.printError(Serial);
+ }
}
-
- void activate(Scheduler *_scheduler)
+ if (!Update.hasError())
{
- ws = new AsyncWebSocket("/ws");
- 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::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 (Update.write(data, len) != len)
+ {
+ Update.printError(Serial);
+ }
}
-
- void wsBroadcast(String msg)
+ if (final)
{
- 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);
- 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", "");
- }
-
- 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
\ No newline at end of file
+}
diff --git a/src/WebApiPlugin.h b/src/WebApiPlugin.h
new file mode 100644
index 0000000..ae121cf
--- /dev/null
+++ b/src/WebApiPlugin.h
@@ -0,0 +1,36 @@
+#ifndef __WEBAPI_PLUGIN__
+#define __WEBAPI_PLUGIN__
+
+#include
+#include
+#include
+
+#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
\ No newline at end of file
diff --git a/src/WebConfigPlugin.cpp b/src/WebConfigPlugin.cpp
index 93e687e..0bfcb59 100644
--- a/src/WebConfigPlugin.cpp
+++ b/src/WebConfigPlugin.cpp
@@ -1,56 +1,36 @@
-#ifndef __WEB_CONFIG_PLUGIN_H__
-#define __WEB_CONFIG_PLUGIN_H__
+#include "WebConfigPlugin.h"
-#include
-#include
-#include
-#include "Plugin.h"
-#include "WebServerConfig.h"
-#include "utils/utils_print.h"
-
-using namespace std;
-using namespace std::placeholders;
-
-class WebConfigPlugin : public Plugin
+WebConfigPlugin::WebConfigPlugin(AsyncWebServer *webServer)
{
- private:
- AsyncWebServer *server;
-
- public:
- WebConfigPlugin(AsyncWebServer *webServer)
- {
- server = webServer;
- server->serveStatic("/config.json", SPIFFS, "config.json");
- }
- void activate(Scheduler *userScheduler)
- {
- server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
- PRINT_MSG(Serial, "WEB", "GET /heap");
- request->send(200, "text/plain", String(ESP.getFreeHeap()));
- });
- server->on("/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
- PRINT_MSG(Serial, "WEB", "POST /restart");
- ESP.restart();
- });
- server->on("/config", HTTP_POST, [](AsyncWebServerRequest *request) {
- PRINT_MSG(Serial, "WEB", "POST /config");
- if (request->hasParam("config", true) && request->hasParam("fileName", true))
+ server = webServer;
+ server->serveStatic("/config.json", SPIFFS, "config.json");
+}
+void WebConfigPlugin::activate(Scheduler *userScheduler)
+{
+ server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
+ PRINT_MSG(Serial, "WEB", "GET /heap");
+ request->send(200, "text/plain", String(ESP.getFreeHeap()));
+ });
+ server->on("/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
+ PRINT_MSG(Serial, "WEB", "POST /restart");
+ ESP.restart();
+ });
+ server->on("/config", HTTP_POST, [](AsyncWebServerRequest *request) {
+ PRINT_MSG(Serial, "WEB", "POST /config");
+ if (request->hasParam("config", true) && request->hasParam("fileName", true))
+ {
+ String inStr = request->getParam("config", true)->value();
+ String fileName = request->getParam("fileName", true)->value();
+ File f = SPIFFS.open(fileName, "w");
+ if (!f)
{
- String inStr = request->getParam("config", true)->value();
- 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();
+ PRINT_MSG(Serial, "WEB", String(String("file open for read failed: ") + fileName).c_str());
}
- request->redirect("/");
- });
- PRINT_MSG(Serial, "WEB", "WebConfig activated");
- }
-};
-
-#endif
\ No newline at end of file
+ PRINT_MSG(Serial, "WEB", String(String("writing to SPIFFS file: ") + fileName).c_str());
+ f.print(inStr);
+ f.close();
+ }
+ request->redirect("/");
+ });
+ PRINT_MSG(Serial, "WEB", "WebConfig activated");
+}
\ No newline at end of file
diff --git a/src/WebConfigPlugin.h b/src/WebConfigPlugin.h
new file mode 100644
index 0000000..ddaa9b4
--- /dev/null
+++ b/src/WebConfigPlugin.h
@@ -0,0 +1,24 @@
+#ifndef __WEB_CONFIG_PLUGIN_H__
+#define __WEB_CONFIG_PLUGIN_H__
+
+#include
+#include
+#include
+#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
\ No newline at end of file
diff --git a/src/WebServerPlugin.cpp b/src/WebServerPlugin.cpp
index bd91def..a999767 100644
--- a/src/WebServerPlugin.cpp
+++ b/src/WebServerPlugin.cpp
@@ -1,43 +1,22 @@
-#ifndef __WEB_SERVER_PLUGIN__
-#define __WEB_SERVER_PLUGIN__
+#include "WebServerPlugin.h"
-#include
-#include
-#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
+WebServerPlugin::WebServerPlugin(WebServerConfig cfg)
{
- private:
- WebServerConfig config;
-
- public:
- AsyncWebServer *server;
- WebServerPlugin(WebServerConfig cfg)
+ config = cfg;
+ server = new AsyncWebServer(config.port);
+}
+WebServerPlugin::WebServerPlugin(WebServerConfig cfg, AsyncWebServer *webServer)
+{
+ 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;
- server = new AsyncWebServer(config.port);
+ staticWebHandler.setAuthentication(config.user, config.password);
}
- WebServerPlugin(WebServerConfig cfg, AsyncWebServer *webServer)
- {
- 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
\ No newline at end of file
+ server->begin();
+ PRINT_MSG(Serial, "WEB", "Server activated");
+}
diff --git a/src/WebServerPlugin.h b/src/WebServerPlugin.h
new file mode 100644
index 0000000..b870925
--- /dev/null
+++ b/src/WebServerPlugin.h
@@ -0,0 +1,26 @@
+#ifndef __WEB_SERVER_PLUGIN__
+#define __WEB_SERVER_PLUGIN__
+
+#include
+#include
+#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
\ No newline at end of file
diff --git a/src/WebUtils.cpp b/src/WebUtils.cpp
new file mode 100644
index 0000000..10695d1
--- /dev/null
+++ b/src/WebUtils.cpp
@@ -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");
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/WebUtils.h b/src/WebUtils.h
index 9b47c72..e54048f 100644
--- a/src/WebUtils.h
+++ b/src/WebUtils.h
@@ -9,139 +9,10 @@
class WebUtils
{
public:
- static String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true)
- {
- if (request->hasParam(param, isPost))
- {
- 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");
- }
- }
- }
- }
- } */
+ 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);
+ 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);
};
#endif
\ No newline at end of file
diff --git a/src/examples/basic/main.cpp b/src/examples/basic/main.cpp
index 99e2739..22a561d 100644
--- a/src/examples/basic/main.cpp
+++ b/src/examples/basic/main.cpp
@@ -4,9 +4,9 @@
#include "ESPAsyncWebServer.h"
#include "WebServerConfig.h"
-#include "WebServerPlugin.cpp"
-#include "WebConfigPlugin.cpp"
-#include "WebApiPlugin.cpp"
+#include "WebServerPlugin.h"
+#include "WebConfigPlugin.h"
+#include "WebApiPlugin.h"
WiFiNet *network;
Sprocket *sprocket;