fileDoc removal (optimisation)

This commit is contained in:
Blaz Kristan 2024-03-26 17:18:52 +01:00
parent d1d45e7166
commit fd149b3f46
6 changed files with 17 additions and 24 deletions

View File

@ -303,7 +303,7 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
return true; return true;
} }
// deserializes WLED state (fileDoc points to doc object if called from web server) // deserializes WLED state
// presetId is non-0 if called from handlePreset() // presetId is non-0 if called from handlePreset()
bool deserializeState(JsonObject root, byte callMode, byte presetId) bool deserializeState(JsonObject root, byte callMode, byte presetId)
{ {

View File

@ -125,8 +125,7 @@ int16_t loadPlaylist(JsonObject playlistObj, byte presetId) {
void handlePlaylist() { void handlePlaylist() {
static unsigned long presetCycledTime = 0; static unsigned long presetCycledTime = 0;
// if fileDoc is not null JSON buffer is in use so just quit if (currentPlaylist < 0 || playlistEntries == nullptr) return;
if (currentPlaylist < 0 || playlistEntries == nullptr || fileDoc != nullptr) return;
if (millis() - presetCycledTime > (100*playlistEntryDur)) { if (millis() - presetCycledTime > (100*playlistEntryDur)) {
presetCycledTime = millis(); presetCycledTime = millis();

View File

@ -27,7 +27,7 @@ static void doSaveState() {
unsigned long start = millis(); unsigned long start = millis();
while (strip.isUpdating() && millis()-start < (2*FRAMETIME_FIXED)+1) yield(); // wait 2 frames while (strip.isUpdating() && millis()-start < (2*FRAMETIME_FIXED)+1) yield(); // wait 2 frames
if (!requestJSONBufferLock(10)) return; // will set fileDoc if (!requestJSONBufferLock(10)) return;
initPresetsFile(); // just in case if someone deleted presets.json using /edit initPresetsFile(); // just in case if someone deleted presets.json using /edit
JsonObject sObj = pDoc->to<JsonObject>(); JsonObject sObj = pDoc->to<JsonObject>();
@ -53,7 +53,7 @@ static void doSaveState() {
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
if (!persist) { if (!persist) {
if (tmpRAMbuffer!=nullptr) free(tmpRAMbuffer); if (tmpRAMbuffer!=nullptr) free(tmpRAMbuffer);
size_t len = measureJson(*fileDoc) + 1; size_t len = measureJson(*pDoc) + 1;
DEBUG_PRINTLN(len); DEBUG_PRINTLN(len);
// if possible use SPI RAM on ESP32 // if possible use SPI RAM on ESP32
if (psramFound()) if (psramFound())
@ -61,13 +61,13 @@ static void doSaveState() {
else else
tmpRAMbuffer = (char*) malloc(len); tmpRAMbuffer = (char*) malloc(len);
if (tmpRAMbuffer!=nullptr) { if (tmpRAMbuffer!=nullptr) {
serializeJson(*fileDoc, tmpRAMbuffer, len); serializeJson(*pDoc, tmpRAMbuffer, len);
} else { } else {
writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, fileDoc); writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, pDoc);
} }
} else } else
#endif #endif
writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, fileDoc); writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, pDoc);
if (persist) presetsModifiedTime = toki.second(); //unix time if (persist) presetsModifiedTime = toki.second(); //unix time
releaseJSONBufferLock(); releaseJSONBufferLock();
@ -152,7 +152,7 @@ void handlePresets()
return; return;
} }
if (presetToApply == 0 || fileDoc) return; // no preset waiting to apply, or JSON buffer is already allocated, return to loop until free if (presetToApply == 0 || !requestJSONBufferLock(9)) return; // no preset waiting to apply, or JSON buffer is already allocated, return to loop until free
bool changePreset = false; bool changePreset = false;
uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset() uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()
@ -160,9 +160,6 @@ void handlePresets()
JsonObject fdo; JsonObject fdo;
// allocate buffer
if (!requestJSONBufferLock(9)) return; // will also assign fileDoc
presetToApply = 0; //clear request for preset presetToApply = 0; //clear request for preset
callModeToApply = 0; callModeToApply = 0;
@ -171,14 +168,14 @@ void handlePresets()
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
if (tmpPreset==255 && tmpRAMbuffer!=nullptr) { if (tmpPreset==255 && tmpRAMbuffer!=nullptr) {
deserializeJson(*fileDoc,tmpRAMbuffer); deserializeJson(*pDoc,tmpRAMbuffer);
errorFlag = ERR_NONE; errorFlag = ERR_NONE;
} else } else
#endif #endif
{ {
errorFlag = readObjectFromFileUsingId(getPresetsFileName(tmpPreset < 255), tmpPreset, fileDoc) ? ERR_NONE : ERR_FS_PLOAD; errorFlag = readObjectFromFileUsingId(getPresetsFileName(tmpPreset < 255), tmpPreset, pDoc) ? ERR_NONE : ERR_FS_PLOAD;
} }
fdo = fileDoc->as<JsonObject>(); fdo = pDoc->as<JsonObject>();
//HTTP API commands //HTTP API commands
const char* httpwin = fdo["win"]; const char* httpwin = fdo["win"];
@ -205,13 +202,13 @@ void handlePresets()
} }
#endif #endif
releaseJSONBufferLock(); // will also clear fileDoc releaseJSONBufferLock();
if (changePreset) notify(tmpMode); // force UDP notification if (changePreset) notify(tmpMode); // force UDP notification
stateUpdated(tmpMode); // was colorUpdated() if anything breaks stateUpdated(tmpMode); // was colorUpdated() if anything breaks
updateInterfaces(tmpMode); updateInterfaces(tmpMode);
} }
//called from handleSet(PS=) [network callback (fileDoc==nullptr), IR (irrational), deserializeState, UDP] and deserializeState() [network callback (filedoc!=nullptr)] //called from handleSet(PS=) [network callback (sObj is empty), IR (irrational), deserializeState, UDP] and deserializeState() [network callback (filedoc!=nullptr)]
void savePreset(byte index, const char* pname, JsonObject sObj) void savePreset(byte index, const char* pname, JsonObject sObj)
{ {
if (!saveName) saveName = new char[33]; if (!saveName) saveName = new char[33];
@ -249,7 +246,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
if (sObj[F("playlist")].isNull()) { if (sObj[F("playlist")].isNull()) {
// we will save API call immediately (often causes presets.json corruption) // we will save API call immediately (often causes presets.json corruption)
presetToSave = 0; presetToSave = 0;
if (index <= 250 && fileDoc) { // cannot save API calls to temporary preset (255) if (index <= 250) { // cannot save API calls to temporary preset (255)
sObj.remove("o"); sObj.remove("o");
sObj.remove("v"); sObj.remove("v");
sObj.remove("time"); sObj.remove("time");
@ -257,7 +254,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
sObj.remove(F("psave")); sObj.remove(F("psave"));
if (sObj["n"].isNull()) sObj["n"] = saveName; if (sObj["n"].isNull()) sObj["n"] = saveName;
initPresetsFile(); // just in case if someone deleted presets.json using /edit initPresetsFile(); // just in case if someone deleted presets.json using /edit
writeObjectToFileUsingId(getPresetsFileName(), index, fileDoc); writeObjectToFileUsingId(getPresetsFileName(), index, pDoc);
presetsModifiedTime = toki.second(); //unix time presetsModifiedTime = toki.second(); //unix time
updateFSInfo(); updateFSInfo();
} }

View File

@ -228,7 +228,6 @@ bool requestJSONBufferLock(uint8_t module)
DEBUG_PRINT(F("JSON buffer locked. (")); DEBUG_PRINT(F("JSON buffer locked. ("));
DEBUG_PRINT(jsonBufferLock); DEBUG_PRINT(jsonBufferLock);
DEBUG_PRINTLN(")"); DEBUG_PRINTLN(")");
fileDoc = pDoc; // used for applying presets (presets.cpp)
pDoc->clear(); pDoc->clear();
return true; return true;
} }
@ -239,7 +238,6 @@ void releaseJSONBufferLock()
DEBUG_PRINT(F("JSON buffer released. (")); DEBUG_PRINT(F("JSON buffer released. ("));
DEBUG_PRINT(jsonBufferLock); DEBUG_PRINT(jsonBufferLock);
DEBUG_PRINTLN(")"); DEBUG_PRINTLN(")");
fileDoc = nullptr;
jsonBufferLock = 0; jsonBufferLock = 0;
} }

View File

@ -8,7 +8,7 @@
*/ */
// version code in format yymmddb (b = daily build) // version code in format yymmddb (b = daily build)
#define VERSION 2403240 #define VERSION 2403260
//uncomment this if you have a "my_config.h" file you'd like to use //uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG //#define WLED_USE_MY_CONFIG
@ -692,7 +692,6 @@ WLED_GLOBAL uint16_t olen _INIT(0);
WLED_GLOBAL size_t fsBytesUsed _INIT(0); WLED_GLOBAL size_t fsBytesUsed _INIT(0);
WLED_GLOBAL size_t fsBytesTotal _INIT(0); WLED_GLOBAL size_t fsBytesTotal _INIT(0);
WLED_GLOBAL unsigned long presetsModifiedTime _INIT(0L); WLED_GLOBAL unsigned long presetsModifiedTime _INIT(0L);
WLED_GLOBAL JsonDocument* fileDoc;
WLED_GLOBAL bool doCloseFile _INIT(false); WLED_GLOBAL bool doCloseFile _INIT(false);
// presets // presets

View File

@ -55,7 +55,7 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
} else { } else {
verboseResponse = deserializeState(root); verboseResponse = deserializeState(root);
} }
releaseJSONBufferLock(); // will clean fileDoc releaseJSONBufferLock();
if (!interfaceUpdateCallMode) { // individual client response only needed if no WS broadcast soon if (!interfaceUpdateCallMode) { // individual client response only needed if no WS broadcast soon
if (verboseResponse) { if (verboseResponse) {