From 9f55d72f97d8af9e630a1419c88e69531840bd6b Mon Sep 17 00:00:00 2001 From: Nic Limper Date: Sat, 30 Dec 2023 12:30:07 +0100 Subject: [PATCH] various small fixes - prevent using html file for tag firmware update - removed excessive logging - fallback to .bak on tagDB load error - scheduled reboot once at night around 4:00 --- ESP32_AP-Flasher/include/tag_db.h | 2 +- ESP32_AP-Flasher/include/util.h | 4 ++-- ESP32_AP-Flasher/src/contentmanager.cpp | 25 ++++++++++++++++-------- ESP32_AP-Flasher/src/main.cpp | 21 ++++---------------- ESP32_AP-Flasher/src/ota.cpp | 1 - ESP32_AP-Flasher/src/tag_db.cpp | 12 +++++++++--- ESP32_AP-Flasher/src/web.cpp | 26 ++++++++++++++++++++++--- 7 files changed, 56 insertions(+), 35 deletions(-) diff --git a/ESP32_AP-Flasher/include/tag_db.h b/ESP32_AP-Flasher/include/tag_db.h index 47615a8a..aa58c20b 100644 --- a/ESP32_AP-Flasher/include/tag_db.h +++ b/ESP32_AP-Flasher/include/tag_db.h @@ -99,7 +99,7 @@ extern String tagDBtoJson(const uint8_t mac[8] = nullptr, uint8_t startPos = 0); extern bool deleteRecord(const uint8_t mac[8]); extern void fillNode(JsonObject& tag, const tagRecord* taginfo); extern void saveDB(const String& filename); -extern void loadDB(const String& filename); +extern bool loadDB(const String& filename); extern void destroyDB(); extern uint32_t getTagCount(); extern uint32_t getTagCount(uint32_t& timeoutcount); diff --git a/ESP32_AP-Flasher/include/util.h b/ESP32_AP-Flasher/include/util.h index 49a1c1e4..388de15e 100644 --- a/ESP32_AP-Flasher/include/util.h +++ b/ESP32_AP-Flasher/include/util.h @@ -72,14 +72,14 @@ static void printLargestFreeBlock() { /// @return True on success, false on error (httpCode != 200 || deserialization error) static bool httpGetJson(String &url, JsonDocument &json, const uint16_t timeout, JsonDocument *filter = nullptr) { HTTPClient http; - logLine("http httpGetJson " + url); + // logLine("http httpGetJson " + url); http.begin(url); http.setTimeout(timeout); http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); const int httpCode = http.GET(); if (httpCode != 200) { http.end(); - wsErr(String("[httpGetJson] http code") + httpCode); + wsErr(String("[httpGetJson] http ") + url + " code " + httpCode); return false; } diff --git a/ESP32_AP-Flasher/src/contentmanager.cpp b/ESP32_AP-Flasher/src/contentmanager.cpp index 82e79115..d7505fae 100644 --- a/ESP32_AP-Flasher/src/contentmanager.cpp +++ b/ESP32_AP-Flasher/src/contentmanager.cpp @@ -312,10 +312,19 @@ void drawNew(const uint8_t mac[8], const bool buttonPressed, tagRecord *&taginfo filename = cfgobj["filename"].as(); if (!util::isEmptyOrNull(filename) && !cfgobj["#fetched"].as()) { - if (prepareDataAvail(filename, DATATYPE_FW_UPDATE, 0, mac, cfgobj["timetolive"].as())) { - cfgobj["#fetched"] = true; - } else { - wsErr("Error accessing " + filename); + + File file = contentFS->open(filename, "r"); + if (file) { + if (file.find("())) { + cfgobj["#fetched"] = true; + } + } } cfgobj["filename"] = ""; taginfo->nextupdate = 3216153600; @@ -975,7 +984,7 @@ char *epoch_to_display(time_t utc) { bool getCalFeed(String &filename, String URL, String title, tagRecord *&taginfo, imgParam &imageParams) { #ifdef CONTENT_CAL // google apps scripts method to retrieve calendar - // see /data/calendar.txt for description + // see https://github.com/jjwbruijn/OpenEPaperLink/wiki/Google-Apps-Scripts for description wsLog("get calendar"); @@ -987,13 +996,13 @@ bool getCalFeed(String &filename, String URL, String title, tagRecord *&taginfo, strftime(dateString, sizeof(dateString), "%d.%m.%Y", &timeinfo); HTTPClient http; - logLine("http getCalFeed " + URL); + // logLine("http getCalFeed " + URL); http.begin(URL); http.setTimeout(10000); http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); int httpCode = http.GET(); if (httpCode != 200) { - wsErr("http error " + String(httpCode)); + wsErr("getCalFeed http error " + String(httpCode)); return false; } @@ -1090,7 +1099,7 @@ uint8_t drawBuienradar(String &filename, JsonObject &cfgobj, tagRecord *&taginfo String lat = cfgobj["#lat"]; String lon = cfgobj["#lon"]; - logLine("http drawBuienradar"); + // logLine("http drawBuienradar"); http.begin("https://gps.buienradar.nl/getrr.php?lat=" + lat + "&lon=" + lon); http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); http.setTimeout(5000); diff --git a/ESP32_AP-Flasher/src/main.cpp b/ESP32_AP-Flasher/src/main.cpp index 11ae17b6..327f08be 100644 --- a/ESP32_AP-Flasher/src/main.cpp +++ b/ESP32_AP-Flasher/src/main.cpp @@ -27,7 +27,6 @@ util::Timer intervalContentRunner(seconds(1)); util::Timer intervalSysinfo(seconds(3)); util::Timer intervalVars(seconds(10)); util::Timer intervalSaveDB(minutes(5)); -util::Timer intervalCheckDate(minutes(5)); #ifdef OPENEPAPERLINK_PCB util::Timer tagConnectTimer(seconds(1)); @@ -129,7 +128,10 @@ void setup() { rgbIdle(); #endif TagData::loadParsers("/parsers.json"); - loadDB("/current/tagDB.json"); + if (!loadDB("/current/tagDB.json")) { + Serial.println("unable to load tagDB, reverting to backup"); + loadDB("/current/tagDB.json.bak"); + } cleanupCurrent(); xTaskCreate(APTask, "AP Process", 6000, NULL, 2, NULL); vTaskDelay(10 / portTICK_PERIOD_MS); @@ -163,21 +165,6 @@ void loop() { if (intervalContentRunner.doRun() && apInfo.state == AP_STATE_ONLINE) { contentRunner(); } - if (intervalCheckDate.doRun() && config.runStatus == RUNSTATUS_RUN) { - static uint8_t day = 0; - - time_t now; - time(&now); - struct tm timedef; - localtime_r(&now, &timedef); - - if (day != timedef.tm_mday) { - day = timedef.tm_mday; - char timeBuffer[80]; - strftime(timeBuffer, sizeof(timeBuffer), "%d-%m-%Y", &timedef); - setVarDB("ap_date", timeBuffer); - } - } #ifdef YELLOW_IPS_AP extern void yellow_ap_display_loop(void); diff --git a/ESP32_AP-Flasher/src/ota.cpp b/ESP32_AP-Flasher/src/ota.cpp index df2f3f5f..b81fdcd1 100644 --- a/ESP32_AP-Flasher/src/ota.cpp +++ b/ESP32_AP-Flasher/src/ota.cpp @@ -213,7 +213,6 @@ void updateFirmware(const char* url, const char* expectedMd5, const size_t size) } httpClient.end(); - // loadDB("/current/tagDB.json"); config.runStatus = RUNSTATUS_RUN; } diff --git a/ESP32_AP-Flasher/src/tag_db.cpp b/ESP32_AP-Flasher/src/tag_db.cpp index b9ed81bb..e54d87a8 100644 --- a/ESP32_AP-Flasher/src/tag_db.cpp +++ b/ESP32_AP-Flasher/src/tag_db.cpp @@ -164,15 +164,15 @@ void saveDB(const String& filename) { Serial.println("DB saved " + String(millis() - t) + "ms"); } -void loadDB(const String& filename) { - Serial.println("reading DB from file"); +bool loadDB(const String& filename) { + Serial.println("reading DB from " + String(filename)); const long t = millis(); Storage.begin(); fs::File readfile = contentFS->open(filename, "r"); if (!readfile) { Serial.println("loadDB: Failed to open file"); - return; + return false; } time_t now; @@ -230,13 +230,19 @@ void loadDB(const String& filename) { Serial.print(F("deserializeJson() failed: ")); Serial.println(err.c_str()); parsing = false; + readfile.close(); + return false; } parsing = parsing && readfile.find(","); } + } else { + readfile.close(); + return false; } readfile.close(); Serial.println("loadDB took " + String(millis() - t) + "ms"); + return true; } void destroyDB() { diff --git a/ESP32_AP-Flasher/src/web.cpp b/ESP32_AP-Flasher/src/web.cpp index fd0fef67..cc22fe19 100644 --- a/ESP32_AP-Flasher/src/web.cpp +++ b/ESP32_AP-Flasher/src/web.cpp @@ -69,10 +69,12 @@ void wsSendSysteminfo() { static int freeSpaceLastRun = 0; static size_t tagDBsize = 0; static uint64_t freeSpace = Storage.freeSpace(); + sys["currtime"] = now; sys["heap"] = ESP.getFreeHeap(); sys["recordcount"] = tagDBsize; sys["dbsize"] = dbSize(); + if (millis() - freeSpaceLastRun > 30000 || freeSpaceLastRun == 0) { freeSpace = Storage.freeSpace(); tagDBsize = tagDB.size(); @@ -86,16 +88,34 @@ void wsSendSysteminfo() { sys["apstate"] = apInfo.state; sys["runstate"] = config.runStatus; -#if !defined(CONFIG_IDF_TARGET_ESP32) - // sys["temp"] = temperatureRead(); -#endif sys["rssi"] = WiFi.RSSI(); sys["wifistatus"] = WiFi.status(); sys["wifissid"] = WiFi.SSID(); + static uint8_t day = 0; + struct tm timeinfo; + localtime_r(&now, &timeinfo); + + if (day != timeinfo.tm_mday) { + day = timeinfo.tm_mday; + char timeBuffer[80]; + strftime(timeBuffer, sizeof(timeBuffer), "%d-%m-%Y", &timeinfo); + setVarDB("ap_date", timeBuffer); + } setVarDB("ap_ip", WiFi.localIP().toString()); setVarDB("ap_ch", String(apInfo.channel)); + // reboot once at night + if (timeinfo.tm_hour == 4 && timeinfo.tm_min == 0 && millis() > 2 * 3600 * 1000) { + logLine("Nightly reboot"); + ws.enable(false); + refreshAllPending(); + saveDB("/current/tagDB.json"); + ws.closeAll(); + delay(100); + ESP.restart(); + } + static uint32_t tagcounttimer = 0; if (millis() - tagcounttimer > 60000 || tagcounttimer == 0) { uint32_t timeoutcount = 0;