--- a/thermferm/mqtt.c Mon Jan 14 22:46:27 2019 +0100 +++ b/thermferm/mqtt.c Sat Jan 19 11:38:09 2019 +0100 @@ -230,10 +230,9 @@ } if (json_object_object_get_ex(metric, "mode", &val)) { - for (int i = 0; i < 4; i++) { + for (int i = 0; i < 5; i++) { if (strcmp((char *)json_object_get_string(val), UNITMODE[i]) == 0) { if (unit->mode != i) { - syslog(LOG_NOTICE, "DCMD change fermenter %s: mode to %s", message_alias, UNITMODE[i]); unit->mqtt_flag |= MQTT_FLAG_DATA; /* Initialize log if the unit is turned on */ if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) { @@ -242,7 +241,14 @@ } else if ((unit->mode != UNITMODE_OFF) && (i == UNITMODE_OFF)) { unit->mqtt_flag |= MQTT_FLAG_DEATH; } - syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]); + if (i == UNITMODE_PROFILE) { + /* Do some checks and refuse profile mode cannot be set */ + if (unit->profile_uuid == NULL) { + syslog(LOG_NOTICE, "Fermenter unit %s refuse profile, not loaded", message_alias); + break; + } + } + syslog(LOG_NOTICE, "DCMD change fermenter %s: mode to %s", message_alias, UNITMODE[i]); unit->mode = i; /* Allways turn everything off after a mode change */ unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0; @@ -258,11 +264,12 @@ * Set a sane default until it will be overruled by the * main processing loop. */ - unit->prof_target_lo = unit->prof_target_hi = 20.0; + unit->prof_target_lo = unit->profile_inittemp_lo; + unit->prof_target_hi = unit->profile_inittemp_hi;; unit->prof_fridge_mode = 0; - if (unit->profile_uuid) { - unit->mqtt_flag |= MQTT_FLAG_DATA; - } + unit->prof_state = PROFILE_OFF; + unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0; + unit->prof_peak_abs = unit->prof_peak_rel = 0.0; } } break; @@ -349,12 +356,66 @@ syslog(LOG_NOTICE, "DCMD change fermenter %s: product_name to `%s'", message_alias, unit->product_name); } } + if (json_object_object_get_ex(setpoint, "uuid", &val)) { + if (strcmp((char *)json_object_get_string(val), unit->product_uuid)) { + free(unit->product_uuid); + unit->product_uuid = xstrcpy((char *)json_object_get_string(val)); + unit->mqtt_flag |= MQTT_FLAG_DATA; + syslog(LOG_NOTICE, "DCMD change fermenter %s: product_uuid to `%s'", message_alias, unit->product_uuid); + } + } } if (json_object_object_get_ex(metric, "profile", &profile)) { if (json_object_object_get_ex(profile, "command", &profile1)) { syslog(LOG_NOTICE, "profile command"); - + if (unit->mode == UNITMODE_PROFILE) { + char *cmd = xstrcpy((char *)json_object_get_string(profile1)); + if (! strcmp(cmd, (char *)"off")) { + if (unit->prof_state == PROFILE_DONE) { + unit->prof_state = PROFILE_OFF; + syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile to OFF", message_alias); + unit->mqtt_flag |= MQTT_FLAG_DATA; + } + } else if (! strcmp(cmd, (char *)"pause")) { + if (unit->prof_state == PROFILE_RUN) { + unit->prof_state = PROFILE_PAUSE; + syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile to PAUSE", message_alias); + unit->mqtt_flag |= MQTT_FLAG_DATA; + } else if (unit->prof_state == PROFILE_PAUSE) { + unit->prof_state = PROFILE_RUN; + syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile resume RUN", message_alias); + unit->mqtt_flag |= MQTT_FLAG_DATA; + } + } else if (! strcmp(cmd, (char *)"start")) { + if (unit->prof_state == PROFILE_OFF) { + unit->prof_state = PROFILE_RUN; + unit->prof_started = time(NULL); + unit->prof_paused = unit->prof_primary_done = 0; + unit->prof_peak_abs = unit->prof_peak_rel = 0.0; + syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile start RUN", message_alias); + unit->mqtt_flag |= MQTT_FLAG_DATA; + } + } else if (! strcmp(cmd, (char *)"abort")) { + if ((unit->prof_state == PROFILE_RUN) || (unit->prof_state == PROFILE_PAUSE)) { + unit->prof_state = PROFILE_OFF; + unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0; + unit->prof_peak_abs = unit->prof_peak_rel = 0.0; + syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile ABORT", message_alias); + unit->mqtt_flag |= MQTT_FLAG_DATA; + } + } else if (! strcmp(cmd, (char *)"done")) { + if (unit->prof_state == PROFILE_DONE) { + unit->prof_state = PROFILE_OFF; + unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0; + unit->prof_peak_abs = unit->prof_peak_rel = 0.0; + syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile OFF", message_alias); + unit->mqtt_flag |= MQTT_FLAG_DATA; + } + } + free(cmd); + cmd = NULL; + } } else if (json_object_object_get_ex(profile, "uuid", &profile1)) { // syslog(LOG_NOTICE, "profile new profile"); if ((unit->prof_state == PROFILE_OFF) || (unit->prof_state == PROFILE_DONE) || (unit->prof_state == PROFILE_ABORT)) {