From d0caee99aaa59710b8b88039b8d86ca9f8f246c3 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Sun, 8 Dec 2024 03:09:28 +0700 Subject: [PATCH] Fix close file after write Better error handling when write and load measurement file comment out spiffs format --- examples/OneOpenAir/OneOpenAir.ino | 4 +-- src/AgConfigure.cpp | 2 +- src/AgValue.cpp | 40 +++++++++++++++++++----------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino index ea17238..ca132d2 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -201,7 +201,7 @@ void setup() { oledDisplay.setText("", "Offline Storage Mode", ""); delay(3000); - mdnsInit(); + // mdnsInit(); localServer.begin(); // Update display and led bar after finishing setup to show dashboard @@ -377,7 +377,7 @@ static void factoryConfigReset(void) { WiFi.disconnect(true, true); /** Reset local config */ - configuration.reset(); + // configuration.reset(); if (ag->isOne()) { oledDisplay.setText("Factory reset", "successful", ""); diff --git a/src/AgConfigure.cpp b/src/AgConfigure.cpp index 57e864b..d3a8c2f 100644 --- a/src/AgConfigure.cpp +++ b/src/AgConfigure.cpp @@ -253,7 +253,7 @@ void Configuration::loadConfig(void) { } file.close(); } else { - SPIFFS.format(); + // SPIFFS.format(); } #endif toConfig(buf); diff --git a/src/AgValue.cpp b/src/AgValue.cpp index ec3bb2c..3bbd4f6 100644 --- a/src/AgValue.cpp +++ b/src/AgValue.cpp @@ -1097,28 +1097,39 @@ bool Measurements::saveLocalStorage(AirGradient &ag, Configuration &config) { file = SPIFFS.open(FILE_PATH, FILE_APPEND, false); } - if (!file) { - Serial.println("Failed local storage file path"); - return false; - } - float pm25 = getCorrectedPM25(ag, config, true); // Save new measurements - file.printf("%s,%.2f,%.2f,%.2f,%.2f,%d,%d,%d,%d,%d\n", ag.getCurrentTime().c_str(), - ag.round2(_pm_03_pc[0].update.avg), ag.round2(pm25), - ag.round2(_temperature[0].update.avg), ag.round2(_humidity[0].update.avg), - (int)round(_co2.update.avg), (int)round(_tvoc.update.avg), - (int)round(_tvoc_raw.update.avg), (int)round(_nox.update.avg), - (int)round(_nox_raw.update.avg)); + char buff[100] = {0}; + sprintf(buff, "%s,%.2f,%.2f,%.2f,%.2f,%d,%d,%d,%d,%d\n\0", ag.getCurrentTime().c_str(), + ag.round2(_pm_03_pc[0].update.avg), ag.round2(pm25), + ag.round2(_temperature[0].update.avg), ag.round2(_humidity[0].update.avg), + (int)round(_co2.update.avg), (int)round(_tvoc.update.avg), + (int)round(_tvoc_raw.update.avg), (int)round(_nox.update.avg), + (int)round(_nox_raw.update.avg)); + + size_t len = strlen(buff); + if (file.write((const uint8_t *)buff, len) != len) { + Serial.println("Write new measurements failed!"); + file.close(); + return false; + } + file.close(); Serial.println("Success save measurements to local storage"); + return true; } char *Measurements::getLocalStorage() { char *buf = nullptr; bool success = false; + + if (!SPIFFS.exists(FILE_PATH)) { + Serial.println("No measurements file exists yet"); + return nullptr; + } + File file = SPIFFS.open(FILE_PATH); if (file && !file.isDirectory()) { // Allocate memory @@ -1134,14 +1145,15 @@ char *Measurements::getLocalStorage() { Serial.println("Reading measurements file: success"); success = true; } + file.close(); - } else { - SPIFFS.format(); } if (!success) { Serial.println("Reading measurements file failed"); - delete buf; + if (buf != nullptr) { + delete buf; + } return nullptr; }