main/config.c

changeset 6
e84200edc852
parent 0
b74b0e4902c3
child 15
34b1eb93e71a
equal deleted inserted replaced
5:87b86d16b1d6 6:e84200edc852
265 return -1; 265 return -1;
266 } 266 }
267 if (strcmp(wifiStation.SSID, (char *)SSID) == 0) { 267 if (strcmp(wifiStation.SSID, (char *)SSID) == 0) {
268 // Fount it 268 // Fount it
269 fclose(f); 269 fclose(f);
270 // ESP_LOGI(TAG, "read_station %s record %d", (char *)SSID, rc);
271 return rc; 270 return rc;
272 } 271 }
273 rc++; 272 rc++;
274 } 273 }
275 return -1; 274 return -1;
334 return; 333 return;
335 } 334 }
336 335
337 size_t bytes = fwrite(dst, 1, sizeof(runtime), f); 336 size_t bytes = fwrite(dst, 1, sizeof(runtime), f);
338 fclose(f); 337 fclose(f);
339 ESP_LOGI(TAG, "/spiffs/etc/runtime.conf written %d bytes", bytes); 338 ESP_LOGD(TAG, "/spiffs/etc/runtime.conf written %d bytes", bytes);
340 } 339 }
341 340
342 341
343 342
344 void read_runtime() 343 void read_runtime()
418 { 417 {
419 uint8_t *dst; 418 uint8_t *dst;
420 FILE *f = fopen("/spiffs/etc/recipe.conf", "r"); 419 FILE *f = fopen("/spiffs/etc/recipe.conf", "r");
421 420
422 if (f == NULL) { 421 if (f == NULL) {
423 // No runtime yet, create it. 422 // No recipe yet, create it.
424 dst = (uint8_t*)&recipe; 423 dst = (uint8_t*)&recipe;
425 memset(dst, 0, sizeof(recipe)); 424 memset(dst, 0, sizeof(recipe));
426 recipe.Version = 1; 425 recipe.Version = 1;
427 recipe.Record = 1;
428 sprintf(recipe.Name, "Recipe 1"); 426 sprintf(recipe.Name, "Recipe 1");
429 sprintf(recipe.Code, "001"); 427 sprintf(recipe.Code, "001");
430 sprintf(recipe.MashStep[0].Name, "Mash-in"); 428 sprintf(recipe.MashStep[0].Name, "Mash-in");
431 recipe.MashStep[0].Temperature = 67.5; 429 recipe.MashStep[0].Temperature = 67.5;
432 recipe.MashStep[0].Resttime = 1; 430 recipe.MashStep[0].Resttime = 1;
462 460
463 461
464 462
465 void delete_recipe(int RecNo) 463 void delete_recipe(int RecNo)
466 { 464 {
467 int RecNow = 1; 465 int RecRead = 1, RecWrite = 1;
468 FILE *n, *o; 466 FILE *n, *o;
469 uint8_t *dst; 467 uint8_t *dst;
470 size_t bytes; 468 size_t bytes;
471 469
472 n = fopen("/spiffs/etc/recipe.new", "a"); 470 n = fopen("/spiffs/etc/recipe.new", "a");
485 while (true) { 483 while (true) {
486 bytes = fread(dst, 1, sizeof(recipe), o); 484 bytes = fread(dst, 1, sizeof(recipe), o);
487 if (bytes == 0) 485 if (bytes == 0)
488 break; 486 break;
489 487
490 if (recipe.Record == RecNo) { 488 if (RecRead != RecNo) {
491 // Record to delete, don't copy 489 // Record to copy
492 printf("Ditch %d\n", RecNo); 490 if ((config.RecipeRec == RecRead) && (config.RecipeRec != RecWrite)) {
493 } else {
494 if ((config.RecipeRec == recipe.Record) && (config.RecipeRec != RecNow)) {
495 // We need to change the default record. 491 // We need to change the default record.
496 config.RecipeRec = RecNow; 492 config.RecipeRec = RecWrite;
497 write_config(); 493 write_config();
498 } 494 }
499 printf("Copy %d to %d\n", recipe.Record, RecNow);
500 recipe.Record = RecNow;
501 fwrite(dst, 1, sizeof(recipe), n); 495 fwrite(dst, 1, sizeof(recipe), n);
502 RecNow++; 496 RecWrite++;
503 } 497 }
498 RecRead++;
504 } 499 }
505 fclose(o); 500 fclose(o);
506 fclose(n); 501 fclose(n);
507 502
508 rename("/spiffs/etc/recipe.conf", "/spiffs/etc/recipe.old"); 503 rename("/spiffs/etc/recipe.conf", "/spiffs/etc/recipe.old");
509 rename("/spiffs/etc/recipe.new", "/spiffs/etc/recipe.conf"); 504 rename("/spiffs/etc/recipe.new", "/spiffs/etc/recipe.conf");
510 unlink("/spiffs/etc/recipe.old"); 505 unlink("/spiffs/etc/recipe.old");
511 } 506 ESP_LOGI(TAG, "Deleted recipe %d", RecNo);
512 507 }
513 508
509

mercurial