From 1ca83509dd6fe1ebd077b60da9426b8447aa30e6 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sun, 20 Dec 2020 11:32:49 -0800 Subject: [PATCH 1/8] Blink the LED for one second on get of /json/blink --- src/meshwifi/meshhttp.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 23cbf58c..d3e1c9bb 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -61,6 +61,7 @@ void handle404(HTTPRequest *req, HTTPResponse *res); void handleFormUpload(HTTPRequest *req, HTTPResponse *res); void handleScanNetworks(HTTPRequest *req, HTTPResponse *res); void handleSpiffsBrowseStatic(HTTPRequest *req, HTTPResponse *res); +void handleBlinkLED(HTTPRequest *req, HTTPResponse *res); void middlewareSpeedUp240(HTTPRequest *req, HTTPResponse *res, std::function next); void middlewareSpeedUp160(HTTPRequest *req, HTTPResponse *res, std::function next); @@ -243,6 +244,7 @@ void initWebServer() ResourceNode *node404 = new ResourceNode("", "GET", &handle404); ResourceNode *nodeFormUpload = new ResourceNode("/upload", "POST", &handleFormUpload); ResourceNode *nodeJsonScanNetworks = new ResourceNode("/json/scanNetworks", "GET", &handleScanNetworks); + ResourceNode *nodeJsonBlinkLED = new ResourceNode("/json/blink", "GET", &handleBlinkLED); ResourceNode *nodeJsonSpiffsBrowseStatic = new ResourceNode("/json/spiffs/browse/static/", "GET", &handleSpiffsBrowseStatic); // Secure nodes @@ -258,6 +260,7 @@ void initWebServer() secureServer->registerNode(nodeRestart); secureServer->registerNode(nodeFormUpload); secureServer->registerNode(nodeJsonScanNetworks); + secureServer->registerNode(nodeJsonBlinkLED); secureServer->registerNode(nodeJsonSpiffsBrowseStatic); secureServer->setDefaultNode(node404); @@ -276,6 +279,7 @@ void initWebServer() insecureServer->registerNode(nodeRestart); insecureServer->registerNode(nodeFormUpload); insecureServer->registerNode(nodeJsonScanNetworks); + insecureServer->registerNode(nodeJsonBlinkLED); insecureServer->registerNode(nodeJsonSpiffsBrowseStatic); insecureServer->setDefaultNode(node404); @@ -970,6 +974,28 @@ void handleRestart(HTTPRequest *req, HTTPResponse *res) ESP.restart(); } +void handleBlinkLED(HTTPRequest *req, HTTPResponse *res) +{ + res->setHeader("Content-Type", "application/json"); + + // This can be cleaned up at some point to make it non-blocking and to allow for more configuration. + + res->println("{"); + res->println("\"status\": \"ok\""); + res->println("}"); + + uint8_t count = 50; + + while (count > 0) + { + setLed(true); + delay(10); + setLed(false); + delay(10); + count = count - 1; + } +} + void handleScanNetworks(HTTPRequest *req, HTTPResponse *res) { res->setHeader("Content-Type", "application/json"); From 7205e9a5b4f6c4174ba50077962ca64a40456c96 Mon Sep 17 00:00:00 2001 From: Charles Crossan Date: Sun, 20 Dec 2020 14:50:13 -0500 Subject: [PATCH 2/8] adjust LED timings; switch to HTTP/POST --- src/meshwifi/meshhttp.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index d3e1c9bb..47d6e6d1 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -244,7 +244,7 @@ void initWebServer() ResourceNode *node404 = new ResourceNode("", "GET", &handle404); ResourceNode *nodeFormUpload = new ResourceNode("/upload", "POST", &handleFormUpload); ResourceNode *nodeJsonScanNetworks = new ResourceNode("/json/scanNetworks", "GET", &handleScanNetworks); - ResourceNode *nodeJsonBlinkLED = new ResourceNode("/json/blink", "GET", &handleBlinkLED); + ResourceNode *nodeJsonBlinkLED = new ResourceNode("/json/blink", "POST", &handleBlinkLED); ResourceNode *nodeJsonSpiffsBrowseStatic = new ResourceNode("/json/spiffs/browse/static/", "GET", &handleSpiffsBrowseStatic); // Secure nodes @@ -984,14 +984,14 @@ void handleBlinkLED(HTTPRequest *req, HTTPResponse *res) res->println("\"status\": \"ok\""); res->println("}"); - uint8_t count = 50; + uint8_t count = 10; while (count > 0) { setLed(true); - delay(10); + delay(50); setLed(false); - delay(10); + delay(50); count = count - 1; } } From db2193b52612eb9542e573520168afdd0e9c3383 Mon Sep 17 00:00:00 2001 From: Charles Crossan Date: Sun, 20 Dec 2020 17:45:45 -0500 Subject: [PATCH 3/8] implement screen blink --- src/graphics/Screen.cpp | 21 +++++++++++++++++++++ src/graphics/Screen.h | 2 ++ src/meshwifi/meshhttp.cpp | 6 ++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 229aa63a..e9cfe89a 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -894,6 +894,27 @@ void Screen::handleStartBluetoothPinScreen(uint32_t pin) setFastFramerate(); } +void Screen::blink() { + setFastFramerate(); + uint8_t count = 10; + uint8_t blinker = 0; + + dispdev.setBrightness(254); + + while(count>0) { + if (blinker == 254) { + blinker = 0; + count--; + } else { + blinker++; + } + int width = blinker / (254.00 / SCREEN_WIDTH); + dispdev.fillRect(0, 0, width, SCREEN_HEIGHT); + dispdev.display(); + } + dispdev.setBrightness(brightness); +} + void Screen::handlePrint(const char *text) { DEBUG_MSG("Screen: %s", text); diff --git a/src/graphics/Screen.h b/src/graphics/Screen.h index 90e8ad08..888f3101 100644 --- a/src/graphics/Screen.h +++ b/src/graphics/Screen.h @@ -107,6 +107,8 @@ class Screen : public concurrency::OSThread */ void doDeepSleep(); + void blink(); + /// Handles a button press. void onPress() { enqueueCmd(ScreenCmd{.cmd = Cmd::ON_PRESS}); } diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 47d6e6d1..50a1ac32 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -986,14 +986,16 @@ void handleBlinkLED(HTTPRequest *req, HTTPResponse *res) uint8_t count = 10; - while (count > 0) + /*while (count > 0) { setLed(true); delay(50); setLed(false); delay(50); count = count - 1; - } + }*/ + + screen->blink(); } void handleScanNetworks(HTTPRequest *req, HTTPResponse *res) From 2f779bfd3705d6d0d8b06935355534e4446b3e77 Mon Sep 17 00:00:00 2001 From: Charles Crossan Date: Sun, 20 Dec 2020 18:24:48 -0500 Subject: [PATCH 4/8] improve blink; LED or SCREEN as POST Parameter --- src/graphics/Screen.cpp | 17 +++++-------- src/meshwifi/meshhttp.cpp | 51 ++++++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index e9cfe89a..3f98969d 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -897,20 +897,15 @@ void Screen::handleStartBluetoothPinScreen(uint32_t pin) void Screen::blink() { setFastFramerate(); uint8_t count = 10; - uint8_t blinker = 0; - dispdev.setBrightness(254); - while(count>0) { - if (blinker == 254) { - blinker = 0; - count--; - } else { - blinker++; - } - int width = blinker / (254.00 / SCREEN_WIDTH); - dispdev.fillRect(0, 0, width, SCREEN_HEIGHT); + dispdev.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); dispdev.display(); + delay(50); + dispdev.clear(); + dispdev.display(); + delay(50); + count = count -1; } dispdev.setBrightness(brightness); } diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 50a1ac32..6190b045 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -979,23 +979,46 @@ void handleBlinkLED(HTTPRequest *req, HTTPResponse *res) res->setHeader("Content-Type", "application/json"); // This can be cleaned up at some point to make it non-blocking and to allow for more configuration. + std::string contentType = req->getHeader("Content-Type"); + std::string blink_target; + HTTPBodyParser *parser; + + + if (contentType.rfind("multipart/form-data",0) == 0) { + // If a body was submitted to /blink, then figure out whether the user + // watned to blink the LED or the screen + parser = new HTTPMultipartBodyParser(req); + while (parser->nextField()) { + std::string name = parser->getFieldName(); + if (name == "blink_target") { + char buf[512]; + size_t readLength = parser->read((byte *)buf, 512); + // filename = std::string("/public/") + std::string(buf, readLength); + blink_target = std::string(buf, readLength); + } + } + } else { + blink_target = "LED"; + } + + if (blink_target == "LED" ) { + uint8_t count = 10; + while (count > 0) + { + setLed(true); + delay(50); + setLed(false); + delay(50); + count = count - 1; + } + } + else { + screen->blink(); + } res->println("{"); - res->println("\"status\": \"ok\""); + res->println("\"status\": \"Blink completed: LED\""); res->println("}"); - - uint8_t count = 10; - - /*while (count > 0) - { - setLed(true); - delay(50); - setLed(false); - delay(50); - count = count - 1; - }*/ - - screen->blink(); } void handleScanNetworks(HTTPRequest *req, HTTPResponse *res) From 2743b9d3100944c86f9174c16ce841aca237f974 Mon Sep 17 00:00:00 2001 From: Charles Crossan Date: Sun, 20 Dec 2020 21:44:51 -0500 Subject: [PATCH 5/8] use POST URL parameters; fix response status --- src/meshwifi/meshhttp.cpp | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 6190b045..98da0dee 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -978,26 +978,14 @@ void handleBlinkLED(HTTPRequest *req, HTTPResponse *res) { res->setHeader("Content-Type", "application/json"); - // This can be cleaned up at some point to make it non-blocking and to allow for more configuration. - std::string contentType = req->getHeader("Content-Type"); + ResourceParameters *params = req->getParams(); std::string blink_target; HTTPBodyParser *parser; - if (contentType.rfind("multipart/form-data",0) == 0) { - // If a body was submitted to /blink, then figure out whether the user - // watned to blink the LED or the screen - parser = new HTTPMultipartBodyParser(req); - while (parser->nextField()) { - std::string name = parser->getFieldName(); - if (name == "blink_target") { - char buf[512]; - size_t readLength = parser->read((byte *)buf, 512); - // filename = std::string("/public/") + std::string(buf, readLength); - blink_target = std::string(buf, readLength); - } - } - } else { + if (! params->getQueryParameter("blink_target", blink_target)) { + // if no blink_target was supplied in the URL parameters of the + // POST request, then assume we should blink the LED blink_target = "LED"; } @@ -1017,7 +1005,7 @@ void handleBlinkLED(HTTPRequest *req, HTTPResponse *res) } res->println("{"); - res->println("\"status\": \"Blink completed: LED\""); + res->println("\"status\": \"ok\""); res->println("}"); } From dcb9125b32cf3dffc72b01571b56bd1369a3307e Mon Sep 17 00:00:00 2001 From: Charles Crossan Date: Sun, 20 Dec 2020 21:47:23 -0500 Subject: [PATCH 6/8] remove unused parser --- src/meshwifi/meshhttp.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 98da0dee..87302816 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -979,9 +979,7 @@ void handleBlinkLED(HTTPRequest *req, HTTPResponse *res) res->setHeader("Content-Type", "application/json"); ResourceParameters *params = req->getParams(); - std::string blink_target; - HTTPBodyParser *parser; - + std::string blink_target; if (! params->getQueryParameter("blink_target", blink_target)) { // if no blink_target was supplied in the URL parameters of the From 7f59e76c72cd229ca233384c7414c3310bc384dd Mon Sep 17 00:00:00 2001 From: Charles Crossan Date: Sun, 20 Dec 2020 21:47:46 -0500 Subject: [PATCH 7/8] fix formatting --- src/meshwifi/meshhttp.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 87302816..1af53112 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -979,27 +979,25 @@ void handleBlinkLED(HTTPRequest *req, HTTPResponse *res) res->setHeader("Content-Type", "application/json"); ResourceParameters *params = req->getParams(); - std::string blink_target; + std::string blink_target; - if (! params->getQueryParameter("blink_target", blink_target)) { - // if no blink_target was supplied in the URL parameters of the + if (!params->getQueryParameter("blink_target", blink_target)) { + // if no blink_target was supplied in the URL parameters of the // POST request, then assume we should blink the LED - blink_target = "LED"; + blink_target = "LED"; } - if (blink_target == "LED" ) { + if (blink_target == "LED") { uint8_t count = 10; - while (count > 0) - { + while (count > 0) { setLed(true); delay(50); setLed(false); delay(50); count = count - 1; } - } - else { - screen->blink(); + } else { + screen->blink(); } res->println("{"); From ef0891ae5dbcf0b5d0470422c8003f09b66cf7bd Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sun, 20 Dec 2020 20:09:17 -0800 Subject: [PATCH 8/8] Fix for #576 - The browser was seeing the other files on the filesystem. --- src/meshwifi/meshhttp.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 1af53112..ebff1e7b 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -405,16 +405,16 @@ void handleSpiffsBrowseStatic(HTTPRequest *req, HTTPResponse *res) res->print("\"files\": ["); bool firstFile = 1; while (file) { - if (firstFile) { - firstFile = 0; - } else { - res->println(","); - } - - res->println("{"); - String filePath = String(file.name()); if (filePath.indexOf("/static") == 0) { + if (firstFile) { + firstFile = 0; + } else { + res->println(","); + } + + res->println("{"); + if (String(file.name()).substring(1).endsWith(".gz")) { String modifiedFile = String(file.name()).substring(1); modifiedFile.remove((modifiedFile.length() - 3), 3); @@ -425,10 +425,10 @@ void handleSpiffsBrowseStatic(HTTPRequest *req, HTTPResponse *res) res->print("\"name\": \"" + String(file.name()).substring(1) + "\","); } res->print("\"size\": " + String(file.size())); + res->print("}"); } file = root.openNextFile(); - res->print("}"); } res->print("],"); res->print("\"filesystem\" : {");