add chart data purge when deleting chart, TODO is when changing chart sensor

This commit is contained in:
DeltaLima 2025-04-11 02:28:16 +02:00
parent c8f9a817d7
commit e9bd5f8968
4 changed files with 43 additions and 4 deletions

View file

@ -741,7 +741,7 @@ unsigned long Timescale(byte unit) {
*
*/
String Str_Chart_Json() {
String Str_Chart_Json(bool escapeUnit = false) {
JsonDocument doc;
for(byte i = 0 ; i < Max_Dashboard_Chart ; i++) {
if(config.grow.dashboard.chartConfigured[i] == true) {
@ -752,14 +752,19 @@ String Str_Chart_Json() {
objChart["name"] = config.system.sensor.name[config.grow.dashboard.chartSensor[i]];
/* when for a RAW reading rawConvert is set, return the converted description and unit */
String unit;
if((SensorIndex[config.system.sensor.type[config.grow.dashboard.chartSensor[i]]].read[config.grow.dashboard.chartRead[i]] == SENSOR_READ_TYPE_RAW) && (config.system.sensor.rawConvert[config.grow.dashboard.chartSensor[i]][config.grow.dashboard.chartRead[i]] > 0)) {
objChart["descr"] = FPSTR(Sensor_Convert_Raw_descr[config.system.sensor.rawConvert[config.grow.dashboard.chartSensor[i]][config.grow.dashboard.chartRead[i]]]);
objChart["unit"] = FPSTR(Sensor_Convert_Raw_unit[config.system.sensor.rawConvert[config.grow.dashboard.chartSensor[i]][config.grow.dashboard.chartRead[i]]]);
unit = FPSTR(Sensor_Convert_Raw_unit[config.system.sensor.rawConvert[config.grow.dashboard.chartSensor[i]][config.grow.dashboard.chartRead[i]]]);
} else {
objChart["descr"] = FPSTR(Sensor_Read_descr[SensorIndex[config.system.sensor.type[config.grow.dashboard.chartSensor[i]]].read[config.grow.dashboard.chartRead[i]]]);
objChart["unit"] = FPSTR(Sensor_Read_unit[SensorIndex[config.system.sensor.type[config.grow.dashboard.chartSensor[i]]].read[config.grow.dashboard.chartRead[i]]]);
unit = FPSTR(Sensor_Read_unit[SensorIndex[config.system.sensor.type[config.grow.dashboard.chartSensor[i]]].read[config.grow.dashboard.chartRead[i]]]);
}
/* if used within the template processor, it would interprate % as template variable */
if(escapeUnit == true)
unit.replace(F("%"), F("%%"));
objChart["unit"] = unit;
/* build path to chart data dir */
char chartDirPath[10];
snprintf(chartDirPath, sizeof(chartDirPath), "%s%d", CHART_BASE_PATH, i);

View file

@ -861,6 +861,37 @@ void LFS_Chart_Rotate_Data(const byte chartId) {
}
/* Function to remove all Chart files */
void LFS_Chart_Purge_Data(const byte chartId) {
const static char LogLoc[] PROGMEM = "[LittleFS:LFS_Chart_Purge_Data]";
char chartDirPath[10];
snprintf(chartDirPath, sizeof(chartDirPath), "%s%d", CHART_BASE_PATH, chartId);
/* https://github.com/cotestatnt/async-esp-fs-webserver/blob/master/src/AsyncFsWebServer.cpp */
File root = LittleFS.open(chartDirPath, "r");
File file = root.openNextFile();
while (file) {
char timestampFile_char[12];
//timestampFile_char = file.name();
unsigned long timestampFile = strtoul(file.name(), NULL, 0);
if (!file.isDirectory()) {
char pathDelete[32];
snprintf(pathDelete, sizeof(pathDelete), "%s/%s", chartDirPath, file.name());
if(LittleFS.remove(pathDelete)) {
#ifdef DEBUG
Log.verbose(F("%s DELETED chart%d/%u" CR), LogLoc, chartId, timestampFile);
#endif
} else {
#ifdef DEBUG
Log.error(F("%s FAILED DELETE chart%d/%u" CR), LogLoc, chartId, timestampFile);
#endif
}
}
file = root.openNextFile();
}
}
///*
//* ESP8266 functions
//*/

View file

@ -1214,6 +1214,8 @@ void WebPage_grow_dashboard(AsyncWebServerRequest *request) {
config.grow.dashboard.chartRead[chartId] = 0;
memset(config.grow.dashboard.chartColor[chartId], '\0', sizeof config.grow.dashboard.chartColor[chartId]);
/* purge all data */
LFS_Chart_Purge_Data(chartId);
SaveConfig();
Log.notice(F("%s config saved" CR), LogLoc);
} else if(request->hasParam("delete_data", true)) {

View file

@ -161,7 +161,8 @@ String Proc_WebPage_root(const String& var) {
if(chartCount > 0) {
/* FOR TESTING ONLY - INCLUDE EXTERNAL CSSCHARTS*/
html += F("<script type='application/json' id='chartJson'>");
html += Str_Chart_Json();
/* get chart json with escaped unit (replace % with %% because of template engine) */
html += Str_Chart_Json(true);
html += F("</script>");
html += F("<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/charts.css/dist/charts.min.css'>");
html += F("<div id='chartDiv'><table id='chartTable' class='charts-css line multiple show-data-on-hover show-heading show-primary-axis'><caption>Linechart</caption>");