forked from airgradienthq/arduino
Fix close file after write
Better error handling when write and load measurement file comment out spiffs format
This commit is contained in:
@ -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", "");
|
||||||
|
@ -253,7 +253,7 @@ void Configuration::loadConfig(void) {
|
|||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
} else {
|
} else {
|
||||||
SPIFFS.format();
|
// SPIFFS.format();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
toConfig(buf);
|
toConfig(buf);
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user