160 lines
4.4 KiB
C++
160 lines
4.4 KiB
C++
#include "config.h"
|
|
|
|
#include <LittleFS.h>
|
|
|
|
enum CONFIG_LOG {
|
|
CONFIG_LOG_FALLBACK,
|
|
CONFIG_LOG_READ,
|
|
CONFIG_LOG_UNCHANGED,
|
|
CONFIG_LOG_WRITE,
|
|
};
|
|
|
|
void listDir(const String &path, const String &indent) {
|
|
auto dir = LittleFS.open(path, "r");
|
|
if (!dir || !dir.isDirectory()) {
|
|
return;
|
|
}
|
|
while (true) {
|
|
File child = dir.openNextFile();
|
|
if (!child) {
|
|
break;
|
|
}
|
|
if (child.isDirectory()) {
|
|
Serial.printf("%s[D] [ ] %s\n", indent.c_str(), child.name());
|
|
#ifdef ESP32
|
|
listDir(child.path(), indent + " ");
|
|
#endif
|
|
#ifdef ESP8266
|
|
listDir(child.fullName(), indent + " ");
|
|
#endif
|
|
}
|
|
child.close();
|
|
}
|
|
dir.rewindDirectory();
|
|
while (true) {
|
|
File child = dir.openNextFile();
|
|
if (!child) {
|
|
break;
|
|
}
|
|
if (!child.isDirectory()) {
|
|
Serial.printf("%s[F] [%4d] %s\n", indent.c_str(), child.size(), child.name());
|
|
}
|
|
child.close();
|
|
}
|
|
dir.close();
|
|
}
|
|
|
|
void configSetup() {
|
|
#ifdef ESP32
|
|
LittleFS.begin(true)
|
|
#endif
|
|
#ifdef ESP8266
|
|
if (!LittleFS.begin()) {
|
|
LittleFS.format();
|
|
LittleFS.begin();
|
|
}
|
|
#endif
|
|
Serial.println("Filesystem-content:");
|
|
listDir("/", "");
|
|
}
|
|
|
|
File configOpen(const String &path, const bool write) {
|
|
if (!write && !LittleFS.exists(path)) {
|
|
return {};
|
|
}
|
|
#ifdef ESP32
|
|
return LittleFS.open(path, write ? "w" : "r", write);
|
|
#endif
|
|
#ifdef ESP8266
|
|
return LittleFS.open(path, write ? "w" : "r");
|
|
#endif
|
|
}
|
|
|
|
void doLog(const String &path, const String &value, const bool isPassword, const CONFIG_LOG type, const bool enable) {
|
|
if (!enable) {
|
|
return;
|
|
}
|
|
Serial.printf(
|
|
"[CONFIG] %-20s = %-30s [%s]\n",
|
|
path.c_str(),
|
|
isPassword ? "*" : value.c_str(),
|
|
type == CONFIG_LOG_FALLBACK ? "fallback" : type == CONFIG_LOG_READ ? "READ" : type == CONFIG_LOG_UNCHANGED ? "UNCHANGED" : type == CONFIG_LOG_WRITE ? "WRITE" : ""
|
|
);
|
|
}
|
|
|
|
long configRead(const String &path, const long fallback, const bool log) {
|
|
if (auto file = configOpen(path, false)) {
|
|
const auto content = file.readString();
|
|
file.close();
|
|
const auto value = content.toInt();
|
|
doLog(path, String(value), false, CONFIG_LOG_READ, log);
|
|
return value;
|
|
}
|
|
doLog(path, String(fallback), false, CONFIG_LOG_FALLBACK, log);
|
|
return fallback;
|
|
}
|
|
|
|
bool configWrite(const String &path, const long fallback, const long value) {
|
|
if (configRead(path, fallback, false) == value) {
|
|
doLog(path, String(value), false, CONFIG_LOG_UNCHANGED, true);
|
|
return false;
|
|
}
|
|
if (auto file = configOpen(path, true)) {
|
|
const auto content = String(value);
|
|
file.write(reinterpret_cast<const uint8_t *>(content.c_str()), content.length());
|
|
file.close();
|
|
doLog(path, String(value), false, CONFIG_LOG_WRITE, true);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool configRead(const String &path, const bool fallback) {
|
|
return configRead(path, fallback ? 1L : 0L) > 0;
|
|
}
|
|
|
|
bool configWrite(const String &path, const bool fallback, const bool value) {
|
|
return configWrite(path, fallback ? 1L : 0L, value ? 1L : 0L);
|
|
}
|
|
|
|
String configRead(const String &path, const char *fallback) {
|
|
return configRead(path, String(fallback));
|
|
}
|
|
|
|
bool configWrite(const String &path, const char *fallback, const char *value) {
|
|
return configWrite(path, String(fallback), String(value), false);
|
|
}
|
|
|
|
String configRead(const String &path, const String &fallback, const bool log, const bool isPassword) {
|
|
if (auto file = configOpen(path, false)) {
|
|
const auto value = file.readString();
|
|
file.close();
|
|
doLog(path, value.c_str(), isPassword, CONFIG_LOG_READ, log);
|
|
return value;
|
|
}
|
|
doLog(path, fallback.c_str(), isPassword, CONFIG_LOG_FALLBACK, log);
|
|
return fallback;
|
|
}
|
|
|
|
bool configWrite(const String &path, const String &fallback, const String &value, const bool isPassword) {
|
|
if (configRead(path, fallback, false) == value) {
|
|
doLog(path, value.c_str(), isPassword, CONFIG_LOG_UNCHANGED, true);
|
|
return false;
|
|
}
|
|
if (auto file = configOpen(path, true)) {
|
|
file.write(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
|
|
file.close();
|
|
doLog(path, value.c_str(), isPassword, CONFIG_LOG_WRITE, true);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
Initial configRead(const String &path, const Initial &fallback) {
|
|
return stringToInitial(configRead(path, initialToString(fallback)));
|
|
}
|
|
|
|
bool configWrite(const String &path, const Initial &fallback, const Initial &value) {
|
|
return configWrite(path, initialToString(fallback), initialToString(value));
|
|
}
|