config persistence FIX + FS REFACTOR
This commit is contained in:
parent
70bcffe505
commit
7345b125ee
24
src/Relay.h
24
src/Relay.h
@ -18,31 +18,39 @@ public:
|
|||||||
|
|
||||||
void setup() override {
|
void setup() override {
|
||||||
Output::setup();
|
Output::setup();
|
||||||
Output::setName(configRead(String("relayName") + index, nameFallback));
|
Output::setName(configRead(path("name"), nameFallback));
|
||||||
Output::setInitial(configRead(String("relayInitial") + index, INITIAL_OFF));
|
Output::setInitial(configRead(path("initial"), INITIAL_OFF));
|
||||||
Output::setOnMillis(configRead(String("relayOnMillis") + index, 0L));
|
Output::setOnMillis(configRead(path("onMillis"), 0L));
|
||||||
Output::setOffMillis(configRead(String("relayOffMillis") + index, 0L));
|
Output::setOffMillis(configRead(path("offMillis"), 0L));
|
||||||
_applyInitial();
|
_applyInitial();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setName(const String &value) override {
|
void setName(const String &value) override {
|
||||||
Output::setName(value);
|
Output::setName(value);
|
||||||
configWrite(String("relayName") + index, nameFallback, value);
|
configWrite(path("name"), nameFallback, value, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setInitial(const Initial value) override {
|
void setInitial(const Initial value) override {
|
||||||
Output::setInitial(value);
|
Output::setInitial(value);
|
||||||
configWrite(String("relayInitial") + index, INITIAL_OFF, value);
|
configWrite(path("initial"), INITIAL_OFF, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOnMillis(const unsigned long value) override {
|
void setOnMillis(const unsigned long value) override {
|
||||||
Output::setOnMillis(value);
|
Output::setOnMillis(value);
|
||||||
configWrite(String("relayOnMillis") + index, 0L, value);
|
configWrite(path("onMillis"), 0L, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOffMillis(const unsigned long value) override {
|
void setOffMillis(const unsigned long value) override {
|
||||||
Output::setOffMillis(value);
|
Output::setOffMillis(value);
|
||||||
configWrite(String("relayOffMillis") + index, 0L, value);
|
configWrite(path("offMillis"), 0L, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
String path(const char *name) const {
|
||||||
|
char path[64];
|
||||||
|
snprintf(path, sizeof(path), "/relay%d/%s", index, name);
|
||||||
|
return String(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
124
src/config.cpp
124
src/config.cpp
@ -1,83 +1,141 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
#include <sys/stat.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());
|
||||||
|
listDir(child.path(), indent + " ");
|
||||||
|
}
|
||||||
|
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() {
|
void configSetup() {
|
||||||
LittleFS.begin();
|
LittleFS.begin(true);
|
||||||
|
Serial.println("Filesystem-content:");
|
||||||
|
listDir("/", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
File configOpen(const String &name, const char *mode) {
|
File configOpen(const String &path, const bool write) {
|
||||||
char path[64];
|
if (!write && !LittleFS.exists(path)) {
|
||||||
snprintf(path, sizeof(path), "/%s", name.c_str());
|
return {};
|
||||||
return LittleFS.open(path, mode);
|
}
|
||||||
|
return LittleFS.open(path, write ? "w" : "r", write);
|
||||||
}
|
}
|
||||||
|
|
||||||
long configRead(const String &name, const long fallback) {
|
void doLog(const String &path, const String &value, const bool isPassword, const CONFIG_LOG type, const bool enable) {
|
||||||
if (auto file = configOpen(name, "r")) {
|
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();
|
const auto content = file.readString();
|
||||||
file.close();
|
file.close();
|
||||||
return content.toInt();
|
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;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const long fallback, const long value) {
|
bool configWrite(const String &path, const long fallback, const long value) {
|
||||||
if (configRead(name, fallback) == value) {
|
if (configRead(path, fallback, false) == value) {
|
||||||
|
doLog(path, String(value), false, CONFIG_LOG_UNCHANGED, true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Serial.printf("[CONFIG] \"%s\" = \"%ld\"\n", name.c_str(), value);
|
if (auto file = configOpen(path, true)) {
|
||||||
if (auto file = configOpen(name, "w")) {
|
|
||||||
const auto content = String(value);
|
const auto content = String(value);
|
||||||
file.write(reinterpret_cast<const uint8_t *>(content.c_str()), content.length());
|
file.write(reinterpret_cast<const uint8_t *>(content.c_str()), content.length());
|
||||||
file.close();
|
file.close();
|
||||||
|
doLog(path, String(value), false, CONFIG_LOG_WRITE, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configRead(const String &name, const bool fallback) {
|
bool configRead(const String &path, const bool fallback) {
|
||||||
return configRead(name, fallback ? 1L : 0L) > 0;
|
return configRead(path, fallback ? 1L : 0L) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const bool fallback, const bool value) {
|
bool configWrite(const String &path, const bool fallback, const bool value) {
|
||||||
return configWrite(name, fallback ? 1L : 0L, value ? 1L : 0L);
|
return configWrite(path, fallback ? 1L : 0L, value ? 1L : 0L);
|
||||||
}
|
}
|
||||||
|
|
||||||
String configRead(const String &name, const char *fallback) {
|
String configRead(const String &path, const char *fallback) {
|
||||||
return configRead(name, String(fallback));
|
return configRead(path, String(fallback));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const char *fallback, const char *value) {
|
bool configWrite(const String &path, const char *fallback, const char *value) {
|
||||||
return configWrite(name, String(fallback), String(value));
|
return configWrite(path, String(fallback), String(value), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
String configRead(const String &name, const String &fallback) {
|
String configRead(const String &path, const String &fallback, const bool log, const bool isPassword) {
|
||||||
if (auto file = configOpen(name, "r")) {
|
if (auto file = configOpen(path, false)) {
|
||||||
const auto content = file.readString();
|
const auto value = file.readString();
|
||||||
file.close();
|
file.close();
|
||||||
return content;
|
doLog(path, value.c_str(), isPassword, CONFIG_LOG_READ, log);
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
doLog(path, fallback.c_str(), isPassword, CONFIG_LOG_FALLBACK, log);
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const String &fallback, const String &value) {
|
bool configWrite(const String &path, const String &fallback, const String &value, const bool isPassword) {
|
||||||
if (configRead(name, fallback) == value) {
|
if (configRead(path, fallback, false) == value) {
|
||||||
|
doLog(path, value.c_str(), isPassword, CONFIG_LOG_UNCHANGED, true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Serial.printf("[CONFIG] \"%s\" = \"%s\"\n", name.c_str(), value.c_str());
|
if (auto file = configOpen(path, true)) {
|
||||||
if (auto file = configOpen(name, "w")) {
|
|
||||||
file.write(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
|
file.write(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
|
||||||
file.close();
|
file.close();
|
||||||
|
doLog(path, value.c_str(), isPassword, CONFIG_LOG_WRITE, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Initial configRead(const String &name, const Initial &fallback) {
|
Initial configRead(const String &path, const Initial &fallback) {
|
||||||
return stringToInitial(configRead(name, initialToString(fallback)));
|
return stringToInitial(configRead(path, initialToString(fallback)));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const Initial &fallback, const Initial &value) {
|
bool configWrite(const String &path, const Initial &fallback, const Initial &value) {
|
||||||
return configWrite(name, initialToString(fallback), initialToString(value));
|
return configWrite(path, initialToString(fallback), initialToString(value));
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/config.h
20
src/config.h
@ -7,24 +7,24 @@
|
|||||||
|
|
||||||
void configSetup();
|
void configSetup();
|
||||||
|
|
||||||
long configRead(const String &name, long fallback);
|
long configRead(const String &path, long fallback, bool log = true);
|
||||||
|
|
||||||
bool configWrite(const String &name, long fallback, long value);
|
bool configWrite(const String &path, long fallback, long value);
|
||||||
|
|
||||||
bool configRead(const String &name, bool fallback);
|
bool configRead(const String &path, bool fallback);
|
||||||
|
|
||||||
bool configWrite(const String &name, bool fallback, bool value);
|
bool configWrite(const String &path, bool fallback, bool value);
|
||||||
|
|
||||||
String configRead(const String &name, const char *fallback);
|
String configRead(const String &path, const char *fallback);
|
||||||
|
|
||||||
bool configWrite(const String &name, const char *fallback, const char *value);
|
bool configWrite(const String &path, const char *fallback, const char *value);
|
||||||
|
|
||||||
String configRead(const String &name, const String &fallback);
|
String configRead(const String &path, const String &fallback, bool log = true, bool isPassword = false);
|
||||||
|
|
||||||
bool configWrite(const String &name, const String &fallback, const String &value);
|
bool configWrite(const String &path, const String &fallback, const String &value, bool isPassword);
|
||||||
|
|
||||||
Initial configRead(const String &name, const Initial &fallback);
|
Initial configRead(const String &path, const Initial &fallback);
|
||||||
|
|
||||||
bool configWrite(const String &name, const Initial &fallback, const Initial &value);
|
bool configWrite(const String &path, const Initial &fallback, const Initial &value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
16
src/wifi.cpp
16
src/wifi.cpp
@ -3,9 +3,9 @@
|
|||||||
#include "http.h"
|
#include "http.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
|
||||||
#define CONFIG_HOSTNAME "hostname"
|
#define CONFIG_HOSTNAME "/wifi/hostname"
|
||||||
#define CONFIG_WIFI_SSID "wifiSSID"
|
#define CONFIG_WIFI_SSID "/wifi/ssid"
|
||||||
#define CONFIG_WIFI_PASSWORD "wifiPassword"
|
#define CONFIG_WIFI_PASSWORD "/wifi/password"
|
||||||
|
|
||||||
#define DEFAULT_HOSTNAME "PatrixSonoff4ChPro"
|
#define DEFAULT_HOSTNAME "PatrixSonoff4ChPro"
|
||||||
#define DEFAULT_WIFI_SSID "HappyNet"
|
#define DEFAULT_WIFI_SSID "HappyNet"
|
||||||
@ -28,7 +28,7 @@ void wifiConnect() {
|
|||||||
|
|
||||||
const auto hostname = configRead(CONFIG_HOSTNAME, DEFAULT_HOSTNAME);
|
const auto hostname = configRead(CONFIG_HOSTNAME, DEFAULT_HOSTNAME);
|
||||||
const auto wifiSSID = configRead(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID);
|
const auto wifiSSID = configRead(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID);
|
||||||
const auto wifiPass = configRead(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD);
|
const auto wifiPass = configRead(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, true, true);
|
||||||
|
|
||||||
WiFi.hostname(hostname);
|
WiFi.hostname(hostname);
|
||||||
WiFi.begin(wifiSSID.c_str(), wifiPass.c_str());
|
WiFi.begin(wifiSSID.c_str(), wifiPass.c_str());
|
||||||
@ -66,19 +66,15 @@ void wifiLoop() {
|
|||||||
void wifiChangeHostname(const char *hostname) {
|
void wifiChangeHostname(const char *hostname) {
|
||||||
if (configWrite(CONFIG_HOSTNAME, DEFAULT_HOSTNAME, hostname)) {
|
if (configWrite(CONFIG_HOSTNAME, DEFAULT_HOSTNAME, hostname)) {
|
||||||
WiFi.setHostname(hostname);
|
WiFi.setHostname(hostname);
|
||||||
Serial.printf("[WiFi] hostname: %s\n", hostname);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiChangeSSID(const char *ssid) {
|
void wifiChangeSSID(const char *ssid) {
|
||||||
if (configWrite(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID, ssid)) {
|
configWrite(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID, ssid);
|
||||||
Serial.printf("[WiFi] SSID: %s\n", ssid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiChangePassword(const char *password) {
|
void wifiChangePassword(const char *password) {
|
||||||
configWrite(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, password);
|
configWrite(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, password, true);
|
||||||
Serial.printf("[WiFi] password: ***");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiSetup() {
|
void wifiSetup() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user