|
1 /** |
|
2 * @file config.c |
|
3 * @brief BrewBoard configuration files. |
|
4 */ |
|
5 |
|
6 #include "config.h" |
|
7 |
|
8 static const char *TAG = "config"; |
|
9 |
|
10 |
|
11 void write_config() { |
|
12 uint8_t *dst = (uint8_t *)&config; |
|
13 FILE *f = fopen("/spiffs/etc/config.conf", "w+"); |
|
14 |
|
15 if (f == NULL) { |
|
16 ESP_LOGE(TAG, "write /spiffs/etc/config.conf failed"); |
|
17 return; |
|
18 } |
|
19 |
|
20 size_t bytes = fwrite(dst, 1, sizeof(config), f); |
|
21 fclose(f); |
|
22 ESP_LOGI(TAG, "/spiffs/etc/config.conf written %d bytes", bytes); |
|
23 } |
|
24 |
|
25 |
|
26 |
|
27 void read_config() { |
|
28 uint8_t *dst; |
|
29 uint8_t mac_addr[8] = {0}; |
|
30 FILE *f = fopen("/spiffs/etc/config.conf", "r"); |
|
31 |
|
32 if (f == NULL) { |
|
33 // No configuration yet, create it. |
|
34 esp_efuse_mac_get_default(mac_addr); |
|
35 config.Version = 1; |
|
36 config.Unit = 'C'; |
|
37 config.BoilTemperature = 99.0; |
|
38 config.AskAdd = true; |
|
39 config.AskRemove = true; |
|
40 config.AskIodine = true; |
|
41 config.IodineTime = 30; |
|
42 config.EquipmentRec = 1; |
|
43 sprintf(config.hostname, "brewboard-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); |
|
44 sprintf(config.ap_ssid, "brewboardAP-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); |
|
45 sprintf(config.ap_pwd, "%s", "triplehop"); |
|
46 config.ap_channel = 5; |
|
47 config.ap_ssid_hidden = 0; |
|
48 config.ap_bandwidth = WIFI_BW_HT20; |
|
49 config.ts_xleft = 0; |
|
50 config.ts_xright = 3600; |
|
51 config.ts_ytop = 3600; |
|
52 config.ts_ybottom = 0; |
|
53 config.RecipeRec = 1; |
|
54 sprintf(config.uuid, "c0ffeeee-dead-beef-cafe-%02x%02x%02x%02x%02x%02x", |
|
55 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); |
|
56 write_config(); |
|
57 } else { |
|
58 dst = (uint8_t*)&config; |
|
59 size_t bytes = fread(dst, 1, sizeof(config), f); |
|
60 fclose(f); |
|
61 ESP_LOGI(TAG, "/spiffs/etc/config.conf read %d bytes", bytes); |
|
62 if (config.AskIodine && ! config.IodineTime) { |
|
63 config.IodineTime = 30; |
|
64 write_config(); |
|
65 } |
|
66 if (strlen(config.uuid) !=36) { |
|
67 esp_efuse_mac_get_default(mac_addr); |
|
68 sprintf(config.uuid, "c0ffeeee-dead-beef-cafe-%02x%02x%02x%02x%02x%02x", |
|
69 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); |
|
70 write_config(); |
|
71 } |
|
72 } |
|
73 } |
|
74 |
|
75 |
|
76 |
|
77 void append_equipment() { |
|
78 uint8_t *dst = (uint8_t *)&equipment; |
|
79 FILE *f = fopen("/spiffs/etc/equipments.conf", "a"); |
|
80 |
|
81 if (f == NULL) { |
|
82 ESP_LOGE(TAG, "append /spiffs/etc/equipments.conf failed"); |
|
83 return; |
|
84 } |
|
85 |
|
86 size_t bytes = fwrite(dst, 1, sizeof(equipment), f); |
|
87 fclose(f); |
|
88 ESP_LOGI(TAG, "/spiffs/etc/equipments.conf appended %d bytes", bytes); |
|
89 } |
|
90 |
|
91 |
|
92 |
|
93 void read_equipment(int RecNo) { |
|
94 uint8_t *dst; |
|
95 FILE *f = fopen("/spiffs/etc/equipments.conf", "r"); |
|
96 |
|
97 if (f == NULL) { |
|
98 // No configuration yet, create it. |
|
99 equipment.Version = 1; |
|
100 equipment.Record = 1; |
|
101 sprintf(equipment.Name, "default"); |
|
102 equipment.BoilPower = 80; |
|
103 equipment.MashPower = 100; |
|
104 equipment.PumpCycle = 8; |
|
105 equipment.PumpRest = 2; |
|
106 equipment.PumpPreMash = true; |
|
107 equipment.PumpOnMash = true; |
|
108 equipment.PumpMashOut = true; |
|
109 equipment.PumpOnBoil = false; |
|
110 equipment.PumpMaxTemp = 80; |
|
111 equipment.PIDPipe = true; |
|
112 equipment.SSR2 = 0; |
|
113 equipment.TempHLT = 85.0; |
|
114 equipment.PID_kP = 150.0; |
|
115 equipment.PID_kI = 1.5; |
|
116 equipment.PID_kD = 15000.0; |
|
117 equipment.PID_POn = PID_P_ON_E; |
|
118 equipment.SampleTime = 5000; |
|
119 append_equipment(); |
|
120 } else { |
|
121 dst = (uint8_t*)&equipment; |
|
122 while (1) { |
|
123 size_t bytes = fread(dst, 1, sizeof(equipment), f); |
|
124 if (bytes && equipment.Record == RecNo) { |
|
125 fclose(f); |
|
126 ESP_LOGI(TAG, "/spiffs/etc/equipments.conf read %d bytes, record %d: %s", bytes, RecNo, equipment.Name); |
|
127 return; |
|
128 } |
|
129 if (bytes == 0) |
|
130 break; |
|
131 } |
|
132 fclose(f); |
|
133 ESP_LOGI(TAG, "/spiffs/etc/equipments.conf read error, record %d not found", RecNo); |
|
134 } |
|
135 } |
|
136 |
|
137 |
|
138 |
|
139 void write_equipment(int RecNo) |
|
140 { |
|
141 uint8_t *dst = (uint8_t *)&equipment; |
|
142 FILE *f; |
|
143 |
|
144 f = fopen("/spiffs/etc/equipments.conf", "r+"); |
|
145 if (f == NULL) { |
|
146 ESP_LOGE(TAG, "write /spiffs/etc/equipments.conf failed"); |
|
147 return; |
|
148 } |
|
149 fseek(f, (RecNo - 1) * sizeof(equipment), SEEK_SET); |
|
150 size_t bytes = fwrite(dst, 1, sizeof(equipment), f); |
|
151 fclose(f); |
|
152 ESP_LOGI(TAG, "/spiffs/etc/equipments.conf update record %d, %d bytes", RecNo, bytes); |
|
153 } |
|
154 |
|
155 |
|
156 |
|
157 void delete_equipment(int RecNo) |
|
158 { |
|
159 int RecNow = 1; |
|
160 FILE *n, *o; |
|
161 uint8_t *dst; |
|
162 size_t bytes; |
|
163 |
|
164 n = fopen("/spiffs/etc/equipments.new", "a"); |
|
165 if (n == NULL) { |
|
166 ESP_LOGE(TAG, "cannot create /spiffs/etc/equipments.new"); |
|
167 return; |
|
168 } |
|
169 o = fopen("/spiffs/etc/equipments.conf", "r"); |
|
170 if (o == NULL) { |
|
171 ESP_LOGE(TAG, "cannot open spiffs/etc/equipments.conf for reading"); |
|
172 fclose(n); |
|
173 return; |
|
174 } |
|
175 |
|
176 dst = (uint8_t*)&equipment; |
|
177 while (true) { |
|
178 bytes = fread(dst, 1, sizeof(equipment), o); |
|
179 if (bytes == 0) |
|
180 break; |
|
181 |
|
182 if (equipment.Record == RecNo) { |
|
183 // Record to delete, don't copy |
|
184 printf("Ditch %d\n", RecNo); |
|
185 } else { |
|
186 if ((config.EquipmentRec == equipment.Record) && (config.EquipmentRec != RecNow)) { |
|
187 // We need to change the default record. |
|
188 config.EquipmentRec = RecNow; |
|
189 write_config(); |
|
190 } |
|
191 printf("Copy %d to %d\n", equipment.Record, RecNow); |
|
192 equipment.Record = RecNow; |
|
193 fwrite(dst, 1, sizeof(equipment), n); |
|
194 RecNow++; |
|
195 } |
|
196 } |
|
197 fclose(o); |
|
198 fclose(n); |
|
199 |
|
200 rename("/spiffs/etc/equipments.conf", "/spiffs/etc/equipments.old"); |
|
201 rename("/spiffs/etc/equipments.new", "/spiffs/etc/equipments.conf"); |
|
202 unlink("/spiffs/etc/equipments.old"); |
|
203 } |
|
204 |
|
205 |
|
206 |
|
207 int add_station(uint8_t *SSID, uint8_t *Password) |
|
208 { |
|
209 FILE *f; |
|
210 uint8_t *dst = (uint8_t *)&wifiStation; |
|
211 |
|
212 if (read_station(SSID) >= 0) { |
|
213 ESP_LOGE(TAG, "add_station %s already excists", SSID); |
|
214 return -1; |
|
215 } |
|
216 |
|
217 f = fopen("/spiffs/etc/stations.conf", "a+"); |
|
218 if (f == NULL) { |
|
219 ESP_LOGE(TAG, "write /spiffs/etc/stations.conf failed"); |
|
220 return -1; |
|
221 } |
|
222 memset(dst, 0, sizeof(wifiStation)); |
|
223 sprintf(wifiStation.SSID, "%s", (char *)SSID); |
|
224 sprintf(wifiStation.Password, "%s", (char *)Password); |
|
225 wifiStation.hide = false; |
|
226 fwrite(dst, 1, sizeof(wifiStation), f); |
|
227 fclose(f); |
|
228 |
|
229 ESP_LOGI(TAG, "add_station %s record: %d", (char *)SSID, read_station(SSID)); |
|
230 /* Return the record number */ |
|
231 return read_station(SSID); |
|
232 } |
|
233 |
|
234 |
|
235 |
|
236 int read_station(uint8_t *SSID) |
|
237 { |
|
238 uint8_t *dst = (uint8_t *)&wifiStation; |
|
239 static int rc; |
|
240 FILE *f; |
|
241 size_t bytes; |
|
242 |
|
243 if ((SSID == NULL) || (strlen((char *)SSID) == 0)) { |
|
244 ESP_LOGI(TAG, "read_station(NULL)"); |
|
245 return -1; |
|
246 } |
|
247 |
|
248 memset(dst, 0, sizeof(wifiStation)); |
|
249 f = fopen("/spiffs/etc/stations.conf", "r+"); |
|
250 if (f == NULL) { |
|
251 f = fopen("/spiffs/etc/stations.conf", "w+"); |
|
252 fclose(f); |
|
253 ESP_LOGI(TAG, "/spiffs/etc/stations.conf created, return -1"); |
|
254 return -1; |
|
255 } |
|
256 |
|
257 rc = 0; |
|
258 fseek(f, 0, SEEK_SET); |
|
259 |
|
260 while (1) { |
|
261 bytes = fread(dst, 1, sizeof(wifiStation), f); |
|
262 if (bytes < sizeof(wifiStation)) { |
|
263 fclose(f); |
|
264 memset(dst, 0, sizeof(wifiStation)); |
|
265 return -1; |
|
266 } |
|
267 if (strcmp(wifiStation.SSID, (char *)SSID) == 0) { |
|
268 // Fount it |
|
269 fclose(f); |
|
270 // ESP_LOGI(TAG, "read_station %s record %d", (char *)SSID, rc); |
|
271 return rc; |
|
272 } |
|
273 rc++; |
|
274 } |
|
275 return -1; |
|
276 } |
|
277 |
|
278 |
|
279 |
|
280 void remove_station(uint8_t *SSID) |
|
281 { |
|
282 FILE *n, *o; |
|
283 uint8_t *dst; |
|
284 size_t bytes; |
|
285 |
|
286 n = fopen("/spiffs/etc/stations.new", "a"); |
|
287 if (n == NULL) { |
|
288 ESP_LOGE(TAG, "cannot create /spiffs/etc/stations.new"); |
|
289 return; |
|
290 } |
|
291 o = fopen("/spiffs/etc/stations.conf", "r"); |
|
292 if (o == NULL) { |
|
293 ESP_LOGE(TAG, "cannot open spiffs/etc/stations.conf for reading"); |
|
294 fclose(n); |
|
295 return; |
|
296 } |
|
297 |
|
298 dst = (uint8_t*)&wifiStation; |
|
299 while (true) { |
|
300 bytes = fread(dst, 1, sizeof(wifiStation), o); |
|
301 if (bytes == 0) |
|
302 break; |
|
303 |
|
304 if ((strcmp((char *)SSID, wifiStation.SSID) == 0) || (strlen(wifiStation.SSID) == 0)) { |
|
305 // Record to delete, don't copy |
|
306 } else { |
|
307 fwrite(dst, 1, sizeof(wifiStation), n); |
|
308 } |
|
309 } |
|
310 fclose(o); |
|
311 fclose(n); |
|
312 |
|
313 rename("/spiffs/etc/stations.conf", "/spiffs/etc/stations.old"); |
|
314 rename("/spiffs/etc/stations.new", "/spiffs/etc/stations.conf"); |
|
315 unlink("/spiffs/etc/stations.old"); |
|
316 } |
|
317 |
|
318 |
|
319 |
|
320 int blacklist_station(uint8_t *SSID) |
|
321 { |
|
322 return -1; |
|
323 } |
|
324 |
|
325 |
|
326 |
|
327 void write_runtime() |
|
328 { |
|
329 uint8_t *dst = (uint8_t *)&runtime; |
|
330 FILE *f = fopen("/spiffs/etc/runtime.conf", "w+"); |
|
331 |
|
332 if (f == NULL) { |
|
333 ESP_LOGE(TAG, "write /spiffs/etc/runtime.conf failed"); |
|
334 return; |
|
335 } |
|
336 |
|
337 size_t bytes = fwrite(dst, 1, sizeof(runtime), f); |
|
338 fclose(f); |
|
339 ESP_LOGI(TAG, "/spiffs/etc/runtime.conf written %d bytes", bytes); |
|
340 } |
|
341 |
|
342 |
|
343 |
|
344 void read_runtime() |
|
345 { |
|
346 uint8_t *dst; |
|
347 FILE *f = fopen("/spiffs/etc/runtime.conf", "r"); |
|
348 |
|
349 if (f == NULL) { |
|
350 // No runtime yet, create it. |
|
351 runtime.Version = 1; |
|
352 runtime.AutoModeStarted = false; |
|
353 runtime.StageResume = 0; |
|
354 runtime.StageTimeLeft = 0; |
|
355 runtime.HopAddition = 0; |
|
356 runtime.ManualMLT = 45.0; |
|
357 runtime.ManualHLT = 45.0; |
|
358 runtime.BrewStart = (time_t)0; |
|
359 runtime.Logfile[0] = '\0'; |
|
360 runtime.PumpCooling = false; |
|
361 runtime.TimeBrewing = 0; |
|
362 write_runtime(); |
|
363 } else { |
|
364 dst = (uint8_t*)&runtime; |
|
365 size_t bytes = fread(dst, 1, sizeof(runtime), f); |
|
366 fclose(f); |
|
367 ESP_LOGI(TAG, "/spiffs/etc/runtime.conf read %d bytes", bytes); |
|
368 #if 0 |
|
369 printf("Auto started %s\n", runtime.AutoModeStarted ? "yes":"no"); |
|
370 printf("Stage resume %d\n", runtime.StageResume); |
|
371 printf("Stage time left %d\n", runtime.StageTimeLeft); |
|
372 printf("Hop addition %d\n", runtime.HopAddition); |
|
373 printf("Brew start %d\n", (int)runtime.BrewStart); |
|
374 printf("Log file %s\n", runtime.Logfile); |
|
375 printf("Pump cooling %s\n", runtime.PumpCooling ? "yes":"no"); |
|
376 printf("Time brewing %d\n", runtime.TimeBrewing); |
|
377 #endif |
|
378 } |
|
379 } |
|
380 |
|
381 |
|
382 |
|
383 void append_recipe() { |
|
384 uint8_t *dst = (uint8_t *)&recipe; |
|
385 FILE *f = fopen("/spiffs/etc/recipe.conf", "a"); |
|
386 |
|
387 if (f == NULL) { |
|
388 ESP_LOGE(TAG, "append /spiffs/etc/recipe.conf failed"); |
|
389 return; |
|
390 } |
|
391 |
|
392 size_t bytes = fwrite(dst, 1, sizeof(recipe), f); |
|
393 fclose(f); |
|
394 ESP_LOGI(TAG, "/spiffs/etc/recipe.conf appended %d bytes", bytes); |
|
395 } |
|
396 |
|
397 |
|
398 |
|
399 void write_recipe(int RecNo) |
|
400 { |
|
401 uint8_t *dst = (uint8_t *)&recipe; |
|
402 FILE *f = fopen("/spiffs/etc/recipe.conf", "r+"); |
|
403 |
|
404 if (f == NULL) { |
|
405 ESP_LOGE(TAG, "write /spiffs/etc/recipe.conf failed"); |
|
406 return; |
|
407 } |
|
408 |
|
409 fseek(f, (RecNo - 1) * sizeof(recipe), SEEK_SET); |
|
410 size_t bytes = fwrite(dst, 1, sizeof(recipe), f); |
|
411 fclose(f); |
|
412 ESP_LOGI(TAG, "/spiffs/etc/recipe.conf written record %d, %d bytes", RecNo, bytes); |
|
413 } |
|
414 |
|
415 |
|
416 |
|
417 void read_recipe(int RecNo) |
|
418 { |
|
419 uint8_t *dst; |
|
420 FILE *f = fopen("/spiffs/etc/recipe.conf", "r"); |
|
421 |
|
422 if (f == NULL) { |
|
423 // No runtime yet, create it. |
|
424 dst = (uint8_t*)&recipe; |
|
425 memset(dst, 0, sizeof(recipe)); |
|
426 recipe.Version = 1; |
|
427 recipe.Record = 1; |
|
428 sprintf(recipe.Name, "Recipe 1"); |
|
429 sprintf(recipe.Code, "001"); |
|
430 sprintf(recipe.MashStep[0].Name, "Mash-in"); |
|
431 recipe.MashStep[0].Temperature = 67.5; |
|
432 recipe.MashStep[0].Resttime = 1; |
|
433 sprintf(recipe.MashStep[1].Name, "Mash"); |
|
434 recipe.MashStep[1].Temperature = 67.0; |
|
435 recipe.MashStep[1].Resttime = 75; |
|
436 sprintf(recipe.MashStep[7].Name, "Mash-out"); |
|
437 recipe.MashStep[7].Temperature = 78.0; |
|
438 recipe.MashStep[7].Resttime = 5; |
|
439 recipe.BoilTime = 60; |
|
440 recipe.Additions = 2; |
|
441 sprintf(recipe.Addition[0].Name, "Hop"); |
|
442 recipe.Addition[0].Time = 60; |
|
443 recipe.Addition[0].Type = ADDITION_HOP; |
|
444 sprintf(recipe.Addition[1].Name, "Hop"); |
|
445 recipe.Addition[1].Time = 10; |
|
446 recipe.Addition[1].Type = ADDITION_HOP; |
|
447 recipe.CoolTemp = 20.0; |
|
448 recipe.Whirlpool9 = 0; |
|
449 recipe.Whirlpool7 = 0; |
|
450 recipe.Whirlpool6 = 0; |
|
451 recipe.Whirlpool2 = 0; |
|
452 recipe.SpargeTemp = 85.0; |
|
453 append_recipe(); |
|
454 } else { |
|
455 dst = (uint8_t*)&recipe; |
|
456 fseek(f, (RecNo - 1) * sizeof(recipe), SEEK_SET); |
|
457 size_t bytes = fread(dst, 1, sizeof(recipe), f); |
|
458 fclose(f); |
|
459 ESP_LOGI(TAG, "/spiffs/etc/recipe.conf read record %d, %d bytes", RecNo, bytes); |
|
460 } |
|
461 } |
|
462 |
|
463 |
|
464 |
|
465 void delete_recipe(int RecNo) |
|
466 { |
|
467 int RecNow = 1; |
|
468 FILE *n, *o; |
|
469 uint8_t *dst; |
|
470 size_t bytes; |
|
471 |
|
472 n = fopen("/spiffs/etc/recipe.new", "a"); |
|
473 if (n == NULL) { |
|
474 ESP_LOGE(TAG, "cannot create /spiffs/etc/recipe.new"); |
|
475 return; |
|
476 } |
|
477 o = fopen("/spiffs/etc/recipe.conf", "r"); |
|
478 if (o == NULL) { |
|
479 ESP_LOGE(TAG, "cannot open spiffs/etc/recipe.conf for reading"); |
|
480 fclose(n); |
|
481 return; |
|
482 } |
|
483 |
|
484 dst = (uint8_t*)&recipe; |
|
485 while (true) { |
|
486 bytes = fread(dst, 1, sizeof(recipe), o); |
|
487 if (bytes == 0) |
|
488 break; |
|
489 |
|
490 if (recipe.Record == RecNo) { |
|
491 // Record to delete, don't copy |
|
492 printf("Ditch %d\n", RecNo); |
|
493 } else { |
|
494 if ((config.RecipeRec == recipe.Record) && (config.RecipeRec != RecNow)) { |
|
495 // We need to change the default record. |
|
496 config.RecipeRec = RecNow; |
|
497 write_config(); |
|
498 } |
|
499 printf("Copy %d to %d\n", recipe.Record, RecNow); |
|
500 recipe.Record = RecNow; |
|
501 fwrite(dst, 1, sizeof(recipe), n); |
|
502 RecNow++; |
|
503 } |
|
504 } |
|
505 fclose(o); |
|
506 fclose(n); |
|
507 |
|
508 rename("/spiffs/etc/recipe.conf", "/spiffs/etc/recipe.old"); |
|
509 rename("/spiffs/etc/recipe.new", "/spiffs/etc/recipe.conf"); |
|
510 unlink("/spiffs/etc/recipe.old"); |
|
511 } |
|
512 |
|
513 |