write and append chart log files wip
This commit is contained in:
parent
f6b9325baf
commit
cd68069567
3 changed files with 128 additions and 24 deletions
|
@ -43,6 +43,8 @@
|
|||
|
||||
#define CANGROW_CFG "/config.json"
|
||||
#define TIME2FS "/time"
|
||||
#define CHART_BASE_PATH "/c"
|
||||
|
||||
|
||||
/* define Max limits for outputs and sensors */
|
||||
const byte Max_Outputs = 16;
|
||||
|
|
|
@ -732,51 +732,151 @@ bool SaveConfig(bool writeToSerial = false) {
|
|||
void LFS_Chart_Log_Data(const byte chartId) {
|
||||
const static char LogLoc[] PROGMEM = "[LittleFS:Chart_Log_Data]";
|
||||
/* sensor data is in directory /d/ */
|
||||
char fullpath[24]; //18
|
||||
//const char *basePath = "/c";
|
||||
char chartDirPath[16];
|
||||
char filePath[32]; //18
|
||||
char tmp_message[32];
|
||||
char tmp_floatChar[16]; // = "12.34";
|
||||
float tmp_float = Sensor_getCalibratedValue(config.grow.dashboard.chartSensor[chartId], config.grow.dashboard.chartRead[chartId]);
|
||||
unsigned long timeNow = now();
|
||||
|
||||
/* build full path - looks like /d/0_1/20250420 */
|
||||
snprintf(fullpath, sizeof(fullpath), "/chart/%d/%s", chartId, Char_Timestamp_yyyymmdd(timeNow));
|
||||
/* build full path with chartId */
|
||||
/* first path to chart dir */
|
||||
snprintf(chartDirPath, sizeof(chartDirPath), "%s/%d", CHART_BASE_PATH, chartId);
|
||||
/* then the full absolute path to the file */
|
||||
snprintf(filePath, sizeof(filePath), "%s/%s", chartDirPath, Char_Timestamp_yyyymmdd(timeNow));
|
||||
|
||||
/* build message
|
||||
* https://forum.arduino.cc/t/sprintf-a-float-number/1013193/5 */
|
||||
/* convert float to char min 2 digits long, max 2 digits after . */
|
||||
dtostrf(tmp_float, 2, 2, tmp_floatChar);
|
||||
snprintf(tmp_message, sizeof(tmp_message), "%lu;%s", timeNow, tmp_floatChar);
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
Log.notice(F("%s fullpath: %s, message: %s" CR), LogLoc, filePath, tmp_message);
|
||||
#endif
|
||||
|
||||
Log.notice(F("%s fullpath: %s, message: %s" CR), LogLoc, fullpath, tmp_message);
|
||||
/* https://github.com/cotestatnt/async-esp-fs-webserver/blob/master/examples/csvLogger/csvLogger.ino */
|
||||
/* check if dir exists */
|
||||
if (!LittleFS.exists(chartDirPath)) {
|
||||
#ifdef DEBUG
|
||||
Log.notice(F("%s create non-existing dir: %s" CR), LogLoc, chartDirPath);
|
||||
#endif
|
||||
LittleFS.mkdir(chartDirPath);
|
||||
}
|
||||
|
||||
File file;
|
||||
if (LittleFS.exists(filePath)) {
|
||||
file = LittleFS.open(filePath, "a"); // Append to existing file
|
||||
#ifdef DEBUG
|
||||
Log.notice(F("%s appending filePath: %s" CR), LogLoc, filePath);
|
||||
#endif
|
||||
} else {
|
||||
file = LittleFS.open(filePath, "w"); // Create a new file
|
||||
#ifdef DEBUG
|
||||
Log.notice(F("%s create filePath: %s" CR), LogLoc, filePath);
|
||||
#endif
|
||||
}
|
||||
file.println(tmp_message);
|
||||
file.close();
|
||||
|
||||
|
||||
|
||||
//#ifdef ESP8266
|
||||
///* create directory */
|
||||
//LittleFS.mkdir(fullpath);
|
||||
///* build full path with filename to write in */
|
||||
//snprintf(fullpath, sizeof(fullpath), "/chart/%d/%s", chartId, Char_Timestamp_yyyymmdd(timeNow));
|
||||
//File file = LittleFS.open(fullpath, "a");
|
||||
//#endif
|
||||
|
||||
//#ifdef ESP32
|
||||
//fs::FS &fs = LittleFS;
|
||||
///* create directory */
|
||||
//fs.mkdir(fullpath);
|
||||
///* build full path with filename to write in */
|
||||
//snprintf(fullpath, sizeof(fullpath), "/chart/%d/%s", chartId, Char_Timestamp_yyyymmdd(timeNow));
|
||||
//snprintf(fullpath, sizeof(fullpath), "/%s",Char_Timestamp_yyyymmdd(timeNow));
|
||||
//File file = fs.open(fullpath, FILE_WRITE);
|
||||
//#endif
|
||||
|
||||
|
||||
//if (!file) {
|
||||
//Log.error(F("%s Failed to open file for appending %s" CR), LogLoc, fullpath);
|
||||
//return;
|
||||
//}
|
||||
//if (file.print(tmp_message)) {
|
||||
//#ifdef DEBUG
|
||||
//Log.notice(F("%s File appended %s" CR), LogLoc, fullpath);
|
||||
//#endif
|
||||
//} else {
|
||||
//Log.error(F("%s Failed to append file %s" CR), LogLoc, fullpath);
|
||||
//}
|
||||
//file.close();
|
||||
}
|
||||
|
||||
/* Function to List Chart dirs, for debugging purposes mainly */
|
||||
void LFS_Chart_Dirlist_Data(const byte chartId) {
|
||||
const static char LogLoc[] PROGMEM = "[LittleFS:Chart_Dirlist_Data]";
|
||||
//const char *basePath = "/c";
|
||||
char chartDirPath[10];
|
||||
//snprintf(path, sizeof(path), "/chart/%d", chartId);
|
||||
snprintf(chartDirPath, sizeof(chartDirPath), "%s/%d", CHART_BASE_PATH, chartId);
|
||||
|
||||
/* FS Implementation of ESP8266 Core and ESP32 Arduino core is different, so it needs to be seperated completely :( */
|
||||
|
||||
#ifdef ESP8266
|
||||
File file = LittleFS.open(fullpath, "a");
|
||||
Dir root = LittleFS.openDir(chartDirPath);
|
||||
while (root.next()) {
|
||||
if (root.isFile()) {
|
||||
Serial.print(" FILE : ");
|
||||
Serial.println(root.fileName());
|
||||
}
|
||||
if(root.isDirectory()) {
|
||||
Serial.print(" DIR: ");
|
||||
Serial.print(root.fileName());
|
||||
Serial.print("\tSIZE: ");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
fs::FS &fs = LittleFS;
|
||||
File file = fs.open(fullpath, FILE_APPEND);
|
||||
File root = fs.open(chartDirPath);
|
||||
if (!root) {
|
||||
Serial.println("- failed to open directory");
|
||||
//return;
|
||||
}
|
||||
if (!root.isDirectory()) {
|
||||
Serial.println(" - not a directory");
|
||||
//return;
|
||||
}
|
||||
|
||||
File file = root.openNextFile();
|
||||
while (file) {
|
||||
if (file.isDirectory()) {
|
||||
Serial.print(" DIR : ");
|
||||
Serial.println(file.name());
|
||||
|
||||
} else {
|
||||
Serial.print(" FILE: ");
|
||||
Serial.print(file.name());
|
||||
Serial.print("\tSIZE: ");
|
||||
Serial.println(file.size());
|
||||
}
|
||||
file = root.openNextFile();
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!file) {
|
||||
Log.error(F("%s Failed to open file for appending %s" CR), LogLoc, fullpath);
|
||||
return;
|
||||
}
|
||||
if (file.print(tmp_message)) {
|
||||
#ifdef DEBUG
|
||||
Log.notice(F("%s File appended %s" CR), LogLoc, fullpath);
|
||||
#endif
|
||||
} else {
|
||||
Log.error(F("%s Failed to append file %s" CR), LogLoc, fullpath);
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void LFS_Chart_Dirlist_Data() {
|
||||
const static char LogLoc[] PROGMEM = "[LittleFS:Chart_Dirlist_Data]";
|
||||
|
||||
}
|
||||
|
||||
/* Rotate Chart data files */
|
||||
void LFS_Chart_Rotate_Data(const byte chartId) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
///*
|
||||
//* ESP8266 functions
|
||||
//*/
|
||||
|
|
|
@ -43,8 +43,10 @@ void Timer_3s() {
|
|||
#ifdef DEBUG2
|
||||
Log.verbose(F("%s" CR), LogLoc);
|
||||
#endif
|
||||
if(config.grow.dashboard.chartConfigured[0] == true)
|
||||
if(config.grow.dashboard.chartConfigured[0] == true) {
|
||||
LFS_Chart_Log_Data(0);
|
||||
LFS_Chart_Dirlist_Data(0);
|
||||
}
|
||||
}
|
||||
|
||||
void Timer_5s() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue