From 66be09c98eb35d15cfa2ba79812ff4e6dc7246a9 Mon Sep 17 00:00:00 2001 From: Slider0007 <115730895+Slider0007@users.noreply.github.com> Date: Sun, 20 Nov 2022 17:44:42 +0100 Subject: [PATCH] Add parameter to disable write data log (#1382) --- .../jomjol_flowcontroll/ClassFlowControll.cpp | 28 +++- .../ClassFlowPostProcessing.cpp | 4 + .../jomjol_logfile/ClassLogFile.cpp | 72 +++++++--- code/components/jomjol_logfile/ClassLogFile.h | 12 +- .../jomjol_tfliteclass/server_tflite.cpp | 3 +- sd-card/config/config.ini | 4 + sd-card/html/edit_config_param.html | 130 +++++++++++------- sd-card/html/readconfigparam.js | 8 ++ 8 files changed, 184 insertions(+), 77 deletions(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp index 6235a058..6549c89a 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp @@ -203,6 +203,9 @@ ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type) if (toUpper(_type).compare("[AUTOTIMER]") == 0) cfc = this; + if (toUpper(_type).compare("[DATALOGGING]") == 0) + cfc = this; + if (toUpper(_type).compare("[DEBUG]") == 0) cfc = this; @@ -446,7 +449,8 @@ bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph) return false; - if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) && (toUpper(aktparamgraph).compare("[SYSTEM]") != 0)) // Paragraph passt nicht zu MakeImage + if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) && + (toUpper(aktparamgraph).compare("[SYSTEM]") != 0 && (toUpper(aktparamgraph).compare("[DATALOGGING]") != 0))) // Paragraph passt nicht zu MakeImage return false; while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph)) @@ -459,10 +463,28 @@ bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph) AutoStart = true; } } + if ((toUpper(zerlegt[0]) == "INTERVALL") && (zerlegt.size() > 1)) { AutoIntervall = std::stof(zerlegt[1]); } + + if ((toUpper(zerlegt[0]) == "DATALOGACTIVE") && (zerlegt.size() > 1)) + { + if (toUpper(zerlegt[1]) == "TRUE") + { + LogFile.SetDataLogToSD(true); + } + else { + LogFile.SetDataLogToSD(false); + } + } + + if ((toUpper(zerlegt[0]) == "DATALOGRETENTIONINDAYS") && (zerlegt.size() > 1)) + { + LogFile.SetDataLogRetention(std::stoi(zerlegt[1])); + } + if ((toUpper(zerlegt[0]) == "LOGFILE") && (zerlegt.size() > 1)) { /* matches esp_log_level_t */ @@ -485,8 +507,8 @@ bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph) } if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1)) { - LogFile.SetRetention(std::stoi(zerlegt[1])); - } + LogFile.SetLogFileRetention(std::stoi(zerlegt[1])); + } if ((toUpper(zerlegt[0]) == "TIMEZONE") && (zerlegt.size() > 1)) { diff --git a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp b/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp index 7f428d81..f1965b52 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp @@ -869,6 +869,10 @@ bool ClassFlowPostProcessing::doFlow(string zwtime) void ClassFlowPostProcessing::WriteDataLog(int _index) { + if (!LogFile.GetDataLogToSD()){ + return; + } + string analog = ""; string digital = ""; string timezw = ""; diff --git a/code/components/jomjol_logfile/ClassLogFile.cpp b/code/components/jomjol_logfile/ClassLogFile.cpp index 62ab0673..492da341 100644 --- a/code/components/jomjol_logfile/ClassLogFile.cpp +++ b/code/components/jomjol_logfile/ClassLogFile.cpp @@ -80,11 +80,6 @@ void ClassLogFile::WriteToData(std::string _timestamp, std::string _name, std::s FILE* pFile; std::string zwtime; - // TODO add separate parameter to disable write of data. - /*if (!doLogFile){ - return; - }*/ - ESP_LOGD(TAG, "Datalogfile: %s", logpath.c_str()); pFile = fopen(logpath.c_str(), "a+"); @@ -208,11 +203,23 @@ void ClassLogFile::setLogLevel(esp_log_level_t _logLevel){ LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Test"); LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test"); */ -}; +} -void ClassLogFile::SetRetention(unsigned short _retentionInDays){ - retentionInDays = _retentionInDays; -}; +void ClassLogFile::SetLogFileRetention(unsigned short _LogFileRetentionInDays){ + logFileRetentionInDays = _LogFileRetentionInDays; +} + +void ClassLogFile::SetDataLogRetention(unsigned short _DataLogRetentionInDays){ + dataLogRetentionInDays = _DataLogRetentionInDays; +} + +void ClassLogFile::SetDataLogToSD(bool _doDataLogToSD){ + doDataLogToSD = _doDataLogToSD; +} + +bool ClassLogFile::GetDataLogToSD(){ + return doDataLogToSD; +} void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::string message, bool _time) { @@ -283,23 +290,24 @@ std::string ClassLogFile::GetCurrentFileName() return logpath; } -void ClassLogFile::RemoveOld() +void ClassLogFile::RemoveOldLogFile() { - if (retentionInDays == 0) { + if (logFileRetentionInDays == 0) { return; } + ESP_LOGI(TAG, "Remove old log files"); + time_t rawtime; struct tm* timeinfo; char cmpfilename[30]; time(&rawtime); - rawtime = addDays(rawtime, -retentionInDays + 1); + rawtime = addDays(rawtime, -logFileRetentionInDays + 1); timeinfo = localtime(&rawtime); - //ESP_LOGD(TAG, "logfileRetentionInDays: %d", retentionInDays); + //ESP_LOGD(TAG, "logFileRetentionInDays: %d", logFileRetentionInDays); + -////////////////////// message ///////////////////////////////////////// - ESP_LOGI(TAG, "remove old log files"); strftime(cmpfilename, 30, logfile.c_str(), timeinfo); //ESP_LOGD(TAG, "log file name to compare: %s", cmpfilename); @@ -329,23 +337,40 @@ void ClassLogFile::RemoveOld() } } } - ESP_LOGI(TAG, "data files deleted: %d | files not deleted (incl. leer.txt): %d", deleted, notDeleted); + ESP_LOGI(TAG, "log files deleted: %d | files not deleted (incl. leer.txt): %d", deleted, notDeleted); closedir(dir); +} -////////////////////// data ///////////////////////////////////////// - ESP_LOGI(TAG, "remove old data files"); +void ClassLogFile::RemoveOldDataLog() +{ + if (dataLogRetentionInDays == 0 || !doDataLogToSD) { + return; + } + + ESP_LOGI(TAG, "Remove old data files"); + + time_t rawtime; + struct tm* timeinfo; + char cmpfilename[30]; + + time(&rawtime); + rawtime = addDays(rawtime, -dataLogRetentionInDays + 1); + timeinfo = localtime(&rawtime); + //ESP_LOGD(TAG, "dataLogRetentionInDays: %d", dataLogRetentionInDays); + strftime(cmpfilename, 30, datafile.c_str(), timeinfo); //ESP_LOGD(TAG, "data file name to compare: %s", cmpfilename); - dir = opendir(dataroot.c_str()); + DIR *dir = opendir(dataroot.c_str()); if (!dir) { ESP_LOGE(TAG, "Failed to stat dir : %s", dataroot.c_str()); return; } - deleted = 0; - notDeleted = 0; + struct dirent *entry; + int deleted = 0; + int notDeleted = 0; while ((entry = readdir(dir)) != NULL) { if (entry->d_type == DT_REG) { //ESP_LOGD(TAG, "Compare data file : %s to %s", entry->d_name, cmpfilename); @@ -367,6 +392,7 @@ void ClassLogFile::RemoveOld() closedir(dir); } + void ClassLogFile::CreateLogDirectories() { MakeDir("/sdcard/log"); @@ -384,6 +410,8 @@ ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile, std::stri logfile = _logfile; datafile = _datafile; dataroot = _logdatapath; - retentionInDays = 10; + logFileRetentionInDays = 3; + dataLogRetentionInDays = 3; + doDataLogToSD = true; loglevel = ESP_LOG_INFO; } diff --git a/code/components/jomjol_logfile/ClassLogFile.h b/code/components/jomjol_logfile/ClassLogFile.h index de7f01a5..8ea731f5 100644 --- a/code/components/jomjol_logfile/ClassLogFile.h +++ b/code/components/jomjol_logfile/ClassLogFile.h @@ -10,7 +10,9 @@ private: std::string logfile; std::string dataroot; std::string datafile; - unsigned short retentionInDays; + unsigned short logFileRetentionInDays; + unsigned short dataLogRetentionInDays; + bool doDataLogToSD; esp_log_level_t loglevel; public: ClassLogFile(std::string _logpath, std::string _logfile, std::string _logdatapath, std::string _datafile); @@ -20,7 +22,10 @@ public: void WriteHeapInfo(std::string _id); void setLogLevel(esp_log_level_t _logLevel); - void SetRetention(unsigned short _retentionInDays); + void SetLogFileRetention(unsigned short _LogFileRetentionInDays); + void SetDataLogRetention(unsigned short _DataLogRetentionInDays); + void SetDataLogToSD(bool _doDataLogToSD); + bool GetDataLogToSD(); void WriteToFile(esp_log_level_t level, std::string tag, std::string message, bool _time); void WriteToFile(esp_log_level_t level, std::string tag, std::string message); @@ -28,7 +33,8 @@ public: void WriteToDedicatedFile(std::string _fn, esp_log_level_t level, std::string message, bool _time = true); void CreateLogDirectories(); - void RemoveOld(); + void RemoveOldLogFile(); + void RemoveOldDataLog(); // void WriteToData(std::string _ReturnRawValue, std::string _ReturnValue, std::string _ReturnPreValue, std::string _ErrorMessageText, std::string _digital, std::string _analog); void WriteToData(std::string _timestamp, std::string _name, std::string _ReturnRawValue, std::string _ReturnValue, std::string _ReturnPreValue, std::string _ReturnRateValue, std::string _ReturnChangeAbsolute, std::string _ErrorMessageText, std::string _digital, std::string _analog); diff --git a/code/components/jomjol_tfliteclass/server_tflite.cpp b/code/components/jomjol_tfliteclass/server_tflite.cpp index a9a782e7..ef706e58 100644 --- a/code/components/jomjol_tfliteclass/server_tflite.cpp +++ b/code/components/jomjol_tfliteclass/server_tflite.cpp @@ -747,7 +747,8 @@ void task_autodoFlow(void *pvParameter) #ifdef DEBUG_DETAIL_ON ESP_LOGD(TAG, "Remove older log files"); #endif - LogFile.RemoveOld(); + LogFile.RemoveOldLogFile(); + LogFile.RemoveOldDataLog(); } LogFile.WriteToFile(ESP_LOG_INFO, TAG, "task_autodoFlow - round #" + std::to_string(countRounds) + " done"); diff --git a/sd-card/config/config.ini b/sd-card/config/config.ini index 31154d23..68b9fbc0 100644 --- a/sd-card/config/config.ini +++ b/sd-card/config/config.ini @@ -84,6 +84,10 @@ LEDColor = 150 150 150 AutoStart = true Intervall = 5 +[DataLogging] +DataLogActive = true +DataLogRetentionInDays = 3 + [Debug] Logfile = 1 LogfileRetentionInDays = 3 diff --git a/sd-card/html/edit_config_param.html b/sd-card/html/edit_config_param.html index c70abfa3..845331dd 100644 --- a/sd-card/html/edit_config_param.html +++ b/sd-card/html/edit_config_param.html @@ -9,9 +9,8 @@ h1 {font-size: 2em;} h2 {font-size: 1.5em; margin-block-start: 0.0em; margin-block-end: 0.2em;} h3 {font-size: 1.2em;} -h4 { - margin-bottom: 0; -} +h4 {font-size: 1em; margin-bottom: 0;} +h5 {font-size: 0.83em; margin-top: 0.2em; margin-bottom: 0;} p {font-size: 1em;} @@ -626,9 +625,11 @@ textarea { -

Homeassistant Discovery (using MQTT)

- The discovery topics and the static topics (IP, MAC, Hostname, Interval, ...) only get sent on startup. - To send them again, you can call the following URL: http://<IP>/mqtt_publish_discovery + +

Homeassistant Discovery (using MQTT)

+
The discovery topics and the static topics (IP, MAC, Hostname, Interval, ...) only get sent on startup. + To send them again, you can call the following URL: http://<IP>/mqtt_publish_discovery
+ @@ -675,8 +676,7 @@ textarea {

- -

+ @@ -739,43 +739,14 @@ textarea { Password for InfluxDB authentication - - - -

AutoTimer

- - - - AutoStart - - - - - - Start the image recognition immediatly after power up. false is basically for debugging. - - - - - Interval - - - - - - Interval in which the number(s) are read (in minutes). If a run takes longer than this interval, the next run gets postponed until the current run completes. - -

-

+ +
Enabling GPIO handler, disable by default integrated flash light.
Please enable it with GPIO4 (internal flash LED) settings or GPIO12 (external LED). +
@@ -1272,13 +1243,71 @@ textarea { + +

AutoTimer

+ + + + AutoStart + + + + + + Start the image recognition immediatly after power up. false is basically for debugging. + + + + + Interval + + + + + + Interval in which the number(s) are read (in minutes). If a run takes longer than this interval, the next run gets postponed until the current run completes. + + + + +

DataLogging

+ + + + DataLogActive + + + + + + Activate data log to SD card + + + + + + DataLogRetentionInDays + + + + + + Time to keep the data files (in days - "0" = forever) + + +

Debug

- - + Logfile Log Level - + LogfileRetentionInDays @@ -1784,8 +1812,11 @@ function UpdateInput() { WriteParameter(param, category, "AutoTimer", "AutoStart", false); WriteParameter(param, category, "AutoTimer", "Intervall", false); - WriteParameter(param, category, "Debug", "Logfile", true); - WriteParameter(param, category, "Debug", "LogfileRetentionInDays", true); + WriteParameter(param, category, "DataLogging", "DataLogActive", false); + WriteParameter(param, category, "DataLogging", "DataLogRetentionInDays", false); + + WriteParameter(param, category, "Debug", "Logfile", false); + WriteParameter(param, category, "Debug", "LogfileRetentionInDays", false); WriteParameter(param, category, "System", "TimeZone", true); WriteParameter(param, category, "System", "Hostname", true); @@ -1908,9 +1939,12 @@ function ReadParameterAll() ReadParameter(param, "AutoTimer", "AutoStart", false); ReadParameter(param, "AutoTimer", "Intervall", false); + + ReadParameter(param, "DataLogging", "DataLogActive", false); + ReadParameter(param, "DataLogging", "DataLogRetentionInDays", false); - ReadParameter(param, "Debug", "Logfile", true); - ReadParameter(param, "Debug", "LogfileRetentionInDays", true); + ReadParameter(param, "Debug", "Logfile", false); + ReadParameter(param, "Debug", "LogfileRetentionInDays", false); ReadParameter(param, "System", "TimeZone", true); ReadParameter(param, "System", "Hostname", true); diff --git a/sd-card/html/readconfigparam.js b/sd-card/html/readconfigparam.js index 267a71b8..1f671da8 100644 --- a/sd-card/html/readconfigparam.js +++ b/sd-card/html/readconfigparam.js @@ -231,6 +231,14 @@ function ParseConfig() { ParamAddValue(param, catname, "AutoStart"); ParamAddValue(param, catname, "Intervall"); + var catname = "DataLogging"; + category[catname] = new Object(); + category[catname]["enabled"] = false; + category[catname]["found"] = false; + param[catname] = new Object(); + ParamAddValue(param, catname, "DataLogActive"); + ParamAddValue(param, catname, "DataLogRetentionInDays"); + var catname = "Debug"; category[catname] = new Object(); category[catname]["enabled"] = false;