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
This commit is contained in:
Nic Limper
2023-12-30 12:30:07 +01:00
parent 3621d4b6e1
commit 9f55d72f97
7 changed files with 56 additions and 35 deletions

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -312,10 +312,19 @@ void drawNew(const uint8_t mac[8], const bool buttonPressed, tagRecord *&taginfo
filename = cfgobj["filename"].as<String>();
if (!util::isEmptyOrNull(filename) && !cfgobj["#fetched"].as<bool>()) {
if (prepareDataAvail(filename, DATATYPE_FW_UPDATE, 0, mac, cfgobj["timetolive"].as<int>())) {
cfgobj["#fetched"] = true;
} else {
wsErr("Error accessing " + filename);
File file = contentFS->open(filename, "r");
if (file) {
if (file.find("<html")) {
file.close();
wsErr("User error flashing tag firmware: this is a html-file!");
cfgobj["#fetched"] = true;
} else {
file.close();
if (prepareDataAvail(filename, DATATYPE_FW_UPDATE, 0, mac, cfgobj["timetolive"].as<int>())) {
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);

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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() {

View File

@@ -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;