From 845ee0f2cd5e5d9dffd28dc71e16adf09de2d718 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Sun, 27 May 2018 10:35:37 +0200 Subject: [PATCH] Adding Memory mode an Gzipdata as default --- src/ESPUI.cpp | 73 +++++++++++++++++++++++++++++++++++++-- src/ESPUI.h | 3 +- src/uploadDataControls.h | 2 ++ src/uploadDataNormalize.h | 2 ++ src/uploadDataSlider.h | 3 ++ src/uploadDataStyle.h | 2 ++ src/uploadDataZepto.h | 5 +++ 7 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index a19da18..4e5f11f 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -505,7 +505,7 @@ void ESPUIClass::jsonDom(AsyncWebSocketClient *client) { } } -void ESPUIClass::begin(const char *_title) { +void ESPUIClass::beginSPIFFS(const char *_title) { ui_title = _title; server = new AsyncWebServer(80); @@ -530,7 +530,76 @@ void ESPUIClass::begin(const char *_title) { // Heap for general Servertest server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send(200, "text/plain", String(ESP.getFreeHeap())); + request->send(200, "text/plain", String(ESP.getFreeHeap()+ " In SPIFFSmode")); + }); + + server->onNotFound( + [](AsyncWebServerRequest *request) { + request->send(404); + }); + + server->on("/zepto.js", HTTP_GET, [](AsyncWebServerRequest *request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_ZEPTO_GZIP, sizeof(JS_ZEPTO_GZIP)); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + server->begin(); + if (debug) + Serial.println("UI Initialized"); +} + +void ESPUIClass::begin(const char *_title) { + + ui_title = _title; + server = new AsyncWebServer(80); + ws = new AsyncWebSocket("/ws"); + + ws->onEvent(onWsEvent); + server->addHandler(ws); + + server->on("/", HTTP_GET, [](AsyncWebServerRequest *request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", HTML_INDEX); + request->send(response); + }); + + // Javascript files + + server->on("/js/zepto.min.js", HTTP_GET, [](AsyncWebServerRequest *request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_ZEPTO_GZIP, sizeof(JS_ZEPTO_GZIP)); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + server->on("/js/controls.js", HTTP_GET, [](AsyncWebServerRequest *request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_CONTROLS_GZIP, sizeof(JS_CONTROLS_GZIP)); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + server->on("/js/slider.js", HTTP_GET, [](AsyncWebServerRequest *request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_SLIDER_GZIP, sizeof(JS_SLIDER_GZIP)); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + // Stylesheets + + server->on("/css/style.css", HTTP_GET, [](AsyncWebServerRequest *request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "text/css", CSS_STYLE_GZIP, sizeof(CSS_STYLE_GZIP)); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + server->on("/css/normalize.css", HTTP_GET, [](AsyncWebServerRequest *request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "text/css", CSS_NORMALIZE_GZIP, sizeof(CSS_NORMALIZE_GZIP)); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + // Heap for general Servertest + server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) { + request->send(200, "text/plain", String(ESP.getFreeHeap() + " In Memorymode")); }); server->onNotFound( diff --git a/src/ESPUI.h b/src/ESPUI.h index e0d874c..58a40ad 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -86,7 +86,8 @@ typedef struct Control { class ESPUIClass { public: -void begin(const char *_title); // Setup servers and page +void begin(const char *_title); // Setup servers and page in Memorymode +void beginSPIFFS(const char *_title); // Setup servers and page in SPIFFSmode void prepareFileSystem(); // Initially preps the filesystem and loads a lot of stuff into SPIFFS void list(); diff --git a/src/uploadDataControls.h b/src/uploadDataControls.h index 2ddce17..6d88e51 100644 --- a/src/uploadDataControls.h +++ b/src/uploadDataControls.h @@ -1,3 +1,5 @@ const char JS_CONTROLS[] PROGMEM = R"=====( function colorClass(e){switch(e=Number(e)){case C_TURQUOISE:return"turquoise";case C_EMERALD:return"emerald";case C_PETERRIVER:return"peterriver";case C_WETASPHALT:return"wetasphalt";case C_SUNFLOWER:return"sunflower";case C_CARROT:return"carrot";case C_ALIZARIN:return"alizarin";case C_NONE:default:return""}}function start(){websock=new WebSocket("ws://"+window.location.hostname+"/ws"),websock.onopen=function(e){console.log("websock open"),$("#conStatus").addClass("color-green"),$("#conStatus").text("Connected")},websock.onclose=function(e){console.log("websock close"),$("#conStatus").removeClass("color-green"),$("#conStatus").addClass("color-red"),$("#conStatus").text("Error / No Connection")},websock.onerror=function(e){console.log(e),$("#conStatus").removeClass("color-green"),$("#conStatus").addClass("color-red"),$("#conStatus").text("Error / No Connection")},websock.onmessage=function(e){console.log(e);var c=JSON.parse(e.data),o=(document.body,"");switch(c.type){case UI_TITEL:document.title=c.label,$("#mainHeader").html(c.label);break;case UI_LABEL:$("#row").append("
"+c.label+"

"+c.value+"
");break;case UI_BUTTON:$("#row").append("
"+c.label+"

"),$("#"+c.id).on({touchstart:function(e){e.preventDefault(),buttonclick(c.id,!0)}}),$("#"+c.id).on({touchend:function(e){e.preventDefault(),buttonclick(c.id,!1)}});break;case UI_SWITCHER:var s="