diff -r 000000000000 -r b74b0e4902c3 main/config.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/config.c Sat Oct 20 13:23:15 2018 +0200 @@ -0,0 +1,513 @@ +/** + * @file config.c + * @brief BrewBoard configuration files. + */ + +#include "config.h" + +static const char *TAG = "config"; + + +void write_config() { + uint8_t *dst = (uint8_t *)&config; + FILE *f = fopen("/spiffs/etc/config.conf", "w+"); + + if (f == NULL) { + ESP_LOGE(TAG, "write /spiffs/etc/config.conf failed"); + return; + } + + size_t bytes = fwrite(dst, 1, sizeof(config), f); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/config.conf written %d bytes", bytes); +} + + + +void read_config() { + uint8_t *dst; + uint8_t mac_addr[8] = {0}; + FILE *f = fopen("/spiffs/etc/config.conf", "r"); + + if (f == NULL) { + // No configuration yet, create it. + esp_efuse_mac_get_default(mac_addr); + config.Version = 1; + config.Unit = 'C'; + config.BoilTemperature = 99.0; + config.AskAdd = true; + config.AskRemove = true; + config.AskIodine = true; + config.IodineTime = 30; + config.EquipmentRec = 1; + sprintf(config.hostname, "brewboard-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); + sprintf(config.ap_ssid, "brewboardAP-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); + sprintf(config.ap_pwd, "%s", "triplehop"); + config.ap_channel = 5; + config.ap_ssid_hidden = 0; + config.ap_bandwidth = WIFI_BW_HT20; + config.ts_xleft = 0; + config.ts_xright = 3600; + config.ts_ytop = 3600; + config.ts_ybottom = 0; + config.RecipeRec = 1; + sprintf(config.uuid, "c0ffeeee-dead-beef-cafe-%02x%02x%02x%02x%02x%02x", + mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); + write_config(); + } else { + dst = (uint8_t*)&config; + size_t bytes = fread(dst, 1, sizeof(config), f); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/config.conf read %d bytes", bytes); + if (config.AskIodine && ! config.IodineTime) { + config.IodineTime = 30; + write_config(); + } + if (strlen(config.uuid) !=36) { + esp_efuse_mac_get_default(mac_addr); + sprintf(config.uuid, "c0ffeeee-dead-beef-cafe-%02x%02x%02x%02x%02x%02x", + mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); + write_config(); + } + } +} + + + +void append_equipment() { + uint8_t *dst = (uint8_t *)&equipment; + FILE *f = fopen("/spiffs/etc/equipments.conf", "a"); + + if (f == NULL) { + ESP_LOGE(TAG, "append /spiffs/etc/equipments.conf failed"); + return; + } + + size_t bytes = fwrite(dst, 1, sizeof(equipment), f); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/equipments.conf appended %d bytes", bytes); +} + + + +void read_equipment(int RecNo) { + uint8_t *dst; + FILE *f = fopen("/spiffs/etc/equipments.conf", "r"); + + if (f == NULL) { + // No configuration yet, create it. + equipment.Version = 1; + equipment.Record = 1; + sprintf(equipment.Name, "default"); + equipment.BoilPower = 80; + equipment.MashPower = 100; + equipment.PumpCycle = 8; + equipment.PumpRest = 2; + equipment.PumpPreMash = true; + equipment.PumpOnMash = true; + equipment.PumpMashOut = true; + equipment.PumpOnBoil = false; + equipment.PumpMaxTemp = 80; + equipment.PIDPipe = true; + equipment.SSR2 = 0; + equipment.TempHLT = 85.0; + equipment.PID_kP = 150.0; + equipment.PID_kI = 1.5; + equipment.PID_kD = 15000.0; + equipment.PID_POn = PID_P_ON_E; + equipment.SampleTime = 5000; + append_equipment(); + } else { + dst = (uint8_t*)&equipment; + while (1) { + size_t bytes = fread(dst, 1, sizeof(equipment), f); + if (bytes && equipment.Record == RecNo) { + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/equipments.conf read %d bytes, record %d: %s", bytes, RecNo, equipment.Name); + return; + } + if (bytes == 0) + break; + } + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/equipments.conf read error, record %d not found", RecNo); + } +} + + + +void write_equipment(int RecNo) +{ + uint8_t *dst = (uint8_t *)&equipment; + FILE *f; + + f = fopen("/spiffs/etc/equipments.conf", "r+"); + if (f == NULL) { + ESP_LOGE(TAG, "write /spiffs/etc/equipments.conf failed"); + return; + } + fseek(f, (RecNo - 1) * sizeof(equipment), SEEK_SET); + size_t bytes = fwrite(dst, 1, sizeof(equipment), f); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/equipments.conf update record %d, %d bytes", RecNo, bytes); +} + + + +void delete_equipment(int RecNo) +{ + int RecNow = 1; + FILE *n, *o; + uint8_t *dst; + size_t bytes; + + n = fopen("/spiffs/etc/equipments.new", "a"); + if (n == NULL) { + ESP_LOGE(TAG, "cannot create /spiffs/etc/equipments.new"); + return; + } + o = fopen("/spiffs/etc/equipments.conf", "r"); + if (o == NULL) { + ESP_LOGE(TAG, "cannot open spiffs/etc/equipments.conf for reading"); + fclose(n); + return; + } + + dst = (uint8_t*)&equipment; + while (true) { + bytes = fread(dst, 1, sizeof(equipment), o); + if (bytes == 0) + break; + + if (equipment.Record == RecNo) { + // Record to delete, don't copy + printf("Ditch %d\n", RecNo); + } else { + if ((config.EquipmentRec == equipment.Record) && (config.EquipmentRec != RecNow)) { + // We need to change the default record. + config.EquipmentRec = RecNow; + write_config(); + } + printf("Copy %d to %d\n", equipment.Record, RecNow); + equipment.Record = RecNow; + fwrite(dst, 1, sizeof(equipment), n); + RecNow++; + } + } + fclose(o); + fclose(n); + + rename("/spiffs/etc/equipments.conf", "/spiffs/etc/equipments.old"); + rename("/spiffs/etc/equipments.new", "/spiffs/etc/equipments.conf"); + unlink("/spiffs/etc/equipments.old"); +} + + + +int add_station(uint8_t *SSID, uint8_t *Password) +{ + FILE *f; + uint8_t *dst = (uint8_t *)&wifiStation; + + if (read_station(SSID) >= 0) { + ESP_LOGE(TAG, "add_station %s already excists", SSID); + return -1; + } + + f = fopen("/spiffs/etc/stations.conf", "a+"); + if (f == NULL) { + ESP_LOGE(TAG, "write /spiffs/etc/stations.conf failed"); + return -1; + } + memset(dst, 0, sizeof(wifiStation)); + sprintf(wifiStation.SSID, "%s", (char *)SSID); + sprintf(wifiStation.Password, "%s", (char *)Password); + wifiStation.hide = false; + fwrite(dst, 1, sizeof(wifiStation), f); + fclose(f); + + ESP_LOGI(TAG, "add_station %s record: %d", (char *)SSID, read_station(SSID)); + /* Return the record number */ + return read_station(SSID); +} + + + +int read_station(uint8_t *SSID) +{ + uint8_t *dst = (uint8_t *)&wifiStation; + static int rc; + FILE *f; + size_t bytes; + + if ((SSID == NULL) || (strlen((char *)SSID) == 0)) { + ESP_LOGI(TAG, "read_station(NULL)"); + return -1; + } + + memset(dst, 0, sizeof(wifiStation)); + f = fopen("/spiffs/etc/stations.conf", "r+"); + if (f == NULL) { + f = fopen("/spiffs/etc/stations.conf", "w+"); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/stations.conf created, return -1"); + return -1; + } + + rc = 0; + fseek(f, 0, SEEK_SET); + + while (1) { + bytes = fread(dst, 1, sizeof(wifiStation), f); + if (bytes < sizeof(wifiStation)) { + fclose(f); + memset(dst, 0, sizeof(wifiStation)); + return -1; + } + if (strcmp(wifiStation.SSID, (char *)SSID) == 0) { + // Fount it + fclose(f); +// ESP_LOGI(TAG, "read_station %s record %d", (char *)SSID, rc); + return rc; + } + rc++; + } + return -1; +} + + + +void remove_station(uint8_t *SSID) +{ + FILE *n, *o; + uint8_t *dst; + size_t bytes; + + n = fopen("/spiffs/etc/stations.new", "a"); + if (n == NULL) { + ESP_LOGE(TAG, "cannot create /spiffs/etc/stations.new"); + return; + } + o = fopen("/spiffs/etc/stations.conf", "r"); + if (o == NULL) { + ESP_LOGE(TAG, "cannot open spiffs/etc/stations.conf for reading"); + fclose(n); + return; + } + + dst = (uint8_t*)&wifiStation; + while (true) { + bytes = fread(dst, 1, sizeof(wifiStation), o); + if (bytes == 0) + break; + + if ((strcmp((char *)SSID, wifiStation.SSID) == 0) || (strlen(wifiStation.SSID) == 0)) { + // Record to delete, don't copy + } else { + fwrite(dst, 1, sizeof(wifiStation), n); + } + } + fclose(o); + fclose(n); + + rename("/spiffs/etc/stations.conf", "/spiffs/etc/stations.old"); + rename("/spiffs/etc/stations.new", "/spiffs/etc/stations.conf"); + unlink("/spiffs/etc/stations.old"); +} + + + +int blacklist_station(uint8_t *SSID) +{ + return -1; +} + + + +void write_runtime() +{ + uint8_t *dst = (uint8_t *)&runtime; + FILE *f = fopen("/spiffs/etc/runtime.conf", "w+"); + + if (f == NULL) { + ESP_LOGE(TAG, "write /spiffs/etc/runtime.conf failed"); + return; + } + + size_t bytes = fwrite(dst, 1, sizeof(runtime), f); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/runtime.conf written %d bytes", bytes); +} + + + +void read_runtime() +{ + uint8_t *dst; + FILE *f = fopen("/spiffs/etc/runtime.conf", "r"); + + if (f == NULL) { + // No runtime yet, create it. + runtime.Version = 1; + runtime.AutoModeStarted = false; + runtime.StageResume = 0; + runtime.StageTimeLeft = 0; + runtime.HopAddition = 0; + runtime.ManualMLT = 45.0; + runtime.ManualHLT = 45.0; + runtime.BrewStart = (time_t)0; + runtime.Logfile[0] = '\0'; + runtime.PumpCooling = false; + runtime.TimeBrewing = 0; + write_runtime(); + } else { + dst = (uint8_t*)&runtime; + size_t bytes = fread(dst, 1, sizeof(runtime), f); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/runtime.conf read %d bytes", bytes); +#if 0 + printf("Auto started %s\n", runtime.AutoModeStarted ? "yes":"no"); + printf("Stage resume %d\n", runtime.StageResume); + printf("Stage time left %d\n", runtime.StageTimeLeft); + printf("Hop addition %d\n", runtime.HopAddition); + printf("Brew start %d\n", (int)runtime.BrewStart); + printf("Log file %s\n", runtime.Logfile); + printf("Pump cooling %s\n", runtime.PumpCooling ? "yes":"no"); + printf("Time brewing %d\n", runtime.TimeBrewing); +#endif + } +} + + + +void append_recipe() { + uint8_t *dst = (uint8_t *)&recipe; + FILE *f = fopen("/spiffs/etc/recipe.conf", "a"); + + if (f == NULL) { + ESP_LOGE(TAG, "append /spiffs/etc/recipe.conf failed"); + return; + } + + size_t bytes = fwrite(dst, 1, sizeof(recipe), f); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/recipe.conf appended %d bytes", bytes); +} + + + +void write_recipe(int RecNo) +{ + uint8_t *dst = (uint8_t *)&recipe; + FILE *f = fopen("/spiffs/etc/recipe.conf", "r+"); + + if (f == NULL) { + ESP_LOGE(TAG, "write /spiffs/etc/recipe.conf failed"); + return; + } + + fseek(f, (RecNo - 1) * sizeof(recipe), SEEK_SET); + size_t bytes = fwrite(dst, 1, sizeof(recipe), f); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/recipe.conf written record %d, %d bytes", RecNo, bytes); +} + + + +void read_recipe(int RecNo) +{ + uint8_t *dst; + FILE *f = fopen("/spiffs/etc/recipe.conf", "r"); + + if (f == NULL) { + // No runtime yet, create it. + dst = (uint8_t*)&recipe; + memset(dst, 0, sizeof(recipe)); + recipe.Version = 1; + recipe.Record = 1; + sprintf(recipe.Name, "Recipe 1"); + sprintf(recipe.Code, "001"); + sprintf(recipe.MashStep[0].Name, "Mash-in"); + recipe.MashStep[0].Temperature = 67.5; + recipe.MashStep[0].Resttime = 1; + sprintf(recipe.MashStep[1].Name, "Mash"); + recipe.MashStep[1].Temperature = 67.0; + recipe.MashStep[1].Resttime = 75; + sprintf(recipe.MashStep[7].Name, "Mash-out"); + recipe.MashStep[7].Temperature = 78.0; + recipe.MashStep[7].Resttime = 5; + recipe.BoilTime = 60; + recipe.Additions = 2; + sprintf(recipe.Addition[0].Name, "Hop"); + recipe.Addition[0].Time = 60; + recipe.Addition[0].Type = ADDITION_HOP; + sprintf(recipe.Addition[1].Name, "Hop"); + recipe.Addition[1].Time = 10; + recipe.Addition[1].Type = ADDITION_HOP; + recipe.CoolTemp = 20.0; + recipe.Whirlpool9 = 0; + recipe.Whirlpool7 = 0; + recipe.Whirlpool6 = 0; + recipe.Whirlpool2 = 0; + recipe.SpargeTemp = 85.0; + append_recipe(); + } else { + dst = (uint8_t*)&recipe; + fseek(f, (RecNo - 1) * sizeof(recipe), SEEK_SET); + size_t bytes = fread(dst, 1, sizeof(recipe), f); + fclose(f); + ESP_LOGI(TAG, "/spiffs/etc/recipe.conf read record %d, %d bytes", RecNo, bytes); + } +} + + + +void delete_recipe(int RecNo) +{ + int RecNow = 1; + FILE *n, *o; + uint8_t *dst; + size_t bytes; + + n = fopen("/spiffs/etc/recipe.new", "a"); + if (n == NULL) { + ESP_LOGE(TAG, "cannot create /spiffs/etc/recipe.new"); + return; + } + o = fopen("/spiffs/etc/recipe.conf", "r"); + if (o == NULL) { + ESP_LOGE(TAG, "cannot open spiffs/etc/recipe.conf for reading"); + fclose(n); + return; + } + + dst = (uint8_t*)&recipe; + while (true) { + bytes = fread(dst, 1, sizeof(recipe), o); + if (bytes == 0) + break; + + if (recipe.Record == RecNo) { + // Record to delete, don't copy + printf("Ditch %d\n", RecNo); + } else { + if ((config.RecipeRec == recipe.Record) && (config.RecipeRec != RecNow)) { + // We need to change the default record. + config.RecipeRec = RecNow; + write_config(); + } + printf("Copy %d to %d\n", recipe.Record, RecNow); + recipe.Record = RecNow; + fwrite(dst, 1, sizeof(recipe), n); + RecNow++; + } + } + fclose(o); + fclose(n); + + rename("/spiffs/etc/recipe.conf", "/spiffs/etc/recipe.old"); + rename("/spiffs/etc/recipe.new", "/spiffs/etc/recipe.conf"); + unlink("/spiffs/etc/recipe.old"); +} + +