fileDoc removal (optimisation)

pull/3862/head
Blaz Kristan 2024-03-26 17:18:52 +01:00
rodzic d1d45e7166
commit fd149b3f46
6 zmienionych plików z 17 dodań i 24 usunięć

Wyświetl plik

@ -303,7 +303,7 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
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()
bool deserializeState(JsonObject root, byte callMode, byte presetId)
{

Wyświetl plik

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

Wyświetl plik

@ -27,7 +27,7 @@ static void doSaveState() {
unsigned long start = millis();
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
JsonObject sObj = pDoc->to<JsonObject>();
@ -53,7 +53,7 @@ static void doSaveState() {
#if defined(ARDUINO_ARCH_ESP32)
if (!persist) {
if (tmpRAMbuffer!=nullptr) free(tmpRAMbuffer);
size_t len = measureJson(*fileDoc) + 1;
size_t len = measureJson(*pDoc) + 1;
DEBUG_PRINTLN(len);
// if possible use SPI RAM on ESP32
if (psramFound())
@ -61,13 +61,13 @@ static void doSaveState() {
else
tmpRAMbuffer = (char*) malloc(len);
if (tmpRAMbuffer!=nullptr) {
serializeJson(*fileDoc, tmpRAMbuffer, len);
serializeJson(*pDoc, tmpRAMbuffer, len);
} else {
writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, fileDoc);
writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, pDoc);
}
} else
#endif
writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, fileDoc);
writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, pDoc);
if (persist) presetsModifiedTime = toki.second(); //unix time
releaseJSONBufferLock();
@ -152,7 +152,7 @@ void handlePresets()
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;
uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()
@ -160,9 +160,6 @@ void handlePresets()
JsonObject fdo;
// allocate buffer
if (!requestJSONBufferLock(9)) return; // will also assign fileDoc
presetToApply = 0; //clear request for preset
callModeToApply = 0;
@ -171,14 +168,14 @@ void handlePresets()
#ifdef ARDUINO_ARCH_ESP32
if (tmpPreset==255 && tmpRAMbuffer!=nullptr) {
deserializeJson(*fileDoc,tmpRAMbuffer);
deserializeJson(*pDoc,tmpRAMbuffer);
errorFlag = ERR_NONE;
} else
#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
const char* httpwin = fdo["win"];
@ -205,13 +202,13 @@ void handlePresets()
}
#endif
releaseJSONBufferLock(); // will also clear fileDoc
releaseJSONBufferLock();
if (changePreset) notify(tmpMode); // force UDP notification
stateUpdated(tmpMode); // was colorUpdated() if anything breaks
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)
{
if (!saveName) saveName = new char[33];
@ -249,7 +246,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
if (sObj[F("playlist")].isNull()) {
// we will save API call immediately (often causes presets.json corruption)
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("v");
sObj.remove("time");
@ -257,7 +254,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
sObj.remove(F("psave"));
if (sObj["n"].isNull()) sObj["n"] = saveName;
initPresetsFile(); // just in case if someone deleted presets.json using /edit
writeObjectToFileUsingId(getPresetsFileName(), index, fileDoc);
writeObjectToFileUsingId(getPresetsFileName(), index, pDoc);
presetsModifiedTime = toki.second(); //unix time
updateFSInfo();
}

Wyświetl plik

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

Wyświetl plik

@ -8,7 +8,7 @@
*/
// 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
//#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 fsBytesTotal _INIT(0);
WLED_GLOBAL unsigned long presetsModifiedTime _INIT(0L);
WLED_GLOBAL JsonDocument* fileDoc;
WLED_GLOBAL bool doCloseFile _INIT(false);
// presets

Wyświetl plik

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