Advanced locking with time-out.

Bugfixes.
This commit is contained in:
Blaz Kristan
2021-11-12 23:33:10 +01:00
parent ce5a81d83c
commit 85ded6e500
15 changed files with 145 additions and 95 deletions

View File

@@ -4,6 +4,8 @@
* Methods to handle saving and loading presets to/from the filesystem
*/
// called from: handleSet(), deserializeState(), applyMacro(), handlePlaylist(), checkCountdown(), checkTimers(), handleNightlight(), presetFallback()
// shortPressAction(), longPressAction(), doublePressAction(), handleSwitch(), onAlexaChange()
bool applyPreset(byte index, byte callMode)
{
if (index == 0) return false;
@@ -19,13 +21,14 @@ bool applyPreset(byte index, byte callMode)
#endif
deserializeState(fdo, callMode, index);
} else {
DEBUGFS_PRINTLN(F("Make read buf"));
DEBUG_PRINTLN(F("Apply preset JSON buffer requested."));
#ifdef WLED_USE_DYNAMIC_JSON
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
#else
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
if (!requestJSONBufferLock()) {
DEBUG_PRINTLN(F("ERROR: Locking JSON buffer failed!"));
return false;
}
#endif
errorFlag = readObjectFromFileUsingId(filename, index, &doc) ? ERR_NONE : ERR_FS_PLOAD;
@@ -35,7 +38,7 @@ bool applyPreset(byte index, byte callMode)
serializeJson(doc, Serial);
#endif
deserializeState(fdo, callMode, index);
jsonBufferLock = false;
releaseJSONBufferLock();
}
if (!errorFlag) {
@@ -53,13 +56,14 @@ void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj)
const char *filename = persist ? "/presets.json" : "/tmp.json";
if (!fileDoc) {
DEBUGFS_PRINTLN(F("Allocating saving buffer"));
DEBUG_PRINTLN(F("Save preset JSON buffer requested."));
#ifdef WLED_USE_DYNAMIC_JSON
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
#else
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
if (!requestJSONBufferLock()) {
DEBUG_PRINTLN(F("ERROR: Locking JSON buffer failed!"));
return;
}
#endif
sObj = doc.to<JsonObject>();
@@ -71,7 +75,7 @@ void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj)
writeObjectToFileUsingId(filename, index, &doc);
jsonBufferLock = false;
releaseJSONBufferLock();
} else { //from JSON API (fileDoc != nullptr)
DEBUGFS_PRINTLN(F("Reuse recv buffer"));
sObj.remove(F("psave"));