WLED/wled00/presets.cpp

96 lines
2.7 KiB
C++
Raw Normal View History

2020-11-06 22:12:48 +01:00
#include "wled.h"
/*
* Methods to handle saving and loading presets to/from the filesystem
*/
bool applyPreset(byte index, byte callMode)
2020-11-06 22:12:48 +01:00
{
2021-01-09 00:35:48 +01:00
if (index == 0) return false;
const char *filename = index < 255 ? "/presets.json" : "/tmp.json";
2020-11-06 22:12:48 +01:00
if (fileDoc) {
errorFlag = readObjectFromFileUsingId(filename, index, fileDoc) ? ERR_NONE : ERR_FS_PLOAD;
2020-11-09 00:50:13 +01:00
JsonObject fdo = fileDoc->as<JsonObject>();
if (fdo["ps"] == index) fdo.remove("ps"); //remove load request for same presets to prevent recursive crash
2020-11-06 22:12:48 +01:00
#ifdef WLED_DEBUG_FS
serializeJson(*fileDoc, Serial);
#endif
deserializeState(fdo, callMode, index);
2020-11-06 22:12:48 +01:00
} else {
DEBUGFS_PRINTLN(F("Make read buf"));
#ifdef WLED_USE_DYNAMIC_JSON
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
#else
if (!requestJSONBufferLock(9)) return false;
#endif
errorFlag = readObjectFromFileUsingId(filename, index, &doc) ? ERR_NONE : ERR_FS_PLOAD;
JsonObject fdo = doc.as<JsonObject>();
2020-11-09 00:50:13 +01:00
if (fdo["ps"] == index) fdo.remove("ps");
2020-11-06 22:12:48 +01:00
#ifdef WLED_DEBUG_FS
serializeJson(doc, Serial);
2020-11-06 22:12:48 +01:00
#endif
deserializeState(fdo, callMode, index);
releaseJSONBufferLock();
2020-11-06 22:12:48 +01:00
}
if (!errorFlag) {
if (index < 255) currentPreset = index;
2020-11-06 22:12:48 +01:00
return true;
}
return false;
}
void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj)
{
if (index == 0 || (index > 250 && persist) || (index<255 && !persist)) return;
2020-11-06 22:12:48 +01:00
JsonObject sObj = saveobj;
const char *filename = persist ? "/presets.json" : "/tmp.json";
if (!fileDoc) {
2020-11-06 22:12:48 +01:00
DEBUGFS_PRINTLN(F("Allocating saving buffer"));
#ifdef WLED_USE_DYNAMIC_JSON
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
#else
if (!requestJSONBufferLock(10)) return;
#endif
sObj = doc.to<JsonObject>();
2020-11-06 22:12:48 +01:00
if (pname) sObj["n"] = pname;
2020-11-29 22:07:12 +01:00
DEBUGFS_PRINTLN(F("Save current state"));
serializeState(sObj, true);
if (persist) currentPreset = index;
2020-11-29 22:07:12 +01:00
writeObjectToFileUsingId(filename, index, &doc);
releaseJSONBufferLock();
} else { //from JSON API (fileDoc != nullptr)
2020-11-06 22:12:48 +01:00
DEBUGFS_PRINTLN(F("Reuse recv buffer"));
sObj.remove(F("psave"));
sObj.remove(F("v"));
2020-11-29 22:07:12 +01:00
if (!sObj["o"]) {
DEBUGFS_PRINTLN(F("Save current state"));
serializeState(sObj, true, sObj["ib"], sObj["sb"]);
if (persist) currentPreset = index;
2020-11-29 22:07:12 +01:00
}
sObj.remove("o");
sObj.remove("ib");
sObj.remove("sb");
sObj.remove(F("error"));
sObj.remove(F("time"));
2020-11-06 22:12:48 +01:00
writeObjectToFileUsingId(filename, index, fileDoc);
2020-11-29 22:07:12 +01:00
}
if (persist) presetsModifiedTime = toki.second(); //unix time
2020-11-06 22:12:48 +01:00
updateFSInfo();
}
void deletePreset(byte index) {
StaticJsonDocument<24> empty;
writeObjectToFileUsingId("/presets.json", index, &empty);
2021-05-25 09:59:19 +02:00
presetsModifiedTime = toki.second(); //unix time
2020-11-06 22:12:48 +01:00
updateFSInfo();
}