From b71844eac08dfe3051e4c4d305cb8f32d07e624e Mon Sep 17 00:00:00 2001 From: Nic Limper Date: Tue, 30 Jan 2024 11:23:58 +0100 Subject: [PATCH] imgupload performance / Norway dayahead codes --- ESP32_AP-Flasher/src/contentmanager.cpp | 2 +- ESP32_AP-Flasher/src/web.cpp | 72 ++++++++++++++++----- ESP32_AP-Flasher/wwwroot/content_cards.json | 5 ++ 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/ESP32_AP-Flasher/src/contentmanager.cpp b/ESP32_AP-Flasher/src/contentmanager.cpp index 49d48b99..d31ac614 100644 --- a/ESP32_AP-Flasher/src/contentmanager.cpp +++ b/ESP32_AP-Flasher/src/contentmanager.cpp @@ -1462,7 +1462,7 @@ bool getDayAheadFeed(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, getTemplate(loc, 27, taginfo->hwType); // This is a link to a Google Apps Script script, which fetches (and caches) the tariff from https://transparency.entsoe.eu/ - // Please don't use this link in any other projects. + // I made it available to provide easy access to the data, but please don't use this link in any projects other than OpenEpaperLink. String URL = "https://script.google.com/macros/s/AKfycbwMmeGAaPrWzVZrESSpmPmD--O132PzW_acnBsuEottKNATTqCRn6h8zN0Yts7S56ggsg/exec?country=" + cfgobj["country"].as(); time_t now; diff --git a/ESP32_AP-Flasher/src/web.cpp b/ESP32_AP-Flasher/src/web.cpp index 383f0f80..4536aa2b 100644 --- a/ESP32_AP-Flasher/src/web.cpp +++ b/ESP32_AP-Flasher/src/web.cpp @@ -693,40 +693,77 @@ void init_web() { server.begin(); } +#define UPLOAD_BUFFER_SIZE 32768 + +struct UploadInfo { + String filename; + uint8_t buffer[UPLOAD_BUFFER_SIZE]; + size_t bufferSize; +}; + void doImageUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { - Serial.println("upload part " + String(index)); String uploadfilename; if (!index) { if (config.runStatus != RUNSTATUS_RUN) { request->send(409, "text/plain", "Come back later"); return; } - if (request->hasParam("mac", true)) { - uploadfilename = request->getParam("mac", true)->value() + "_" + String(millis()) + ".jpg"; - } else { + if (!request->hasParam("mac", true)) { request->send(400, "text/plain", "parameters incomplete"); return; } logLine("http imageUpload " + uploadfilename); - request->_tempObject = (void *)new String(uploadfilename); + + uploadfilename = request->getParam("mac", true)->value() + "_" + String(millis()) + ".jpg"; + File file = contentFS->open("/temp/" + uploadfilename, "w"); + file.close(); + Serial.println("upload started " + uploadfilename); + + UploadInfo *uploadInfo = new UploadInfo{uploadfilename, {}, 0}; + request->_tempObject = (void *)uploadInfo; } - String *savedFilename = static_cast(request->_tempObject); - if (savedFilename != nullptr) { - uploadfilename = *savedFilename; + + UploadInfo *uploadInfo = static_cast(request->_tempObject); + + if (uploadInfo != nullptr) { + uploadfilename = uploadInfo->filename; + if (len) { - xSemaphoreTake(fsMutex, portMAX_DELAY); - File file = contentFS->open("/temp/" + uploadfilename, "a"); - if (file) { - file.seek(index); - file.write(data, len); - file.close(); + if (uploadInfo->bufferSize + len <= UPLOAD_BUFFER_SIZE) { + memcpy(&uploadInfo->buffer[uploadInfo->bufferSize], data, len); + uploadInfo->bufferSize += len; } else { - logLine("Failed to open file for appending: " + uploadfilename); + xSemaphoreTake(fsMutex, portMAX_DELAY); + File file = contentFS->open("/temp/" + uploadfilename, "a"); + if (file) { + file.write(uploadInfo->buffer, uploadInfo->bufferSize); + file.close(); + uploadInfo->bufferSize = 0; + } else { + logLine("Failed to open file for appending: " + uploadfilename); + } + xSemaphoreGive(fsMutex); + + memcpy(uploadInfo->buffer, data, len); + uploadInfo->bufferSize = len; } - xSemaphoreGive(fsMutex); } + if (final) { - Serial.println("upload final"); + if (uploadInfo->bufferSize > 0) { + xSemaphoreTake(fsMutex, portMAX_DELAY); + File file = contentFS->open("/temp/" + uploadfilename, "a"); + if (file) { + file.write(uploadInfo->buffer, uploadInfo->bufferSize); + file.close(); + } else { + logLine("Failed to open file for appending: " + uploadfilename); + } + xSemaphoreGive(fsMutex); + request->_tempObject = nullptr; + delete uploadInfo; + } + if (request->hasParam("mac", true)) { String dst = request->getParam("mac", true)->value(); uint8_t mac[8]; @@ -757,6 +794,7 @@ void doImageUpload(AsyncWebServerRequest *request, String filename, size_t index } else { taginfo->contentMode = 24; } + Serial.println("upload finished " + uploadfilename); taginfo->nextupdate = 0; wsSendTaginfo(mac, SYNC_USERCFG); request->send(200, "text/plain", "Ok, saved"); diff --git a/ESP32_AP-Flasher/wwwroot/content_cards.json b/ESP32_AP-Flasher/wwwroot/content_cards.json index 331c4926..1597616d 100644 --- a/ESP32_AP-Flasher/wwwroot/content_cards.json +++ b/ESP32_AP-Flasher/wwwroot/content_cards.json @@ -304,6 +304,11 @@ "LT": "Lithuania", "LV": "Latvia", "NL": "Netherlands", + "NO1": "Norway NO1", + "NO2": "Norway NO2", + "NO3": "Norway NO3", + "NO4": "Norway NO4", + "NO5": "Norway NO5", "PL": "Poland", "RO": "Romania", "SI": "Slovenia",