41 lines
1.2 KiB
C++
41 lines
1.2 KiB
C++
#ifndef STORAGE_H
|
|
#define STORAGE_H
|
|
#include <Arduino.h>
|
|
#include <LittleFS.h> //Built-in ESP32 library for file system management
|
|
|
|
class Storage {
|
|
public:
|
|
/**
|
|
* @brief Initialice LittleFS and read configuration file.
|
|
* @return String with the JSON content or an empty string if the file does not exist or cannot be read.
|
|
*/
|
|
static String readConfigFile() {
|
|
if(!LittleFS.begin(true)) {
|
|
Serial.println("[Storage] Error to initialize LittleFS");
|
|
return "";
|
|
}
|
|
if (!LittleFS.exists("/config.json")) {
|
|
Serial.println("[Storage] Configuration file does not exist");
|
|
return "";
|
|
}
|
|
File file = LittleFS.open("/config.json", "r");
|
|
if (!file) {
|
|
Serial.println("[Storage] Error opening configuration file");
|
|
return "";
|
|
}
|
|
String content = file.readString();
|
|
file.close();
|
|
return content;
|
|
}
|
|
static void saveConfigFile(const String& jsonConfig) {
|
|
File file = LittleFS.open("/config.json", "w");
|
|
if (!file) {
|
|
Serial.println("[Storage] Error opening configuration file for writing");
|
|
return;
|
|
}
|
|
file.print(jsonConfig);
|
|
file.close();
|
|
Serial.println("[Storage] Configuration file saved");
|
|
}
|
|
};
|
|
#endif |