@@ -16,15 +16,15 @@ static char quickLoad[9];
1616static char saveName[33 ];
1717static bool includeBri = true , segBounds = true , selectedOnly = false , playlistSave = false ;;
1818
19- static const char *getName (bool persist = true ) {
19+ static const char *getFileName (bool persist = true ) {
2020 return persist ? " /presets.json" : " /tmp.json" ;
2121}
2222
2323static void doSaveState () {
2424 bool persist = (presetToSave < 251 );
25- const char *filename = getName (persist);
25+ const char *filename = getFileName (persist);
2626
27- if (!requestJSONBufferLock (10 )) return ; // will set fileDoc
27+ if (!requestJSONBufferLock (10 )) return ; // will set fileDoc
2828
2929 initPresetsFile (); // just in case if someone deleted presets.json using /edit
3030 JsonObject sObj = doc.to <JsonObject>();
@@ -83,7 +83,7 @@ bool getPresetName(byte index, String& name)
8383{
8484 if (!requestJSONBufferLock (9 )) return false ;
8585 bool presetExists = false ;
86- if (readObjectFromFileUsingId (getName (), index, &doc))
86+ if (readObjectFromFileUsingId (getFileName (), index, &doc))
8787 {
8888 JsonObject fdo = doc.as <JsonObject>();
8989 if (fdo[" n" ]) {
@@ -97,12 +97,12 @@ bool getPresetName(byte index, String& name)
9797
9898void initPresetsFile ()
9999{
100- if (WLED_FS.exists (getName ())) return ;
100+ if (WLED_FS.exists (getFileName ())) return ;
101101
102102 StaticJsonDocument<64 > doc;
103103 JsonObject sObj = doc.to <JsonObject>();
104104 sObj .createNestedObject (" 0" );
105- File f = WLED_FS.open (getName (), " w" );
105+ File f = WLED_FS.open (getFileName (), " w" );
106106 if (!f) {
107107 errorFlag = ERR_FS_GENERAL;
108108 return ;
@@ -127,14 +127,14 @@ void handlePresets()
127127 return ;
128128 }
129129
130+ if (presetToApply == 0 || fileDoc) return ; // no preset waiting to apply, or JSON buffer is already allocated, return to loop until free
131+
130132 bool changePreset = false ;
131133 uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()
132134 uint8_t tmpMode = callModeToApply;
133135
134- if (tmpPreset == 0 || (fileDoc /* && !force*/ )) return ; // JSON buffer already allocated and not force apply or no preset waiting
135-
136136 JsonObject fdo;
137- const char *filename = getName (tmpPreset < 255 );
137+ const char *filename = getFileName (tmpPreset < 255 );
138138
139139 // allocate buffer
140140 if (!requestJSONBufferLock (9 )) return ; // will also assign fileDoc
@@ -201,30 +201,28 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
201201 presetToSave = index;
202202 playlistSave = false ;
203203 if (sObj [F (" ql" )].is <const char *>()) strlcpy (quickLoad, sObj [F (" ql" )].as <const char *>(), 9 ); // client limits QL to 2 chars, buffer for 8 bytes to allow unicode
204- sObj .remove (" v" );
205- sObj .remove (" time" );
206- sObj .remove (F (" error" ));
207- sObj .remove (F (" psave" ));
208- if (sObj [" o" ].isNull ()) { // "o" marks a playlist or manually entered API
204+
205+ if (sObj [" o" ].isNull ()) { // no "o" means not a playlist or custom API call, saving of state is async (not immediately)
209206 includeBri = sObj [" ib" ].as <bool >() || index==255 ; // temporary preset needs brightness
210207 segBounds = sObj [" sb" ].as <bool >() || index==255 ; // temporary preset needs bounds
211208 selectedOnly = sObj [F (" sc" )].as <bool >();
212209 saveLedmap = sObj [F (" ledmap" )] | -1 ;
213- sObj .remove (" ib" );
214- sObj .remove (" sb" );
215- sObj .remove (F (" sc" ));
216210 } else {
217- // this is a playlist or API
218- sObj .remove (" o" );
211+ // this is a playlist or API call
219212 if (sObj [F (" playlist" )].isNull ()) {
220- presetToSave = 0 ; // we will save API immediately
221- if (index < 251 && fileDoc) {
222- if (sObj [" n" ].isNull ()) sObj [" n" ] = saveName;
223- initPresetsFile (); // just in case if someone deleted presets.json using /edit
224- writeObjectToFileUsingId (getName (index), index, fileDoc);
225- presetsModifiedTime = toki.second (); // unix time
226- updateFSInfo ();
227- }
213+ // we will save API call immediately
214+ presetToSave = 0 ;
215+ if (index > 250 || !fileDoc) return ; // cannot save API calls to temporary preset (255)
216+ sObj .remove (" o" );
217+ sObj .remove (" v" );
218+ sObj .remove (" time" );
219+ sObj .remove (F (" error" ));
220+ sObj .remove (F (" psave" ));
221+ if (sObj [" n" ].isNull ()) sObj [" n" ] = saveName;
222+ initPresetsFile (); // just in case if someone deleted presets.json using /edit
223+ writeObjectToFileUsingId (getFileName (index), index, fileDoc);
224+ presetsModifiedTime = toki.second (); // unix time
225+ updateFSInfo ();
228226 } else {
229227 // store playlist
230228 includeBri = true ; // !sObj["on"].isNull();
@@ -235,7 +233,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
235233
236234void deletePreset (byte index) {
237235 StaticJsonDocument<24 > empty;
238- writeObjectToFileUsingId (getName (), index, &empty);
236+ writeObjectToFileUsingId (getFileName (), index, &empty);
239237 presetsModifiedTime = toki.second (); // unix time
240238 updateFSInfo ();
241239}
0 commit comments