task refactoring work in progress
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include "datasave.h"
|
||||
#include <math.h>
|
||||
|
||||
static const size_t min_free = 1024 * 1024; // minimum free space in LittleFS to allow saving history (1MB)
|
||||
|
||||
|
||||
LITTLEFSGuard::LITTLEFSGuard()
|
||||
{
|
||||
@@ -22,26 +22,26 @@ LITTLEFSGuard::~LITTLEFSGuard()
|
||||
LOG_INFO("LittleFS unmounted successfully");
|
||||
}
|
||||
|
||||
void ignitionBoxStatusAverage::filter(int32_t &old, const int32_t value, const uint32_t k)
|
||||
void ignitionBoxStatusFiltered::filter(int32_t &old, const int32_t value, const uint32_t k)
|
||||
{
|
||||
float alpha = 1.0f / (float)k;
|
||||
old = old + (int32_t)(alpha * (float)(value - old));
|
||||
}
|
||||
|
||||
void ignitionBoxStatusAverage::filter(float &old, const float value, const uint32_t k)
|
||||
void ignitionBoxStatusFiltered::filter(float &old, const float value, const uint32_t k)
|
||||
{
|
||||
float alpha = 1.0f / (float)k;
|
||||
old = old + (float)(alpha * (float)(value - old));
|
||||
}
|
||||
|
||||
void ignitionBoxStatusAverage::reset()
|
||||
void ignitionBoxStatusFiltered::reset()
|
||||
{
|
||||
m_last = ignitionBoxStatus();
|
||||
m_count = 0;
|
||||
m_data_valid = false;
|
||||
}
|
||||
|
||||
void ignitionBoxStatusAverage::update(const ignitionBoxStatus &new_status)
|
||||
void ignitionBoxStatusFiltered::update(const ignitionBoxStatus &new_status)
|
||||
{
|
||||
if (m_count == 0 && !m_data_valid)
|
||||
{
|
||||
@@ -81,7 +81,7 @@ void ignitionBoxStatusAverage::update(const ignitionBoxStatus &new_status)
|
||||
}
|
||||
}
|
||||
|
||||
const bool ignitionBoxStatusAverage::get(ignitionBoxStatus &status) const
|
||||
const bool ignitionBoxStatusFiltered::get(ignitionBoxStatus &status) const
|
||||
{
|
||||
if (m_data_valid)
|
||||
{
|
||||
@@ -90,7 +90,7 @@ const bool ignitionBoxStatusAverage::get(ignitionBoxStatus &status) const
|
||||
return m_data_valid;
|
||||
}
|
||||
|
||||
const ArduinoJson::JsonDocument ignitionBoxStatusAverage::toJson() const
|
||||
const ArduinoJson::JsonDocument ignitionBoxStatusFiltered::toJson() const
|
||||
{
|
||||
ArduinoJson::JsonDocument doc;
|
||||
if (m_data_valid)
|
||||
@@ -125,97 +125,3 @@ const ArduinoJson::JsonDocument ignitionBoxStatusAverage::toJson() const
|
||||
return doc;
|
||||
}
|
||||
|
||||
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 (!SAVE_HISTORY_TO_LITTLEFS)
|
||||
return;
|
||||
|
||||
auto littlefs_guard = LITTLEFSGuard(); // use RAII guard to ensure LittleFS is properly mounted and unmounted
|
||||
|
||||
if (LittleFS.totalBytes() - LittleFS.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 file_path = file_name;
|
||||
if (file_name.root_path() != "/littlefs")
|
||||
file_path = std::filesystem::path("/littlefs") / file_name;
|
||||
|
||||
auto save_flags = std::ios::out;
|
||||
if (first_save && LittleFS.exists(file_path.c_str()))
|
||||
{
|
||||
first_save = false;
|
||||
save_flags |= std::ios::trunc; // overwrite existing file
|
||||
LittleFS.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 LittleFS, new file:", file_path.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
save_flags |= std::ios::app; // append to new file
|
||||
LOG_INFO("Saving history to LittleFS, appending to existing file:", file_path.c_str());
|
||||
}
|
||||
|
||||
std::ofstream ofs(file_path, save_flags);
|
||||
if (ofs.fail())
|
||||
{
|
||||
LOG_ERROR("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
|
||||
// write csv header
|
||||
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;
|
||||
ofs.flush();
|
||||
}
|
||||
|
||||
for (const auto &entry : history)
|
||||
{
|
||||
ofs << std::to_string(entry.timestamp) << ","
|
||||
<< std::to_string(entry.coils12.n_events) << ","
|
||||
<< std::to_string(entry.coils12.spark_delay) << ","
|
||||
<< std::string(sparkStatusNames.at(entry.coils12.spark_status)) << ","
|
||||
<< std::to_string(entry.coils12.peak_p_in) << ","
|
||||
<< std::to_string(entry.coils12.peak_n_in) << ","
|
||||
<< std::to_string(entry.coils12.peak_p_out) << ","
|
||||
<< std::to_string(entry.coils12.peak_n_out) << ","
|
||||
<< std::string(softStartStatusNames.at(entry.coils12.sstart_status)) << ","
|
||||
<< std::to_string(entry.coils34.n_events) << ","
|
||||
<< std::to_string(entry.coils34.spark_delay) << ","
|
||||
<< std::string(sparkStatusNames.at(entry.coils34.spark_status)) << ","
|
||||
<< std::to_string(entry.coils34.peak_p_in) << ","
|
||||
<< std::to_string(entry.coils34.peak_n_in) << ","
|
||||
<< std::to_string(entry.coils34.peak_p_out) << ","
|
||||
<< std::to_string(entry.coils34.peak_n_out) << ","
|
||||
<< std::string(softStartStatusNames.at(entry.coils34.sstart_status)) << ","
|
||||
<< std::to_string(entry.eng_rpm) << ","
|
||||
<< std::to_string(entry.adc_read_time) << ","
|
||||
<< std::to_string(entry.n_queue_errors);
|
||||
ofs << std::endl;
|
||||
ofs.flush();
|
||||
}
|
||||
|
||||
ofs.close();
|
||||
LOG_INFO("Ignition A history saved to LittleFS, records written: ", history.size());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user