diff --git a/src/IlluCat.h b/src/IlluCat.h index 4143776..72605f9 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -25,6 +25,8 @@ class IlluCat : public Sprocket { SprocketConfig sprocketConfig; OtaConfig otaConfig; WebServerConfig webConfig; + + SprocketMessage currentMessage; IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) { sprocketConfig = cfg; @@ -41,6 +43,16 @@ class IlluCat : public Sprocket { addPlugin(new WebApi(server, 1)); } + // TODO move to core + virtual void dispatch( uint32_t from, String &msg ) { + currentMessage.fromJsonString(msg); + if(currentMessage.valid){ + currentMessage.from = from; + publish(currentMessage.topic, currentMessage.payload); + } + } + + }; #endif \ No newline at end of file diff --git a/src/wifi/WebCat.h_ b/src/wifi/WebCat.h_ deleted file mode 100644 index 83f6ee8..0000000 --- a/src/wifi/WebCat.h_ +++ /dev/null @@ -1,118 +0,0 @@ -#ifndef __WEB_CAT__ -#define __WEB_CAT__ - -#include -#include -#include - -#include "config.h" -#include "NeoPattern.cpp" -#include "NeoPatternDto.h" -#include "NeoPattern_api_json.h" -#include "NeoPattern_api_modes.cpp" -#include "utils_print.h" -#include "utils_web.h" -#include -#include -#include -#include -#include "PixelPlugin.h" -#include "IlluCat.h" - -using namespace std; -using namespace std::placeholders; - - -class WebCat : public IlluCat { - public: - AsyncWebServer* server; - AsyncWebSocket* ws; - //AsyncWebSocket* wsStream; - WebServerConfig webConfig; - - SprocketMessage currentMessage; - - WebCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : IlluCat(cfg, otaCfg, webCfg) { - webConfig = webCfg; - } - - Sprocket* activate(Scheduler* scheduler, Network* network) { - - Serial.println("SETUP SERVER"); - // initialize services - server = new AsyncWebServer(80); - ws = new AsyncWebSocket("/pixel"); - //wsStream = new AsyncWebSocket("/stream"); - Serial.println("init handlers"); - // TODO move to plugin - // setup web stuff - server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json"); - server->on("/pixel/api", HTTP_POST, bind(&WebCat::patternWebRequestHandler, this, _1)); - ws->onEvent(bind(&WebCat::onWsEvent, this, _1, _2, _3, _4, _5, _6)); - server->addHandler(ws); - //wsStream->onEvent(bind(&WebCat::onStream, this, _1, _2, _3, _4, _5, _6)); - //server->addHandler(wsStream); - Serial.println("add plugins"); - // add plugins - // TODO add HTTP OTA instead of TCP - //addPlugin(new OtaTcpPlugin(otaConfig)); - addPlugin(new WebServerPlugin(webConfig, server)); - addPlugin(new WebConfigPlugin(server)); - - return IlluCat::activate(scheduler, network);; - }; - - - // TODO move to utils - String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){ - if(request->hasParam(param, isPost)) { - return request->getParam(param, isPost)->value(); - } - return defaultValue; - } - - - void onStream(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) { - if(type == WS_EVT_DATA){ - PRINT_MSG(Serial, SPROCKET_TYPE, WebUtils::parseFrameAsString(type, arg, data, len, 0).c_str()); - pixels->ActivePattern = NONE; - pixels->handleStream(data, len); - } - } - - void patternWebRequestHandler(AsyncWebServerRequest *request) { - PRINT_MSG(Serial, SPROCKET_TYPE, "POST /pixel/api"); - currentMessage.topic = getRequestParameterOrDefault(request, "topic", ""); - currentMessage.payload = getRequestParameterOrDefault(request, "payload", ""); - currentMessage.broadcast = atoi(getRequestParameterOrDefault(request, "broadcast", "0").c_str()); - String msg = currentMessage.toJsonString(); - publish(currentMessage.topic, currentMessage.payload); - if(currentMessage.broadcast){ - network.broadcast(msg); - } - request->send(200, "text/plain", msg); - } - - virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) { - if(type == WS_EVT_DATA){ - String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0); - dispatch(0, frame); - network.broadcast(frame); - } - } - - virtual void dispatch( uint32_t from, String &msg ) { - currentMessage.fromJsonString(msg); - if(currentMessage.valid){ - currentMessage.from = from; - publish(currentMessage.topic, currentMessage.payload); - } - } - - void loop(){ - Sprocket::loop(); - yield(); - } -}; - -#endif \ No newline at end of file