Progress with parsers

This commit is contained in:
cschwinne 2020-11-05 22:54:13 +01:00
parent 8ddae6bba0
commit d9050dd8b9
4 changed files with 67 additions and 31 deletions

View File

@ -12,14 +12,22 @@ void getStringFromJson(char* dest, const char* src, size_t len) {
if (src != nullptr) strlcpy(dest, src, len);
}
void deserializeSettings() {
void deserializeConfig() {
bool fromeep = false;
bool success = deserializeConfigSec();
if (!success) { //if file does not exist, try reading from EEPROM
loadSettingsFromEEPROM();
fromeep = true;
}
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DEBUG_PRINTLN(F("Reading settings from /cfg.json..."));
bool success = readObjectFromFile("/cfg.json", nullptr, &doc);
success = readObjectFromFile("/cfg.json", nullptr, &doc);
if (!success) { //if file does not exist, try reading from EEPROM
loadSettingsFromEEPROM();
if (!fromeep) deEEP();
return;
}
//deserializeJson(doc, json);
@ -298,7 +306,7 @@ void deserializeSettings() {
}
}
void serializeSettings() {
void serializeConfig() {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
JsonArray rev = doc.createNestedArray("rev");
@ -561,7 +569,7 @@ void serializeSettings() {
}
//settings in /wsec.json, not accessible via webserver, for passwords and tokens
void deserializeSettingsSec() {
bool deserializeConfigSec() {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
JsonObject nw_ins_0 = doc["nw"]["ins"][0];
@ -570,15 +578,25 @@ void deserializeSettingsSec() {
JsonObject ap = doc["ap"];
getStringFromJson(apPass, ap["psk"] , 65);
//mqtt pass
JsonObject interfaces = doc["if"];
//blynk token
const char* apikey = interfaces["blynk"]["token"] | "Hidden";
int tdd = strnlen(apikey, 36);
if (tdd > 20 || tdd == 0)
getStringFromJson(blynkApiKey, apikey, 36);
//hue token
JsonObject if_mqtt = interfaces["mqtt"];
getStringFromJson(mqttPass, if_mqtt["psk"], 41);
//ota pass
getStringFromJson(hueApiKey, interfaces["hue"]["key"], 47);
JsonObject ota = doc["ota"];
getStringFromJson(otaPass, ota["pwd"], 33);
CJSON(otaLock, ota["lock"]);
CJSON(wifiLock, ota["lock-wifi"]);
CJSON(aOtaEnabled, ota["aota"]);
}
void serializeSettingsSec() {
void serializeConfigSec() {
}

View File

@ -25,6 +25,12 @@ bool isButtonPressed();
void handleButton();
void handleIO();
//cfg.cpp
void deserializeConfig();
bool deserializeConfigSec();
void serializeConfig();
void serializeConfigSec();
//colors.cpp
void colorFromUint32(uint32_t in, bool secondary = false);
void colorFromUint24(uint32_t in, bool secondary = false);
@ -209,6 +215,7 @@ void loadMacro(byte index, char* m);
void applyMacro(byte index);
void saveMacro(byte index, const String& mc, bool persist = true); //only commit on single save, not in settings
void deEEP();
void deEEPSettings();
//wled_serial.cpp
void handleSerial();

View File

@ -172,33 +172,33 @@ void WLED::setup()
DEBUG_PRINTLN(ESP.getFreeHeap());
registerUsermods();
strip.init(EEPROM.read(372), ledCount, EEPROM.read(2204)); // init LEDs quickly
strip.setBrightness(0);
//strip.init(EEPROM.read(372), ledCount, EEPROM.read(2204)); // init LEDs quickly
//strip.setBrightness(0);
DEBUG_PRINT(F("LEDs inited. heap usage ~"));
DEBUG_PRINTLN(heapPreAlloc - ESP.getFreeHeap());
//DEBUG_PRINT(F("LEDs inited. heap usage ~"));
//DEBUG_PRINTLN(heapPreAlloc - ESP.getFreeHeap());
#ifndef WLED_DISABLE_FILESYSTEM
bool fsinit = false;
DEBUGFS_PRINTLN(F("Mount FS"));
#ifdef ARDUINO_ARCH_ESP32
fsinit = WLED_FS.begin(true);
#else
fsinit = WLED_FS.begin();
#endif
if (!fsinit) {
DEBUGFS_PRINTLN(F("FS failed!"));
errorFlag = ERR_FS_BEGIN;
} else deEEP();
updateFSInfo();
bool fsinit = false;
DEBUGFS_PRINTLN(F("Mount FS"));
#ifdef ARDUINO_ARCH_ESP32
fsinit = WLED_FS.begin(true);
#else
fsinit = WLED_FS.begin();
#endif
if (!fsinit) {
DEBUGFS_PRINTLN(F("FS failed!"));
errorFlag = ERR_FS_BEGIN;
} else deEEP();
updateFSInfo();
deserializeConfig();
#if STATUSLED && STATUSLED != LEDPIN
pinMode(STATUSLED, OUTPUT);
#endif
DEBUG_PRINTLN(F("Load EEPROM"));
loadSettingsFromEEPROM();
//DEBUG_PRINTLN(F("Load EEPROM"));
//loadSettingsFromEEPROM();
beginStrip();
userSetup();
usermods.setup();
@ -252,6 +252,8 @@ void WLED::setup()
void WLED::beginStrip()
{
// Initialize NeoPixel Strip and button
strip.init(useRGBW, ledCount, skipFirstLed);
strip.setBrightness(0);
strip.setShowCallback(handleOverlayDraw);
#ifdef BTNPIN

View File

@ -790,7 +790,7 @@ void deEEP() {
JsonObject sObj = dDoc.to<JsonObject>();
sObj.createNestedObject("0");
//EEPROM.begin(EEPSIZE);
EEPROM.begin(EEPSIZE);
if (EEPROM.read(233) == 233) { //valid EEPROM save
for (uint16_t index = 1; index <= 16; index++) { //copy presets to presets.json
uint16_t i = 380 + index*20;
@ -861,7 +861,7 @@ void deEEP() {
}
}
//EEPROM.end();
EEPROM.end();
File f = WLED_FS.open("/presets.json", "w");
if (!f) {
@ -871,4 +871,13 @@ void deEEP() {
serializeJson(dDoc, f);
f.close();
DEBUG_PRINTLN(F("deEEP complete!"));
}
void deEEPSettings() {
DEBUG_PRINTLN(F("Restore settings from EEPROM"));
EEPROM.begin(EEPSIZE);
loadSettingsFromEEPROM();
EEPROM.end();
serializeConfig();
}