Save History Sync on flash, still some issues in deleting previous file

This commit is contained in:
Emanuele Trabattoni
2026-04-08 10:27:18 +02:00
parent 481f12f526
commit 07eb06f67b
5 changed files with 59 additions and 42 deletions

View File

@@ -1,45 +1,51 @@
#include "datasave.h"
static const size_t min_free = 1024 * 1024; // minimum free space in SPIFFS to allow saving history (1MB)
static bool first_save = true; // flag to indicate if this is the first save (to write header)
void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::filesystem::path &file_path)
void saveHistoryTask(void *pvParameters)
{
const auto *params = static_cast<dataSaveParams *>(pvParameters);
const auto &history = *params->history;
const auto &file_path = params->file_path;
if (!params) {
LOG_ERROR("Invalid parameters for saveHistoryTask");
return;
}
LOG_DEBUG("Starting saving: ", file_path.c_str());
save_history(history, file_path);
vTaskDelete(NULL);
}
void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::filesystem::path &file_name)
{
// Initialize SPIFFS
if (!SPIFFS.begin(true))
{
LOG_ERROR("Failed to mount SPIFFS");
LOG_ERROR("5 seconds to restart...");
vTaskDelay(pdMS_TO_TICKS(5000));
esp_restart();
}
auto spiffs_guard = SPIFFSGuard(); // use RAII guard to ensure SPIFFS is properly mounted and unmounted
LOG_INFO("SPIFFS mounted successfully");
if (SPIFFS.totalBytes() - SPIFFS.usedBytes() < min_free ) // check if at least 1MB is free for saving history
{
LOG_ERROR("Not enough space in SPIFFS to save history");
return;
}
std::filesystem::path to_save = file_path;
if (file_path.root_path() != "/spiffs")
to_save = std::filesystem::path("/spiffs") / file_path;
std::filesystem::path file_path = file_name;
if (file_name.root_path() != "/spiffs")
file_path = std::filesystem::path("/spiffs") / file_name;
auto save_flags = std::ios::out;
if (first_save && SPIFFS.exists(to_save.c_str()))
if (first_save && SPIFFS.exists(file_path.c_str()))
{
first_save = false;
save_flags |= std::ios::trunc; // overwrite existing file
SPIFFS.remove(to_save.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS
LOG_INFO("Saving history to SPIFFS, new file: ", to_save.c_str());
SPIFFS.remove(file_path.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS
LOG_INFO("Saving history to SPIFFS, new file:", file_path.c_str());
}
else
{
save_flags |= std::ios::app; // append to new file
LOG_INFO("Saving history to SPIFFS, appending to existing file: ", to_save.c_str());
LOG_INFO("Saving history to SPIFFS, appending to existing file:", file_path.c_str());
}
std::ofstream ofs(to_save, save_flags);
std::ofstream ofs(file_path, save_flags);
if (ofs.fail())
{
LOG_ERROR("Failed to open file for writing");
@@ -50,9 +56,9 @@ void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::file
if (first_save)
{
ofs << "TS,\
EVENTS_12,DLY_12,STAT_12,V_12_1,V_12_2,V_12_3,V_12_4,IGNITION_MODE_12,\
EVENTS_34,DLY_34,STAT_34,V_34_1,V_34_2,V_34_3,V_34_4,IGNITION_MODE_34,\
ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS" << std::endl;
EVENTS_12,DLY_12,STAT_12,V_12_1,V_12_2,V_12_3,V_12_4,IGNITION_MODE_12,\
EVENTS_34,DLY_34,STAT_34,V_34_1,V_34_2,V_34_3,V_34_4,IGNITION_MODE_34,\
ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS" << std::endl;
ofs.flush();
}
@@ -84,5 +90,4 @@ void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::file
ofs.close();
LOG_INFO("Ignition A history saved to SPIFFS, records written: ", history.size());
SPIFFS.end(); // unmount SPIFFS to ensure data is written and avoid corruption on next mount
}