Skip to content

Commit

Permalink
Fix close file after write
Browse files Browse the repository at this point in the history
Better error handling when write and load measurement file
comment out spiffs format
  • Loading branch information
samuelbles07 committed Dec 7, 2024
1 parent 3162030 commit d0caee9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/OneOpenAir/OneOpenAir.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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", "");
Expand Down
2 changes: 1 addition & 1 deletion src/AgConfigure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void Configuration::loadConfig(void) {
}
file.close();
} else {
SPIFFS.format();
// SPIFFS.format();
}
#endif
toConfig(buf);
Expand Down
40 changes: 26 additions & 14 deletions src/AgValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down

0 comments on commit d0caee9

Please sign in to comment.