228 } |
228 } |
229 } |
229 } |
230 } |
230 } |
231 |
231 |
232 if (json_object_object_get_ex(metric, "mode", &val)) { |
232 if (json_object_object_get_ex(metric, "mode", &val)) { |
233 for (int i = 0; i < 4; i++) { |
233 for (int i = 0; i < 5; i++) { |
234 if (strcmp((char *)json_object_get_string(val), UNITMODE[i]) == 0) { |
234 if (strcmp((char *)json_object_get_string(val), UNITMODE[i]) == 0) { |
235 if (unit->mode != i) { |
235 if (unit->mode != i) { |
236 syslog(LOG_NOTICE, "DCMD change fermenter %s: mode to %s", message_alias, UNITMODE[i]); |
|
237 unit->mqtt_flag |= MQTT_FLAG_DATA; |
236 unit->mqtt_flag |= MQTT_FLAG_DATA; |
238 /* Initialize log if the unit is turned on */ |
237 /* Initialize log if the unit is turned on */ |
239 if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) { |
238 if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) { |
240 initlog(unit->product_code, unit->product_name); |
239 initlog(unit->product_code, unit->product_name); |
241 unit->mqtt_flag |= MQTT_FLAG_BIRTH; |
240 unit->mqtt_flag |= MQTT_FLAG_BIRTH; |
242 } else if ((unit->mode != UNITMODE_OFF) && (i == UNITMODE_OFF)) { |
241 } else if ((unit->mode != UNITMODE_OFF) && (i == UNITMODE_OFF)) { |
243 unit->mqtt_flag |= MQTT_FLAG_DEATH; |
242 unit->mqtt_flag |= MQTT_FLAG_DEATH; |
244 } |
243 } |
245 syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]); |
244 if (i == UNITMODE_PROFILE) { |
|
245 /* Do some checks and refuse profile mode cannot be set */ |
|
246 if (unit->profile_uuid == NULL) { |
|
247 syslog(LOG_NOTICE, "Fermenter unit %s refuse profile, not loaded", message_alias); |
|
248 break; |
|
249 } |
|
250 } |
|
251 syslog(LOG_NOTICE, "DCMD change fermenter %s: mode to %s", message_alias, UNITMODE[i]); |
246 unit->mode = i; |
252 unit->mode = i; |
247 /* Allways turn everything off after a mode change */ |
253 /* Allways turn everything off after a mode change */ |
248 unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0; |
254 unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0; |
249 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE; |
255 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE; |
250 unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0; |
256 unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0; |
347 unit->product_name = xstrcpy((char *)json_object_get_string(val)); |
354 unit->product_name = xstrcpy((char *)json_object_get_string(val)); |
348 unit->mqtt_flag |= MQTT_FLAG_DATA; |
355 unit->mqtt_flag |= MQTT_FLAG_DATA; |
349 syslog(LOG_NOTICE, "DCMD change fermenter %s: product_name to `%s'", message_alias, unit->product_name); |
356 syslog(LOG_NOTICE, "DCMD change fermenter %s: product_name to `%s'", message_alias, unit->product_name); |
350 } |
357 } |
351 } |
358 } |
|
359 if (json_object_object_get_ex(setpoint, "uuid", &val)) { |
|
360 if (strcmp((char *)json_object_get_string(val), unit->product_uuid)) { |
|
361 free(unit->product_uuid); |
|
362 unit->product_uuid = xstrcpy((char *)json_object_get_string(val)); |
|
363 unit->mqtt_flag |= MQTT_FLAG_DATA; |
|
364 syslog(LOG_NOTICE, "DCMD change fermenter %s: product_uuid to `%s'", message_alias, unit->product_uuid); |
|
365 } |
|
366 } |
352 } |
367 } |
353 |
368 |
354 if (json_object_object_get_ex(metric, "profile", &profile)) { |
369 if (json_object_object_get_ex(metric, "profile", &profile)) { |
355 if (json_object_object_get_ex(profile, "command", &profile1)) { |
370 if (json_object_object_get_ex(profile, "command", &profile1)) { |
356 syslog(LOG_NOTICE, "profile command"); |
371 syslog(LOG_NOTICE, "profile command"); |
357 |
372 if (unit->mode == UNITMODE_PROFILE) { |
|
373 char *cmd = xstrcpy((char *)json_object_get_string(profile1)); |
|
374 if (! strcmp(cmd, (char *)"off")) { |
|
375 if (unit->prof_state == PROFILE_DONE) { |
|
376 unit->prof_state = PROFILE_OFF; |
|
377 syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile to OFF", message_alias); |
|
378 unit->mqtt_flag |= MQTT_FLAG_DATA; |
|
379 } |
|
380 } else if (! strcmp(cmd, (char *)"pause")) { |
|
381 if (unit->prof_state == PROFILE_RUN) { |
|
382 unit->prof_state = PROFILE_PAUSE; |
|
383 syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile to PAUSE", message_alias); |
|
384 unit->mqtt_flag |= MQTT_FLAG_DATA; |
|
385 } else if (unit->prof_state == PROFILE_PAUSE) { |
|
386 unit->prof_state = PROFILE_RUN; |
|
387 syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile resume RUN", message_alias); |
|
388 unit->mqtt_flag |= MQTT_FLAG_DATA; |
|
389 } |
|
390 } else if (! strcmp(cmd, (char *)"start")) { |
|
391 if (unit->prof_state == PROFILE_OFF) { |
|
392 unit->prof_state = PROFILE_RUN; |
|
393 unit->prof_started = time(NULL); |
|
394 unit->prof_paused = unit->prof_primary_done = 0; |
|
395 unit->prof_peak_abs = unit->prof_peak_rel = 0.0; |
|
396 syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile start RUN", message_alias); |
|
397 unit->mqtt_flag |= MQTT_FLAG_DATA; |
|
398 } |
|
399 } else if (! strcmp(cmd, (char *)"abort")) { |
|
400 if ((unit->prof_state == PROFILE_RUN) || (unit->prof_state == PROFILE_PAUSE)) { |
|
401 unit->prof_state = PROFILE_OFF; |
|
402 unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0; |
|
403 unit->prof_peak_abs = unit->prof_peak_rel = 0.0; |
|
404 syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile ABORT", message_alias); |
|
405 unit->mqtt_flag |= MQTT_FLAG_DATA; |
|
406 } |
|
407 } else if (! strcmp(cmd, (char *)"done")) { |
|
408 if (unit->prof_state == PROFILE_DONE) { |
|
409 unit->prof_state = PROFILE_OFF; |
|
410 unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0; |
|
411 unit->prof_peak_abs = unit->prof_peak_rel = 0.0; |
|
412 syslog(LOG_NOTICE, "DCMD change fermenter `%s' profile OFF", message_alias); |
|
413 unit->mqtt_flag |= MQTT_FLAG_DATA; |
|
414 } |
|
415 } |
|
416 free(cmd); |
|
417 cmd = NULL; |
|
418 } |
358 } else if (json_object_object_get_ex(profile, "uuid", &profile1)) { |
419 } else if (json_object_object_get_ex(profile, "uuid", &profile1)) { |
359 // syslog(LOG_NOTICE, "profile new profile"); |
420 // syslog(LOG_NOTICE, "profile new profile"); |
360 if ((unit->prof_state == PROFILE_OFF) || (unit->prof_state == PROFILE_DONE) || (unit->prof_state == PROFILE_ABORT)) { |
421 if ((unit->prof_state == PROFILE_OFF) || (unit->prof_state == PROFILE_DONE) || (unit->prof_state == PROFILE_ABORT)) { |
361 if (unit->profile_uuid) |
422 if (unit->profile_uuid) |
362 free(unit->profile_uuid); |
423 free(unit->profile_uuid); |