forked from Aircoookie/WLED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presets.cpp
104 lines (89 loc) · 3.07 KB
/
presets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "wled.h"
/*
* Methods to handle saving and loading presets to/from the filesystem
*/
bool applyPreset(byte index, byte callMode)
{
if (index == 0) return false;
const char *filename = index < 255 ? "/presets.json" : "/tmp.json";
uint8_t core = 1;
//crude way to determine if this was called by a network request
#ifdef ARDUINO_ARCH_ESP32
core = xPortGetCoreID();
#endif
//only allow use of fileDoc from the core responsible for network requests
//do not use active network request doc from preset called by main loop (playlist, schedule, ...)
if (fileDoc && core) {
errorFlag = readObjectFromFileUsingId(filename, index, fileDoc) ? ERR_NONE : ERR_FS_PLOAD;
JsonObject fdo = fileDoc->as<JsonObject>();
if (fdo["ps"] == index) fdo.remove("ps"); //remove load request for same presets to prevent recursive crash
#ifdef WLED_DEBUG_FS
serializeJson(*fileDoc, Serial);
#endif
deserializeState(fdo, callMode, index);
} 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>();
if (fdo["ps"] == index) fdo.remove("ps");
#ifdef WLED_DEBUG_FS
serializeJson(doc, Serial);
#endif
deserializeState(fdo, callMode, index);
releaseJSONBufferLock();
}
if (!errorFlag) {
if (index < 255) currentPreset = index;
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;
JsonObject sObj = saveobj;
const char *filename = persist ? "/presets.json" : "/tmp.json";
if (!fileDoc) {
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>();
if (pname) sObj["n"] = pname;
DEBUGFS_PRINTLN(F("Save current state"));
serializeState(sObj, true);
if (persist) currentPreset = index;
writeObjectToFileUsingId(filename, index, &doc);
releaseJSONBufferLock();
} else { //from JSON API (fileDoc != nullptr)
DEBUGFS_PRINTLN(F("Reuse recv buffer"));
sObj.remove(F("psave"));
sObj.remove(F("v"));
if (!sObj["o"]) {
DEBUGFS_PRINTLN(F("Save current state"));
serializeState(sObj, true, sObj["ib"], sObj["sb"]);
if (persist) currentPreset = index;
}
sObj.remove("o");
sObj.remove("ib");
sObj.remove("sb");
sObj.remove(F("error"));
sObj.remove(F("time"));
writeObjectToFileUsingId(filename, index, fileDoc);
}
if (persist) presetsModifiedTime = toki.second(); //unix time
updateFSInfo();
}
void deletePreset(byte index) {
StaticJsonDocument<24> empty;
writeObjectToFileUsingId("/presets.json", index, &empty);
presetsModifiedTime = toki.second(); //unix time
updateFSInfo();
}