From 722f725bad2709284c468f0201cd2dc42ad930cd Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Thu, 15 Nov 2018 16:50:50 +0100 Subject: [PATCH] use web plugins from plugin repo --- platformio.ini | 6 +- src/IlluCat.h | 10 ++-- src/WebApiPlugin.cpp | 132 ------------------------------------------- 3 files changed, 9 insertions(+), 139 deletions(-) delete mode 100644 src/WebApiPlugin.cpp diff --git a/platformio.ini b/platformio.ini index d1ba39b..cf98794 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,8 +8,8 @@ ; Please visit documentation for the other options and examples ; http://docs.platformio.org/page/projectconf.html -;[platformio] -;env_default = build +[platformio] +env_default = build [common] framework = arduino @@ -39,6 +39,7 @@ lib_deps = ${common.lib_deps} ESP8266mDNS ESP Async WebServer https://gitlab.com/wirelos/sprocket-lib.git#develop + https://gitlab.com/wirelos/sprocket-plugin-web.git [env:build-mesh] @@ -65,6 +66,7 @@ build_flags = -Wl,-Teagle.flash.4m1m.ld -DSPROCKET_PRINT=0 lib_deps = ${common.lib_deps} https://gitlab.com/wirelos/sprocket-lib.git#develop + https://gitlab.com/wirelos/sprocket-plugin-web.git ;[env:mqttcat] diff --git a/src/IlluCat.h b/src/IlluCat.h index f3ac3c1..e6aef08 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -5,10 +5,10 @@ #include #include -#include -#include -#include -#include "WebApiPlugin.cpp" +#include "WebServerConfig.h" +#include "WebServerPlugin.h" +#include "WebConfigPlugin.h" +#include "WebApiPlugin.h" #include "PixelPlugin.h" using namespace std; @@ -30,7 +30,7 @@ class IlluCat : public Sprocket addPlugin(new PixelPlugin()); addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebConfigPlugin(server)); - addPlugin(new WebApiPlugin(server, "mesh/broadcast")); + addPlugin(new WebApiPlugin(server)); } }; diff --git a/src/WebApiPlugin.cpp b/src/WebApiPlugin.cpp deleted file mode 100644 index 43a8ca4..0000000 --- a/src/WebApiPlugin.cpp +++ /dev/null @@ -1,132 +0,0 @@ -#ifndef __WEBAPI_PLUGIN__ -#define __WEBAPI_PLUGIN__ - -#include -#include -#include - -#include "config.h" -#include "utils_print.h" -#include "utils_web.h" -#include -#include - -using namespace std; -using namespace std::placeholders; - -// TODO headerfile -// FIXME constants - -class WebApiPlugin : public Plugin -{ - - public: - AsyncWebServer *server; - AsyncWebSocket *ws; - SprocketMessage currentMessage; - - int broadcast; - String broadcastTopic; - - WebApiPlugin(AsyncWebServer *_server, String _broadcastTopic) - { - server = _server; - broadcast = _broadcastTopic ? 1 : 0; - broadcastTopic = _broadcastTopic; - Update.runAsync(true); - } - - void 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::simpleFirmwareUploadFormvoid, this, _1)); - server->on("/update", HTTP_POST, bind(&WebApiPlugin::onFirmwareUpdateRequest, this, _1), bind(&WebApiPlugin::onFirmwareUpload, this, _1, _2, _3, _4, _5, _6)); - } - - void postRequestHandler(AsyncWebServerRequest *request) - { - PRINT_MSG(Serial, SPROCKET_TYPE, "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); - if (currentMessage.broadcast) - { - publish(broadcastTopic, msg); - } - 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, SPROCKET_TYPE, "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, SPROCKET_TYPE, "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); - if (broadcast) - { - publish(broadcastTopic, msg); - } - } - } -}; - -#endif \ No newline at end of file