@@ -42,57 +42,16 @@ Settings::Settings() {
4242 homeAssistantPassword = preferences.getString (" ha_pw" , " " );
4343 timezone = preferences.getString (" tz" , DEFAULT_TIMEZONE);
4444 preferences.end ();
45+
46+ xTaskCreate (loopTask, " Settings::loop" , configMINIMAL_STACK_SIZE * 6 , this , 1 , &taskHandle);
4547}
4648
4749void Settings::batchUpdate (const SettingsCallback &callback) {
48- inBatch = true ;
4950 callback (this );
50- inBatch = false ;
5151 save ();
5252}
5353
54- void Settings::save () {
55- if (inBatch)
56- return ;
57- preferences.begin (PREFERENCES_KEY, false );
58- preferences.putInt (" sm" , startupMode);
59- preferences.putInt (" tb" , targetBrewTemp);
60- preferences.putInt (" ts" , targetSteamTemp);
61- preferences.putInt (" tw" , targetWaterTemp);
62- preferences.putInt (" td" , targetDuration);
63- preferences.putInt (" tv" , targetVolume);
64- preferences.putInt (" tgv" , targetGrindVolume);
65- preferences.putInt (" tgd" , targetGrindDuration);
66- preferences.putDouble (" del_br" , brewDelay);
67- preferences.putDouble (" del_gd" , grindDelay);
68- preferences.putBool (" del_ad" , delayAdjust);
69- preferences.putInt (" to" , temperatureOffset);
70- preferences.putString (" pid" , pid);
71- preferences.putString (" ws" , wifiSsid);
72- preferences.putString (" wp" , wifiPassword);
73- preferences.putString (" mn" , mdnsName);
74- preferences.putBool (" hk" , homekit);
75- preferences.putBool (" vt" , volumetricTarget);
76- preferences.putString (" oc" , otaChannel);
77- preferences.putInt (" ipt" , infusePumpTime);
78- preferences.putInt (" ibt" , infuseBloomTime);
79- preferences.putInt (" pt" , pressurizeTime);
80- preferences.putString (" ssc" , savedScale);
81- preferences.putBool (" bf_a" , boilerFillActive);
82- preferences.putInt (" bf_su" , startupFillTime);
83- preferences.putInt (" bf_st" , steamFillTime);
84- preferences.putBool (" sg_a" , smartGrindActive);
85- preferences.putString (" sg_i" , smartGrindIp);
86- preferences.putBool (" sg_t" , smartGrindToggle);
87- preferences.putInt (" sg_m" , smartGrindMode);
88- preferences.putBool (" ha_a" , homeAssistant);
89- preferences.putString (" ha_i" , homeAssistantIP);
90- preferences.putInt (" ha_p" , homeAssistantPort);
91- preferences.putString (" ha_u" , homeAssistantUser);
92- preferences.putString (" ha_pw" , homeAssistantPassword);
93- preferences.putString (" tz" , timezone);
94- preferences.end ();
95- }
54+ void Settings::save () { dirty = true ; }
9655
9756void Settings::setTargetBrewTemp (const int target_brew_temp) {
9857 targetBrewTemp = target_brew_temp;
@@ -276,3 +235,57 @@ void Settings::setTimezone(String timezone) {
276235 this ->timezone = std::move (timezone);
277236 save ();
278237}
238+
239+ void Settings::doSave () {
240+ if (!dirty) {
241+ return ;
242+ }
243+ dirty = false ;
244+ ESP_LOGI (" Settings" , " Saving settings" );
245+ preferences.begin (PREFERENCES_KEY, false );
246+ preferences.putInt (" sm" , startupMode);
247+ preferences.putInt (" tb" , targetBrewTemp);
248+ preferences.putInt (" ts" , targetSteamTemp);
249+ preferences.putInt (" tw" , targetWaterTemp);
250+ preferences.putInt (" td" , targetDuration);
251+ preferences.putInt (" tv" , targetVolume);
252+ preferences.putInt (" tgv" , targetGrindVolume);
253+ preferences.putInt (" tgd" , targetGrindDuration);
254+ preferences.putDouble (" del_br" , brewDelay);
255+ preferences.putDouble (" del_gd" , grindDelay);
256+ preferences.putBool (" del_ad" , delayAdjust);
257+ preferences.putInt (" to" , temperatureOffset);
258+ preferences.putString (" pid" , pid);
259+ preferences.putString (" ws" , wifiSsid);
260+ preferences.putString (" wp" , wifiPassword);
261+ preferences.putString (" mn" , mdnsName);
262+ preferences.putBool (" hk" , homekit);
263+ preferences.putBool (" vt" , volumetricTarget);
264+ preferences.putString (" oc" , otaChannel);
265+ preferences.putInt (" ipt" , infusePumpTime);
266+ preferences.putInt (" ibt" , infuseBloomTime);
267+ preferences.putInt (" pt" , pressurizeTime);
268+ preferences.putString (" ssc" , savedScale);
269+ preferences.putBool (" bf_a" , boilerFillActive);
270+ preferences.putInt (" bf_su" , startupFillTime);
271+ preferences.putInt (" bf_st" , steamFillTime);
272+ preferences.putBool (" sg_a" , smartGrindActive);
273+ preferences.putString (" sg_i" , smartGrindIp);
274+ preferences.putBool (" sg_t" , smartGrindToggle);
275+ preferences.putInt (" sg_m" , smartGrindMode);
276+ preferences.putBool (" ha_a" , homeAssistant);
277+ preferences.putString (" ha_i" , homeAssistantIP);
278+ preferences.putInt (" ha_p" , homeAssistantPort);
279+ preferences.putString (" ha_u" , homeAssistantUser);
280+ preferences.putString (" ha_pw" , homeAssistantPassword);
281+ preferences.putString (" tz" , timezone);
282+ preferences.end ();
283+ }
284+
285+ void Settings::loopTask (void *arg) {
286+ auto *settings = static_cast <Settings *>(arg);
287+ while (true ) {
288+ settings->doSave ();
289+ vTaskDelay (1000 / portTICK_PERIOD_MS);
290+ }
291+ }
0 commit comments