Fix close file after write

Better error handling when write and load measurement file
comment out spiffs format
This commit is contained in:
samuelbles07
2024-12-08 03:09:28 +07:00
parent 3162030800
commit d0caee99aa
3 changed files with 29 additions and 17 deletions

View File

@ -201,7 +201,7 @@ void setup() {
oledDisplay.setText("", "Offline Storage Mode", ""); oledDisplay.setText("", "Offline Storage Mode", "");
delay(3000); delay(3000);
mdnsInit(); // mdnsInit();
localServer.begin(); localServer.begin();
// Update display and led bar after finishing setup to show dashboard // Update display and led bar after finishing setup to show dashboard
@ -377,7 +377,7 @@ static void factoryConfigReset(void) {
WiFi.disconnect(true, true); WiFi.disconnect(true, true);
/** Reset local config */ /** Reset local config */
configuration.reset(); // configuration.reset();
if (ag->isOne()) { if (ag->isOne()) {
oledDisplay.setText("Factory reset", "successful", ""); oledDisplay.setText("Factory reset", "successful", "");

View File

@ -253,7 +253,7 @@ void Configuration::loadConfig(void) {
} }
file.close(); file.close();
} else { } else {
SPIFFS.format(); // SPIFFS.format();
} }
#endif #endif
toConfig(buf); toConfig(buf);

View File

@ -1097,28 +1097,39 @@ bool Measurements::saveLocalStorage(AirGradient &ag, Configuration &config) {
file = SPIFFS.open(FILE_PATH, FILE_APPEND, false); 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); float pm25 = getCorrectedPM25(ag, config, true);
// Save new measurements // Save new measurements
file.printf("%s,%.2f,%.2f,%.2f,%.2f,%d,%d,%d,%d,%d\n", ag.getCurrentTime().c_str(), char buff[100] = {0};
ag.round2(_pm_03_pc[0].update.avg), ag.round2(pm25), sprintf(buff, "%s,%.2f,%.2f,%.2f,%.2f,%d,%d,%d,%d,%d\n\0", ag.getCurrentTime().c_str(),
ag.round2(_temperature[0].update.avg), ag.round2(_humidity[0].update.avg), ag.round2(_pm_03_pc[0].update.avg), ag.round2(pm25),
(int)round(_co2.update.avg), (int)round(_tvoc.update.avg), ag.round2(_temperature[0].update.avg), ag.round2(_humidity[0].update.avg),
(int)round(_tvoc_raw.update.avg), (int)round(_nox.update.avg), (int)round(_co2.update.avg), (int)round(_tvoc.update.avg),
(int)round(_nox_raw.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"); Serial.println("Success save measurements to local storage");
return true; return true;
} }
char *Measurements::getLocalStorage() { char *Measurements::getLocalStorage() {
char *buf = nullptr; char *buf = nullptr;
bool success = false; bool success = false;
if (!SPIFFS.exists(FILE_PATH)) {
Serial.println("No measurements file exists yet");
return nullptr;
}
File file = SPIFFS.open(FILE_PATH); File file = SPIFFS.open(FILE_PATH);
if (file && !file.isDirectory()) { if (file && !file.isDirectory()) {
// Allocate memory // Allocate memory
@ -1134,14 +1145,15 @@ char *Measurements::getLocalStorage() {
Serial.println("Reading measurements file: success"); Serial.println("Reading measurements file: success");
success = true; success = true;
} }
file.close(); file.close();
} else {
SPIFFS.format();
} }
if (!success) { if (!success) {
Serial.println("Reading measurements file failed"); Serial.println("Reading measurements file failed");
delete buf; if (buf != nullptr) {
delete buf;
}
return nullptr; return nullptr;
} }