diff -r 9c69d43bfb06 -r 1e0192b295b9 thermferm/server.c --- a/thermferm/server.c Thu Jan 10 16:33:42 2019 +0100 +++ b/thermferm/server.c Mon Jan 14 22:46:27 2019 +0100 @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (C) 2008-2018 + * Copyright (C) 2008-2019 * * Michiel Broek * @@ -185,57 +185,6 @@ -int delete_Profile(char *uuid) -{ - profiles_list *current = Config.profiles; - profiles_list *previous = NULL; - prof_step *step, *olds; - - while (current) { - if (strcmp(current->uuid, uuid) == 0) { - if (previous == NULL) { - Config.profiles = current->next; - free(current->uuid); - current->uuid = NULL; - free(current->name); - current->name = NULL; - if (current->steps) { - for (step = current->steps; step; step = olds) { - olds = step->next; - free(step); - } - current->steps = NULL; - } - free(current); - return 1; - } else { - free(current->uuid); - current->uuid = NULL; - free(current->name); - current->name = NULL; - if (current->steps) { - for (step = current->steps; step; step = olds) { - olds = step->next; - free(step); - } - current->steps = NULL; - } - previous->next = current->next; - free(current); - current = previous->next; - return 1; - } - } else { - previous = current; - current = current->next; - } - } - - return 0; -} - - - void tidy_lslist(ls_list **lap) { ls_list *tmp, *old; @@ -699,7 +648,6 @@ device = (devices_list *)malloc(sizeof(devices_list)); device->next = NULL; - device->version = 1; device->uuid = malloc(37); uuid_generate(uu); uuid_unparse(uu, device->uuid); @@ -1200,266 +1148,6 @@ -/* - * PROFILE ADD name Add a new profile - * PROFILE DEL uuid Delete profile with uuid - * PROFILE LIST List available profiles - * PROFILE GET uuid Get profile record - * PROFILE PUT uuid Put profile record - * PROFILE GETS uuid Get profile steps list - * PROFILE PUTS uuid Put profile steps list - */ -int cmd_profile(char *buf) -{ - char ibuf[SS_BUFSIZE], *sstep, *rest, *tlarg, *tharg, *frarg, *param, *kwd, *val; - int j, ival, rlen, istep, irest, ifrarg; - float ftlarg, ftharg, fval; - char *opt; - profiles_list *profile, *tmpp; - prof_step *step, *olds; - uuid_t uu; - - opt = strtok(buf, " \0"); - opt = strtok(NULL, " \0"); - - if (opt == NULL) { - srv_send((char *)"501 Subcommand missing"); - return 0; - } - - if (strcmp(opt, (char *)"HELP") == 0) { - srv_send((char *)"100 Help text follows:"); - srv_send((char *)"Recognized commands:"); - srv_send((char *)"PROFILE uuid,name Profile rename"); - srv_send((char *)"PROFILE ADD name Add new Profile with name"); - srv_send((char *)"PROFILE DEL uuid Delete Profile by uuid"); - srv_send((char *)"PROFILE LIST List available profiles"); - srv_send((char *)"PROFILE GET uuid Get Profile record by uuid"); - srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid"); - srv_send((char *)"PROFILE GETS uuid Profile get steps list"); - srv_send((char *)"PROFILE PUTS uuid Profile put steps list"); - srv_send((char *)"."); - return 0; - } - - if (strcmp(opt, (char *)"LIST") == 0) { - /* - * Fermenting profiles - */ - srv_send((char *)"212 Profiles list follows:"); - for (profile = Config.profiles; profile; profile = profile->next) { - j = 0; - for (step = profile->steps; step; step = step->next) - j++; - srv_send((char *)"%s,%s,%d,%d", profile->uuid, profile->name, j, profile->busy); - } - srv_send((char *)"."); - return 0; - } - - param = strtok(NULL, "\0"); - if (param == NULL) { - srv_send((char *)"502 Parameter missing"); - return 0; - } - - if (strcmp(opt, (char *)"ADD") == 0) { - profile = (profiles_list *)malloc(sizeof(profiles_list)); - profile->next = NULL; - profile->version = 1; - profile->uuid = malloc(37); - uuid_generate(uu); - uuid_unparse(uu, profile->uuid); - profile->name = xstrcpy(param); - profile->busy = profile->fridge_mode = 0; - profile->inittemp_lo = 19.8; - profile->inittemp_hi = 20.2; - profile->steps = NULL; - if (Config.profiles == NULL) { - Config.profiles = profile; - } else { - for (tmpp = Config.profiles; tmpp; tmpp = tmpp->next) { - if (tmpp->next == NULL) { - tmpp->next = profile; - break; - } - } - } - - syslog(LOG_NOTICE, "Profile %s added", profile->uuid); - srv_send((char *)"211 Profile %s added", profile->uuid); - return 1; - - - } else if (strcmp(opt, (char *)"DEL") == 0) { - if (delete_Profile(param)) { - syslog(LOG_NOTICE, "Profile %s deleted", param); - srv_send((char *)"211 Profile %s deleted", param); - return 1; - } else { - srv_send((char *)"440 No such profile"); - return 0; - } - - } else if (strcmp(opt, (char *)"GET") == 0) { - for (profile = Config.profiles; profile; profile = profile->next) { - if (strcmp(profile->uuid, param) == 0) { - srv_send((char *)"213 Profile record follows:"); - srv_send((char *)"UUID,%s", profile->uuid); - srv_send((char *)"NAME,%s", profile->name); - srv_send((char *)"INITTEMP_LO,%.1f", profile->inittemp_lo); - srv_send((char *)"INITTEMP_HI,%.1f", profile->inittemp_hi); - srv_send((char *)"FRIDGE_MODE,%d", profile->fridge_mode); - srv_send((char *)"."); - return 0; - } - } - srv_send((char *)"440 No such profile"); - return 0; - - } else if (strcmp(opt, (char *)"PUT") == 0) { - for (profile = Config.profiles; profile; profile = profile->next) { - if (strcmp(profile->uuid, param) == 0) { - while (1) { - rlen = srv_recv(ibuf); - if (rlen == -1) { - return 0; - } - if (strlen(ibuf)) { - if (strcmp(ibuf, (char *)".") == 0) { - srv_send((char *)"219 Accepted Profile record"); - return 1; - } - kwd = strtok(ibuf, ",\0"); - val = strtok(NULL, "\0"); - if (kwd && val) { - if (strcmp(kwd, (char *)"NAME") == 0) { - if (profile->name) { - if (strcmp(profile->name, val)) - syslog(LOG_NOTICE, "Profile %s name `%s' to `%s'", profile->uuid, profile->name, val); - free(profile->name); - } - profile->name = xstrcpy(val); - } else if (strcmp(kwd, (char *)"INITTEMP_LO") == 0) { - if (sscanf(val, "%f", &fval) == 1) { - if (profile->inittemp_lo != fval) - syslog(LOG_NOTICE, "Profile %s initial temperature low %.1f to %.1f", profile->uuid, profile->inittemp_lo, fval); - profile->inittemp_lo = fval; - } - } else if (strcmp(kwd, (char *)"INITTEMP_HI") == 0) { - if (sscanf(val, "%f", &fval) == 1) { - if (profile->inittemp_hi != fval) - syslog(LOG_NOTICE, "Profile %s initial temperature high %.1f to %.1f", profile->uuid, profile->inittemp_hi, fval); - profile->inittemp_hi = fval; - } - } else if (strcmp(kwd, (char *)"FRIDGE_MODE") == 0) { - if (sscanf(val, "%d", &ival) == 1) { - if (profile->fridge_mode != ival) - syslog(LOG_NOTICE, "Profile %s fridge mode %d to %d", profile->uuid, profile->fridge_mode, ival); - profile->fridge_mode = ival; - } - } - } - } - } - } - } - srv_send((char *)"440 No such profile"); - return 0; - - } else if (strcmp(opt, (char *)"GETS") == 0) { - - for (profile = Config.profiles; profile; profile = profile->next) { - if (strcmp(profile->uuid, param) == 0) { - srv_send((char *)"215 Profile steps follow:"); - for (step = profile->steps; step; step = step->next) { - srv_send((char *)"%d,%d,%.1f,%.1f,%d", step->steptime, step->resttime, step->target_lo, step->target_hi, step->fridge_mode); - } - srv_send((char *)"."); - return 0; - } - } - - srv_send((char *)"440 No such profile"); - return 0; - - } else if (strcmp(opt, (char *)"PUTS") == 0) { - - for (profile = Config.profiles; profile; profile = profile->next) { - if (strcmp(profile->uuid, param) == 0) { - - if (profile->steps) { - syslog(LOG_NOTICE, "PROFILE PUTS %s erased all old steps", profile->uuid); - for (step = profile->steps; step; step = olds) { - olds = step->next; - free(step); - } - profile->steps = NULL; - } - - j = 0; - while (1) { - rlen = srv_recv(ibuf); - if (rlen == -1) { - return 0; - } else { - if (strlen(ibuf)) { - if (strcmp(ibuf, (char *)".") == 0) { - - srv_send((char *)"219 Accepted Profile steps"); - return 1; - } - sstep = strtok(ibuf, ",\0"); - rest = strtok(NULL, ",\0"); - tlarg = strtok(NULL, ",\0"); - tharg = strtok(NULL, ",\0"); - frarg = strtok(NULL, "\0"); - - if ((sscanf(sstep, "%d", &istep) == 1) && - (sscanf(rest, "%d", &irest) == 1) && - (sscanf(tlarg, "%f", &ftlarg) == 1) && - (sscanf(tharg, "%f", &ftharg) == 1) && - (sscanf(frarg, "%d", &ifrarg) == 1)) { - - j++; - syslog(LOG_NOTICE, "PROFILE PUTS %s add step %d: steptime=%d resttime=%d target=%.1f..%.1f fridge_mode=%d", - profile->uuid, j, istep, irest, ftlarg, ftharg, ifrarg); - step = (prof_step *)malloc(sizeof(prof_step)); - step->next = NULL; - step->version = 1; - step->steptime = istep; - step->resttime = irest; - step->target_lo = ftlarg; - step->target_hi = ftharg; - step->fridge_mode = ifrarg; - - if (profile->steps == NULL) { - profile->steps = step; - } else { - for (olds = profile->steps; olds; olds = olds->next) { - if (olds->next == NULL) { - olds->next = step; - break; - } - } - } - } - } - } - } - } - } - - srv_send((char *)"440 No such profile"); - return 0; - } - - srv_send((char *)"504 Subcommand error"); - return 0; -} - - - #ifdef USE_SIMULATOR int delete_Simulator(char *uuid) { @@ -1558,7 +1246,6 @@ simulator = (simulator_list *)malloc(sizeof(simulator_list)); simulator->next = NULL; - simulator->version = 1; simulator->uuid = malloc(37); uuid_generate(uu); uuid_unparse(uu, simulator->uuid); @@ -1794,6 +1481,7 @@ { units_list *current = Config.units; units_list *previous = NULL; + prof_step *step, *olds; while (current) { if (strcmp(current->uuid, uuid) == 0) { @@ -1837,15 +1525,25 @@ if (current->psu_address) free(current->psu_address); current->psu_address = NULL; - if (current->profile) - free(current->profile); - current->profile = NULL; + if (current->profile_uuid) + free(current->profile_uuid); + current->profile_uuid = NULL; + if (current->profile_name) + free(current->profile_name); + current->profile_name = NULL; if (current->PID_cool) free(current->PID_cool); current->PID_cool = NULL; if (current->PID_heat) free(current->PID_heat); current->PID_heat = NULL; + if (current->profile_steps) { + for (step = current->profile_steps; step; step = olds) { + olds = step->next; + free(step); + } + current->profile_steps = NULL; + } free(current); return 1; } else { @@ -1887,15 +1585,25 @@ if (current->psu_address) free(current->psu_address); current->psu_address = NULL; - if (current->profile) - free(current->profile); - current->profile = NULL; + if (current->profile_uuid) + free(current->profile_uuid); + current->profile_uuid = NULL; + if (current->profile_name) + free(current->profile_name); + current->profile_name = NULL; if (current->PID_cool) free(current->PID_cool); current->PID_cool = NULL; if (current->PID_heat) free(current->PID_heat); current->PID_heat = NULL; + if (current->profile_steps) { + for (step = current->profile_steps; step; step = olds) { + olds = step->next; + free(step); + } + current->profile_steps = NULL; + } previous->next = current->next; free(current); current = previous->next; @@ -1972,7 +1680,6 @@ Config.next_unit++; unit = (units_list *)malloc(sizeof(units_list)); unit->next = NULL; - unit->version = 1; unit->uuid = malloc(37); uuid_generate(uu); uuid_unparse(uu, unit->uuid); @@ -1982,13 +1689,15 @@ unit->alias = xstrcpy(an); unit->air_address = unit->beer_address = unit->chiller_address = unit->heater_address = unit->cooler_address = \ unit->fan_address = unit->door_address = unit->light_address = \ - unit->psu_address = unit->profile = NULL; + unit->psu_address = unit->profile_uuid = unit->profile_name = NULL; unit->air_idx = unit->beer_idx = unit->chiller_idx = unit->heater_idx = unit->cooler_idx = unit->fan_idx = \ - unit->door_idx = unit->light_idx = unit->psu_idx = 0; + unit->door_idx = unit->light_idx = unit->psu_idx = unit->profile_fridge_mode = \ + unit->profile_duration = unit->profile_totalsteps = 0; + unit->profile_steps = NULL; unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0; unit->air_state = unit->beer_state = unit->chiller_state = 1; unit->air_temperature = unit->beer_temperature = unit->chiller_temperature = 20000; - unit->beer_set = unit->fridge_set = 20.0; + unit->beer_set = unit->fridge_set = unit->profile_inittemp_lo = unit->profile_inittemp_hi =20.0; unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = \ unit->light_state = unit->psu_state = unit->prof_state = unit->stage = 0; unit->heater_delay = unit->cooler_delay = unit->fan_delay = 20; /* 5 minutes delay */ @@ -2124,19 +1833,27 @@ srv_send((char *)"PSU_IDX,%d", unit->psu_idx); srv_send((char *)"FRIDGE_SET,%.1f", unit->fridge_set); srv_send((char *)"BEER_SET,%.1f", unit->beer_set); - srv_send((char *)"PROFILE,%s", unit->profile); - srv_send((char *)"PROF_STARTED,%d", (int)unit->prof_started); - if (unit->prof_state == PROFILE_RUN) { - srv_send((char *)"PROF_STATE,%s %d%%", PROFSTATE[unit->prof_state], unit->prof_percent); - } else { - srv_send((char *)"PROF_STATE,%s", PROFSTATE[unit->prof_state]); + if (unit->profile_uuid) { + srv_send((char *)"PROFILE_UUID,%s", unit->profile_uuid); + srv_send((char *)"PROFILE_NAME,%s", unit->profile_name); + srv_send((char *)"PROFILE_INITTEMP_LO,%.1f", unit->profile_inittemp_lo); + srv_send((char *)"PROFILE_INITTEMP_HI,%.1f", unit->profile_inittemp_hi); + srv_send((char *)"PROFILE_FRIDGE_MODE,%d", unit->profile_fridge_mode); + srv_send((char *)"PROFILE_DURATION,%d", unit->profile_duration); + srv_send((char *)"PROFILE_TOTALSTEPS,%d", unit->profile_totalsteps); + srv_send((char *)"PROF_STARTED,%d", (int)unit->prof_started); + if (unit->prof_state == PROFILE_RUN) { + srv_send((char *)"PROF_STATE,%s %d%%", PROFSTATE[unit->prof_state], unit->prof_percent); + } else { + srv_send((char *)"PROF_STATE,%s", PROFSTATE[unit->prof_state]); + } + srv_send((char *)"PROF_TARGET_LO,%.3f", unit->prof_target_lo); + srv_send((char *)"PROF_TARGET_HI,%.3f", unit->prof_target_hi); + srv_send((char *)"PROF_FRIDGE_MODE,%d", unit->prof_fridge_mode); + srv_send((char *)"PROF_PEAK_ABS,%.3f", unit->prof_peak_abs); + srv_send((char *)"PROF_PEAK_REL,%.3f", unit->prof_peak_rel); + srv_send((char *)"PROF_PRIMARY_DONE,%d", (int)unit->prof_primary_done); } - srv_send((char *)"PROF_TARGET_LO,%.3f", unit->prof_target_lo); - srv_send((char *)"PROF_TARGET_HI,%.3f", unit->prof_target_hi); - srv_send((char *)"PROF_FRIDGE_MODE,%d", unit->prof_fridge_mode); - srv_send((char *)"PROF_PEAK_ABS,%.3f", unit->prof_peak_abs); - srv_send((char *)"PROF_PEAK_REL,%.3f", unit->prof_peak_rel); - srv_send((char *)"PROF_PRIMARY_DONE,%d", (int)unit->prof_primary_done); srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min); srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max); srv_send((char *)"ALARM,%d", unit->alarm_flag); @@ -2499,7 +2216,7 @@ */ unit->prof_target_lo = unit->prof_target_hi = 20.0; unit->prof_fridge_mode = 0; - if (unit->profile) { + if (unit->profile_uuid) { unit->mqtt_flag |= MQTT_FLAG_DATA; } } @@ -2593,35 +2310,7 @@ unit->PID_heat->idleRange = fval; } - } else if (strcmp(kwd, (char *)"PROFILE") == 0) { - if (unit->prof_state == PROFILE_OFF) { - /* - * Only change profile if it is not active, else drop this one. - */ - if (unit->profile && val && strcmp(unit->profile, val)) - syslog(LOG_NOTICE, "Fermenter unit %s profile name `%s' to `%s'", unit->uuid, unit->profile, val); - if (unit->profile) - free(unit->profile); - - if (val) - unit->profile = xstrcpy(val); - else - unit->profile = NULL; - /* - * Reset all output devices - */ - unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0; - unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE; - unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0; - unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; - device_out(unit->heater_address, unit->heater_state); - device_out(unit->cooler_address, unit->cooler_state); - device_out(unit->fan_address, unit->fan_state); - device_out(unit->light_address, unit->light_state); - unit->mqtt_flag |= MQTT_FLAG_DATA; - } - - } else if (val && (strcmp(kwd, (char *)"PROF_STATE") == 0)) { + } else if (val && (strcmp(kwd, (char *)"PROF_STATE") == 0) && unit->profile_uuid) { for (i = 0; i < 5; i++) { if (strcmp(val, PROFSTATE[i]) == 0) { switch (i) { @@ -2649,7 +2338,8 @@ case PROFILE_DONE: break; /* Command is illegal */ case PROFILE_ABORT: if ((unit->prof_state == PROFILE_RUN) || (unit->prof_state == PROFILE_PAUSE)) { unit->prof_state = PROFILE_OFF; - unit->prof_started = 0; + unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0; + unit->prof_peak_abs = unit->prof_peak_rel = 0.0; syslog(LOG_NOTICE, "Fermenter unit %s profile ABORT", unit->uuid); } break; @@ -2739,8 +2429,6 @@ srv_send((char *)"LIST [parameters] List commands"); srv_send((char *)"LIST HELP List help screen"); srv_send((char *)"PING Check if server is alive"); - srv_send((char *)"PROFILE [parameters] Profile commands"); - srv_send((char *)"PROFILE HELP Profile help screen"); #ifdef USE_SIMULATOR srv_send((char *)"SIMULATOR [parameters] Simulator commands"); srv_send((char *)"SIMULATOR HELP Simulator help screen"); @@ -2755,10 +2443,6 @@ } else if (strncmp(buf, "PING", 4) == 0) { srv_send((char *)"101 PONG"); - } else if (strncmp(buf, "PROFILE", 7) == 0) { - if (cmd_profile(buf)) - wrconfig(); - #ifdef USE_SIMULATOR } else if (strncmp(buf, "SIMULATOR", 9) == 0) { if (cmd_simulator(buf))