main/config.c

changeset 77
66c77497d86d
parent 57
6c5211c0120b
child 82
7d17e2cb31a8
equal deleted inserted replaced
76:3ff381bfa469 77:66c77497d86d
364 runtime.ManualHLT = 45.0; 364 runtime.ManualHLT = 45.0;
365 runtime.BrewStart = (time_t)0; 365 runtime.BrewStart = (time_t)0;
366 runtime.Logfile[0] = '\0'; 366 runtime.Logfile[0] = '\0';
367 runtime.PumpCooling = false; 367 runtime.PumpCooling = false;
368 runtime.TimeBrewing = 0; 368 runtime.TimeBrewing = 0;
369 runtime.MashStep = 0;
369 write_runtime(); 370 write_runtime();
370 } else { 371 } else {
371 dst = (uint8_t*)&runtime; 372 dst = (uint8_t*)&runtime;
372 size_t bytes = fread(dst, 1, sizeof(runtime), f); 373 size_t bytes = fread(dst, 1, sizeof(runtime), f);
373 fclose(f); 374 fclose(f);
374 if (bytes != sizeof(runtime)) { 375 if (bytes != sizeof(runtime)) {
375 ESP_LOGE(TAG, "/spiffs/etc/runtime.conf read %d/%d bytes", bytes, sizeof(runtime)); 376 ESP_LOGE(TAG, "/spiffs/etc/runtime.conf read %d/%d bytes", bytes, sizeof(runtime));
377 runtime.MashStep = 0;
378 runtime.MaltAdded = false;
376 } 379 }
377 #if 0 380 #if 0
378 printf("Auto started %s\n", runtime.AutoModeStarted ? "yes":"no"); 381 printf("Auto started %s\n", runtime.AutoModeStarted ? "yes":"no");
379 printf("Stage resume %d\n", runtime.StageResume); 382 printf("Stage resume %d\n", runtime.StageResume);
380 printf("Stage time left %d\n", runtime.StageTimeLeft); 383 printf("Stage time left %d\n", runtime.StageTimeLeft);
387 } 390 }
388 } 391 }
389 392
390 393
391 394
392 void append_recipe() { 395 void append_recipe()
396 {
393 uint8_t *dst = (uint8_t *)&recipe; 397 uint8_t *dst = (uint8_t *)&recipe;
394 FILE *f = fopen("/spiffs/etc/recipe.conf", "a"); 398 FILE *f = fopen("/spiffs/etc/recipe.conf", "a");
395 399
396 if (f == NULL) { 400 if (f == NULL) {
397 ESP_LOGE(TAG, "append /spiffs/etc/recipe.conf failed"); 401 ESP_LOGE(TAG, "append /spiffs/etc/recipe.conf failed");
398 return; 402 return;
399 } 403 }
400 404
401 size_t bytes = fwrite(dst, 1, sizeof(recipe), f); 405 size_t bytes = fwrite(dst, 1, recipe_hdr.recsize, f);
402 fclose(f); 406 fclose(f);
403 ESP_LOGI(TAG, "/spiffs/etc/recipe.conf appended %d bytes", bytes); 407 ESP_LOGI(TAG, "/spiffs/etc/recipe.conf appended %d bytes", bytes);
404 } 408 }
405 409
406 410
413 if (f == NULL) { 417 if (f == NULL) {
414 ESP_LOGE(TAG, "write /spiffs/etc/recipe.conf failed"); 418 ESP_LOGE(TAG, "write /spiffs/etc/recipe.conf failed");
415 return; 419 return;
416 } 420 }
417 421
418 fseek(f, (RecNo - 1) * sizeof(recipe), SEEK_SET); 422 fseek(f, (RecNo - 1) * recipe_hdr.recsize + recipe_hdr.hdrsize, SEEK_SET);
419 size_t bytes = fwrite(dst, 1, sizeof(recipe), f); 423 size_t bytes = fwrite(dst, 1, recipe_hdr.recsize, f);
420 fclose(f); 424 fclose(f);
421 if (bytes != sizeof(recipe)) { 425 if (bytes != recipe_hdr.recsize) {
422 ESP_LOGE(TAG, "/spiffs/etc/recipe.conf write record %d, %d/%d bytes", RecNo, bytes, sizeof(recipe)); 426 ESP_LOGE(TAG, "/spiffs/etc/recipe.conf write record %d, %d/%d bytes", RecNo, bytes, recipe_hdr.recsize);
423 } 427 }
424 } 428 }
425 429
426 430
427 431
428 void read_recipe(int RecNo) 432 void read_recipe(int RecNo)
429 { 433 {
430 uint8_t *dst; 434 uint8_t *dst;
435 size_t bytes;
431 FILE *f = fopen("/spiffs/etc/recipe.conf", "r"); 436 FILE *f = fopen("/spiffs/etc/recipe.conf", "r");
432 437
433 if (f == NULL) { 438 if (f == NULL) {
434 // No recipe yet, create it. 439 // No recipe yet, create it.
440 dst = (uint8_t*)&recipe_hdr;
441 memset(dst, 0, sizeof(recipe_hdr));
442 recipe_hdr.version = RECIPE_VERSION;
443 recipe_hdr.hdrsize = sizeof(recipe_hdr);
444 recipe_hdr.recsize = sizeof(recipe);
445 recipe_hdr.mashmax = MASH_MAX;
446 recipe_hdr.additionmax = ADDITION_MAX;
447 f = fopen("/spiffs/etc/recipe.conf", "w");
448 bytes = fwrite(dst, 1, sizeof(recipe_hdr), f);
449 if (bytes != sizeof(recipe_hdr)) {
450 ESP_LOGE(TAG, "/spiffs/etc/recipe.conf write header, %d/%d bytes", bytes, sizeof(recipe_hdr));
451 }
435 dst = (uint8_t*)&recipe; 452 dst = (uint8_t*)&recipe;
436 memset(dst, 0, sizeof(recipe)); 453 memset(dst, 0, sizeof(recipe));
437 recipe.Version = 1;
438 sprintf(recipe.Name, "Recipe 1"); 454 sprintf(recipe.Name, "Recipe 1");
439 sprintf(recipe.Code, "001"); 455 sprintf(recipe.Code, "001");
440 sprintf(recipe.MashStep[0].Name, "Mash-in"); 456 sprintf(recipe.MashStep[0].Name, "Mash-in");
441 recipe.MashStep[0].Type = MASHTYPE_INFUSION; 457 recipe.MashStep[0].Type = MASHTYPE_INFUSION;
442 recipe.MashStep[0].Temperature = recipe.MashStep[0].Infusion_temp = 67.5; 458 recipe.MashStep[0].Step_temp = recipe.MashStep[0].End_temp = recipe.MashStep[0].Infuse_temp = 67.5;
443 recipe.MashStep[0].Infusion_amount = 15.0; 459 recipe.MashStep[0].Infuse_amount = 15.0;
444 recipe.MashStep[0].Resttime = 1; 460 recipe.MashStep[0].Step_time = 1;
445 recipe.MashStep[0].Ramptime = 1; 461 recipe.MashStep[0].Ramp_time = 1;
446 for (int i = 1; i < 8; i++) 462 for (int i = 1; i < MASH_MAX; i++)
447 recipe.MashStep[i].Type = MASHTYPE_TEMPERATURE; 463 recipe.MashStep[i].Type = MASHTYPE_TEMPERATURE;
448 sprintf(recipe.MashStep[1].Name, "Mash"); 464 sprintf(recipe.MashStep[1].Name, "Mash");
449 recipe.MashStep[1].Temperature = 67.0; 465 recipe.MashStep[1].Step_temp = recipe.MashStep[1].End_temp = 67.0;
450 recipe.MashStep[1].Resttime = 75; 466 recipe.MashStep[1].Step_time = 75;
451 recipe.MashStep[1].Ramptime = 1; 467 recipe.MashStep[1].Ramp_time = 1;
452 sprintf(recipe.MashStep[7].Name, "Mash-out"); 468 sprintf(recipe.MashStep[2].Name, "Mash-out");
453 recipe.MashStep[7].Temperature = 78.0; 469 recipe.MashStep[2].Step_temp = recipe.MashStep[2].End_temp = 78.0;
454 recipe.MashStep[7].Resttime = 5; 470 recipe.MashStep[2].Step_time = 5;
455 recipe.MashStep[7].Ramptime = 11; 471 recipe.MashStep[2].Ramp_time = 11;
472 recipe.Mashsteps = 3;
456 recipe.BoilTime = 60; 473 recipe.BoilTime = 60;
457 recipe.Additions = 2; 474 recipe.Additions = 2;
458 sprintf(recipe.Addition[0].Name, "Hop"); 475 sprintf(recipe.Addition[0].Name, "Hop");
459 recipe.Addition[0].Time = 60; 476 recipe.Addition[0].Time = 60;
460 recipe.Addition[0].Type = ADDITION_HOP; 477 recipe.Addition[0].Type = ADDITION_HOP;
465 recipe.Whirlpool9 = 0; 482 recipe.Whirlpool9 = 0;
466 recipe.Whirlpool7 = 0; 483 recipe.Whirlpool7 = 0;
467 recipe.Whirlpool6 = 0; 484 recipe.Whirlpool6 = 0;
468 recipe.Whirlpool2 = 0; 485 recipe.Whirlpool2 = 0;
469 recipe.SpargeTemp = 85.0; 486 recipe.SpargeTemp = 85.0;
470 append_recipe(); 487 bytes = fwrite(dst, 1, sizeof(recipe), f);
488 fclose(f);
471 } else { 489 } else {
490 /*
491 * Try to read the new file header
492 */
493 dst = (uint8_t*)&recipe_hdr;
494 fseek(f, 0, SEEK_SET);
495 bytes = fread(dst, 1, sizeof(recipe_hdr), f);
496 if (bytes != sizeof(recipe_hdr)) {
497 ESP_LOGE(TAG, "/spiffs/etc/recipe.conf read header, %d/%d bytes", bytes, sizeof(recipe_hdr));
498 fclose(f);
499 return;
500 }
501 /*
502 if (recipe_hdr.version < RECIPE_VERSION) {
503 FILE *nf = fopen("/spiffs/etc/recipe.new", "w");
504
505 ESP_LOGI(TAG, "/spiffs/etc/recipe.conf version %d, new %d", recipe_hdr.version, RECIPE_VERSION);
506
507 fseek(f, recipe_hdr.hdrsize, SEEK_SET);
508 dst = (uint8_t*)&recipe;
509 while ((bytes = fread(dst, 1, recipe_hdr.recsize, f))) {
510
511 // Upgrade data here
512 bytes = fwrite(dst, 1, sizeof(recipe), nf);
513 if (bytes != sizeof(recipe)) {
514 ESP_LOGE(TAG, "/spiffs/etc/recipe.new write data, %d/%d bytes", bytes, sizeof(recipe));
515 }
516 }
517 // Update the header with new sizes
518 fclose(nf);
519 fclose(f);
520 rename("/spiffs/etc/recipe.conf", "/spiffs/etc/recipe.old");
521 rename("/spiffs/etc/recipe.new", "/spiffs/etc/recipe.conf");
522 unlink("/spiffs/etc/recipe.old");
523 f = fopen("/spiffs/etc/recipe.conf", "r");
524 }
525 */
472 dst = (uint8_t*)&recipe; 526 dst = (uint8_t*)&recipe;
473 fseek(f, (RecNo - 1) * sizeof(recipe), SEEK_SET); 527 fseek(f, (RecNo - 1) * recipe_hdr.recsize + recipe_hdr.hdrsize, SEEK_SET);
474 size_t bytes = fread(dst, 1, sizeof(recipe), f); 528 bytes = fread(dst, 1, sizeof(recipe), f);
475 fclose(f); 529 fclose(f);
476 if (bytes != sizeof(recipe)) { 530 if (bytes != sizeof(recipe)) {
477 ESP_LOGE(TAG, "/spiffs/etc/recipe.conf read record %d, %d/%d bytes", RecNo, bytes, sizeof(recipe)); 531 ESP_LOGE(TAG, "/spiffs/etc/recipe.conf read record %d, %d/%d bytes", RecNo, bytes, sizeof(recipe));
478 } 532 }
479 } 533 }
498 ESP_LOGE(TAG, "cannot open spiffs/etc/recipe.conf for reading"); 552 ESP_LOGE(TAG, "cannot open spiffs/etc/recipe.conf for reading");
499 fclose(n); 553 fclose(n);
500 return; 554 return;
501 } 555 }
502 556
557 dst = (uint8_t*)&recipe_hdr;
558 fread(dst, 1, recipe_hdr.hdrsize, o);
559 fwrite(dst, 1, recipe_hdr.hdrsize, n);
560
503 dst = (uint8_t*)&recipe; 561 dst = (uint8_t*)&recipe;
504 while (true) { 562 while (true) {
505 bytes = fread(dst, 1, sizeof(recipe), o); 563 bytes = fread(dst, 1, recipe_hdr.recsize, o);
506 if (bytes == 0) 564 if (bytes == 0)
507 break; 565 break;
508 566
509 if (RecRead != RecNo) { 567 if (RecRead != RecNo) {
510 // Record to copy 568 // Record to copy
511 if ((config.RecipeRec == RecRead) && (config.RecipeRec != RecWrite)) { 569 if ((config.RecipeRec == RecRead) && (config.RecipeRec != RecWrite)) {
512 // We need to change the default record. 570 // We need to change the default record.
513 config.RecipeRec = RecWrite; 571 config.RecipeRec = RecWrite;
514 write_config(); 572 write_config();
515 } 573 }
516 fwrite(dst, 1, sizeof(recipe), n); 574 fwrite(dst, 1, recipe_hdr.recsize, n);
517 RecWrite++; 575 RecWrite++;
518 } 576 }
519 RecRead++; 577 RecRead++;
520 } 578 }
521 fclose(o); 579 fclose(o);

mercurial