Add persistent configuration storage
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
#include "app_config.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <Preferences.h>
|
||||
|
||||
AppConfig appConfig;
|
||||
|
||||
static Preferences preferences;
|
||||
|
||||
void loadDefaultConfig() {
|
||||
appConfig.deviceName = DEVICE_NAME;
|
||||
|
||||
@@ -22,40 +26,12 @@ void loadDefaultConfig() {
|
||||
|
||||
appConfig.tempSensorCount = 4;
|
||||
|
||||
appConfig.tempSensors[0] = {
|
||||
"temp_1",
|
||||
"Temperature 1",
|
||||
"",
|
||||
true
|
||||
};
|
||||
|
||||
appConfig.tempSensors[1] = {
|
||||
"temp_2",
|
||||
"Temperature 2",
|
||||
"",
|
||||
true
|
||||
};
|
||||
|
||||
appConfig.tempSensors[2] = {
|
||||
"temp_3",
|
||||
"Temperature 3",
|
||||
"",
|
||||
true
|
||||
};
|
||||
|
||||
appConfig.tempSensors[3] = {
|
||||
"temp_4",
|
||||
"Temperature 4",
|
||||
"",
|
||||
true
|
||||
};
|
||||
|
||||
for (int i = 4; i < MAX_TEMP_SENSORS; i++) {
|
||||
for (int i = 0; i < MAX_TEMP_SENSORS; i++) {
|
||||
appConfig.tempSensors[i] = {
|
||||
"temp_" + String(i + 1),
|
||||
"Temperature " + String(i + 1),
|
||||
"",
|
||||
false
|
||||
i < appConfig.tempSensorCount
|
||||
};
|
||||
}
|
||||
|
||||
@@ -65,6 +41,108 @@ void loadDefaultConfig() {
|
||||
appConfig.bms.addressType = "public";
|
||||
}
|
||||
|
||||
void loadConfig() {
|
||||
loadDefaultConfig();
|
||||
|
||||
preferences.begin("xterra", true);
|
||||
|
||||
bool configured = preferences.getBool("configured", false);
|
||||
|
||||
if (configured) {
|
||||
appConfig.deviceName = preferences.getString("device", appConfig.deviceName);
|
||||
|
||||
for (int i = 0; i < MAX_RELAYS; i++) {
|
||||
String prefix = "r" + String(i + 1) + "_";
|
||||
appConfig.relays[i].name = preferences.getString(
|
||||
(prefix + "name").c_str(),
|
||||
appConfig.relays[i].name
|
||||
);
|
||||
appConfig.relays[i].enabled = preferences.getBool(
|
||||
(prefix + "enabled").c_str(),
|
||||
appConfig.relays[i].enabled
|
||||
);
|
||||
}
|
||||
|
||||
appConfig.tempSensorCount = preferences.getInt(
|
||||
"temp_count",
|
||||
appConfig.tempSensorCount
|
||||
);
|
||||
|
||||
if (appConfig.tempSensorCount < 0) {
|
||||
appConfig.tempSensorCount = 0;
|
||||
}
|
||||
|
||||
if (appConfig.tempSensorCount > MAX_TEMP_SENSORS) {
|
||||
appConfig.tempSensorCount = MAX_TEMP_SENSORS;
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_TEMP_SENSORS; i++) {
|
||||
String prefix = "t" + String(i + 1) + "_";
|
||||
appConfig.tempSensors[i].name = preferences.getString(
|
||||
(prefix + "name").c_str(),
|
||||
appConfig.tempSensors[i].name
|
||||
);
|
||||
appConfig.tempSensors[i].address = preferences.getString(
|
||||
(prefix + "addr").c_str(),
|
||||
appConfig.tempSensors[i].address
|
||||
);
|
||||
appConfig.tempSensors[i].enabled = preferences.getBool(
|
||||
(prefix + "enabled").c_str(),
|
||||
appConfig.tempSensors[i].enabled
|
||||
);
|
||||
}
|
||||
|
||||
appConfig.bms.enabled = preferences.getBool("bms_enabled", appConfig.bms.enabled);
|
||||
appConfig.bms.name = preferences.getString("bms_name", appConfig.bms.name);
|
||||
appConfig.bms.address = preferences.getString("bms_addr", appConfig.bms.address);
|
||||
appConfig.bms.addressType = preferences.getString("bms_type", appConfig.bms.addressType);
|
||||
}
|
||||
|
||||
preferences.end();
|
||||
|
||||
if (!configured) {
|
||||
saveConfig();
|
||||
}
|
||||
}
|
||||
|
||||
void saveConfig() {
|
||||
preferences.begin("xterra", false);
|
||||
|
||||
preferences.putBool("configured", true);
|
||||
preferences.putString("device", appConfig.deviceName);
|
||||
|
||||
for (int i = 0; i < MAX_RELAYS; i++) {
|
||||
String prefix = "r" + String(i + 1) + "_";
|
||||
preferences.putString((prefix + "name").c_str(), appConfig.relays[i].name);
|
||||
preferences.putBool((prefix + "enabled").c_str(), appConfig.relays[i].enabled);
|
||||
}
|
||||
|
||||
preferences.putInt("temp_count", appConfig.tempSensorCount);
|
||||
|
||||
for (int i = 0; i < MAX_TEMP_SENSORS; i++) {
|
||||
String prefix = "t" + String(i + 1) + "_";
|
||||
preferences.putString((prefix + "name").c_str(), appConfig.tempSensors[i].name);
|
||||
preferences.putString((prefix + "addr").c_str(), appConfig.tempSensors[i].address);
|
||||
preferences.putBool((prefix + "enabled").c_str(), appConfig.tempSensors[i].enabled);
|
||||
}
|
||||
|
||||
preferences.putBool("bms_enabled", appConfig.bms.enabled);
|
||||
preferences.putString("bms_name", appConfig.bms.name);
|
||||
preferences.putString("bms_addr", appConfig.bms.address);
|
||||
preferences.putString("bms_type", appConfig.bms.addressType);
|
||||
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
void factoryResetConfig() {
|
||||
preferences.begin("xterra", false);
|
||||
preferences.clear();
|
||||
preferences.end();
|
||||
|
||||
loadDefaultConfig();
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
void printConfig() {
|
||||
Serial.println("Config:");
|
||||
Serial.print(" Device: ");
|
||||
@@ -77,7 +155,21 @@ void printConfig() {
|
||||
Serial.print(" / ");
|
||||
Serial.print(appConfig.relays[i].name);
|
||||
Serial.print(" / GPIO ");
|
||||
Serial.println(appConfig.relays[i].pin);
|
||||
Serial.print(appConfig.relays[i].pin);
|
||||
Serial.print(" / enabled ");
|
||||
Serial.println(appConfig.relays[i].enabled ? "true" : "false");
|
||||
}
|
||||
|
||||
Serial.println(" Temperature Sensors:");
|
||||
for (int i = 0; i < MAX_TEMP_SENSORS; i++) {
|
||||
Serial.print(" ");
|
||||
Serial.print(appConfig.tempSensors[i].id);
|
||||
Serial.print(" / ");
|
||||
Serial.print(appConfig.tempSensors[i].name);
|
||||
Serial.print(" / enabled ");
|
||||
Serial.print(appConfig.tempSensors[i].enabled ? "true" : "false");
|
||||
Serial.print(" / address ");
|
||||
Serial.println(appConfig.tempSensors[i].address);
|
||||
}
|
||||
|
||||
Serial.println(" BMS:");
|
||||
@@ -87,4 +179,6 @@ void printConfig() {
|
||||
Serial.println(appConfig.bms.name);
|
||||
Serial.print(" Address: ");
|
||||
Serial.println(appConfig.bms.address);
|
||||
Serial.print(" Address Type: ");
|
||||
Serial.println(appConfig.bms.addressType);
|
||||
}
|
||||
|
||||
@@ -37,4 +37,7 @@ struct AppConfig {
|
||||
extern AppConfig appConfig;
|
||||
|
||||
void loadDefaultConfig();
|
||||
void loadConfig();
|
||||
void saveConfig();
|
||||
void factoryResetConfig();
|
||||
void printConfig();
|
||||
|
||||
@@ -298,6 +298,17 @@ void handleDebugSerial() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (command == "config") {
|
||||
printConfig();
|
||||
return;
|
||||
}
|
||||
|
||||
if (command == "factory reset") {
|
||||
factoryResetConfig();
|
||||
Serial.println("OK factory reset complete");
|
||||
return;
|
||||
}
|
||||
|
||||
if (command.length() > 0) {
|
||||
Serial.print("Unknown command: ");
|
||||
Serial.println(command);
|
||||
@@ -362,12 +373,12 @@ void setup() {
|
||||
Serial.println();
|
||||
Serial.println("==================================");
|
||||
|
||||
loadDefaultConfig();
|
||||
loadConfig();
|
||||
printConfig();
|
||||
Serial.println("Xterra Controller Booting");
|
||||
Serial.println("==================================");
|
||||
|
||||
loadDefaultConfig();
|
||||
loadConfig();
|
||||
printConfig();
|
||||
|
||||
pinMode(IGNITION_PIN, INPUT);
|
||||
|
||||
Reference in New Issue
Block a user