diff --git a/ESP32_AP-Flasher/include/contentmanager.h b/ESP32_AP-Flasher/include/contentmanager.h index 61fe7940..50cd73c2 100644 --- a/ESP32_AP-Flasher/include/contentmanager.h +++ b/ESP32_AP-Flasher/include/contentmanager.h @@ -30,8 +30,9 @@ bool getRssFeed(String &filename, String URL, String title, tagRecord *&taginfo, bool getCalFeed(String &filename, String URL, String title, tagRecord *&taginfo, imgParam &imageParams); void drawQR(String &filename, String qrcontent, String title, tagRecord *&taginfo, imgParam &imageParams); uint8_t drawBuienradar(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgParam &imageParams); -int getJsonTemplate(String URL, JsonDocument &jsondoc, time_t fetched, String MAC); -void drawJsonTemplate(JsonDocument &doc, String filename, tagRecord *&taginfo, imgParam &imageParams); +int getJsonTemplateFile(String &filename, String jsonfile, tagRecord *&taginfo, imgParam &imageParams); +int getJsonTemplateUrl(String &filename, String URL, time_t fetched, String MAC, tagRecord *&taginfo, imgParam &imageParams); +void drawJsonStream(Stream &stream, String &filename, tagRecord *&taginfo, imgParam &imageParams); void drawElement(const JsonObject &element, TFT_eSprite &spr); uint16_t getColor(uint8_t color); char *formatHttpDate(time_t t); diff --git a/ESP32_AP-Flasher/src/contentmanager.cpp b/ESP32_AP-Flasher/src/contentmanager.cpp index 0e1db2a7..cd29cb08 100644 --- a/ESP32_AP-Flasher/src/contentmanager.cpp +++ b/ESP32_AP-Flasher/src/contentmanager.cpp @@ -310,32 +310,24 @@ void drawNew(uint8_t mac[8], bool buttonPressed, tagRecord *&taginfo) { case 19: // json template { - DynamicJsonDocument doc(5000); if (cfgobj["filename"]) { - File file = contentFS->open("/" + String(hexmac) + ".json", "r"); - if (file) { - DeserializationError error = deserializeJson(doc, file); - if (error) { - wsErr(error.c_str()); - } else { - taginfo->nextupdate = 3216153600; - drawJsonTemplate(doc, filename, taginfo, imageParams); - updateTagImage(filename, mac, 0, taginfo, imageParams); - } + int result = getJsonTemplateFile(filename, cfgobj["filename"], taginfo, imageParams); + if (result) { + updateTagImage(filename, mac, 0, taginfo, imageParams); } else { - wsErr("json file not found"); + wsErr("error opening file " + cfgobj["filename"].as()); } + taginfo->nextupdate = 3216153600; } else { - int httpcode = getJsonTemplate(cfgobj["url"], doc, (time_t)cfgobj["#fetched"], String(hexmac)); + int httpcode = getJsonTemplateUrl(filename, cfgobj["url"], (time_t)cfgobj["#fetched"], String(hexmac), taginfo, imageParams); if (httpcode == 200) { taginfo->nextupdate = now + 60 * (cfgobj["interval"].as() < 3 ? 15 : cfgobj["interval"].as()); - drawJsonTemplate(doc, filename, taginfo, imageParams); updateTagImage(filename, mac, cfgobj["interval"].as(), taginfo, imageParams); cfgobj["#fetched"] = now; } else if (httpcode == 304) { taginfo->nextupdate = now + 60 * (cfgobj["interval"].as() < 3 ? 15 : cfgobj["interval"].as()); } else { - taginfo->nextupdate = now + 300; + taginfo->nextupdate = now + 600; } } break; @@ -966,8 +958,23 @@ uint8_t drawBuienradar(String &filename, JsonObject &cfgobj, tagRecord *&taginfo return refresh; } -int getJsonTemplate(String URL, JsonDocument &jsondoc, time_t fetched, String MAC) { +int getJsonTemplateFile(String &filename, String jsonfile, tagRecord *&taginfo, imgParam &imageParams) { + if (jsonfile.c_str()[0] != '/') { + jsonfile = "/" + jsonfile; + } + File file = contentFS->open(jsonfile, "r"); + if (file) { + drawJsonStream(file, filename, taginfo, imageParams); + file.close(); + contentFS->remove(jsonfile); + return 1; + } + return 0; +} + +int getJsonTemplateUrl(String &filename, String URL, time_t fetched, String MAC, tagRecord *&taginfo, imgParam &imageParams) { Serial.println("get external " + URL); + HTTPClient http; http.useHTTP10(true); http.begin(URL); @@ -977,11 +984,7 @@ int getJsonTemplate(String URL, JsonDocument &jsondoc, time_t fetched, String MA http.setTimeout(5000); int httpCode = http.GET(); if (httpCode == 200) { - DeserializationError error = deserializeJson(jsondoc, http.getStream()); - if (error) { - wsErr("json error " + String(error.c_str())); - return 0; - } + drawJsonStream(http.getStream(), filename, taginfo, imageParams); } else { if (httpCode != 304) { wsErr("http " + URL + " status " + String(httpCode)); @@ -993,14 +996,25 @@ int getJsonTemplate(String URL, JsonDocument &jsondoc, time_t fetched, String MA return httpCode; } -void drawJsonTemplate(JsonDocument &doc, String filename, tagRecord *&taginfo, imgParam &imageParams) { +void drawJsonStream(Stream &stream, String &filename, tagRecord *&taginfo, imgParam &imageParams) { TFT_eSPI tft = TFT_eSPI(); TFT_eSprite spr = TFT_eSprite(&tft); initSprite(spr, hwdata[taginfo->hwType].width, hwdata[taginfo->hwType].height, imageParams); + DynamicJsonDocument doc(300); - for (const JsonVariant &element : doc.as()) { - drawElement(element.as(), spr); + if (stream.find("[")) { + do { + DeserializationError error = deserializeJson(doc, stream); + if (error) { + wsErr("json error " + String(error.c_str())); + break; + } else { + drawElement(doc.as(), spr); + doc.clear(); + } + } while (stream.findUntil(",", "]")); } + spr2buffer(spr, filename, imageParams); spr.deleteSprite(); } diff --git a/ESP32_AP-Flasher/src/web.cpp b/ESP32_AP-Flasher/src/web.cpp index dd3c006f..744bed06 100644 --- a/ESP32_AP-Flasher/src/web.cpp +++ b/ESP32_AP-Flasher/src/web.cpp @@ -530,7 +530,7 @@ void doJsonUpload(AsyncWebServerRequest *request) { tagRecord *taginfo = nullptr; taginfo = tagRecord::findByMAC(mac); if (taginfo != nullptr) { - taginfo->modeConfigJson = "{\"filename\":\"" + dst + ".json\"}"; + taginfo->modeConfigJson = "{\"filename\":\"/" + dst + ".json\"}"; taginfo->contentMode = 19; taginfo->nextupdate = 0; wsSendTaginfo(mac, SYNC_USERCFG);