417 return payload; |
417 return payload; |
418 } |
418 } |
419 |
419 |
420 |
420 |
421 |
421 |
422 void publishDBirth(void) |
422 /** |
|
423 * @brief Publish DBIRTH for all active units. If there are no active units, don't |
|
424 * publish anything. This function should be called at program start. |
|
425 */ |
|
426 void publishDBirthAll(void) |
423 { |
427 { |
424 char *payload = NULL; |
428 char *payload = NULL; |
425 units_list *unit; |
429 units_list *unit; |
426 int comma = FALSE; |
430 int comma = FALSE; |
427 |
431 |
428 payload = payload_header(); |
432 payload = payload_header(); |
429 payload = xstrcat(payload, (char *)"{\"units\":["); |
433 payload = xstrcat(payload, (char *)"{\"units\":["); |
430 for (unit = Config.units; unit; unit = unit->next) { |
434 for (unit = Config.units; unit; unit = unit->next) { |
431 if (comma) |
435 if (unit->mode != UNITMODE_OFF) { |
432 payload = xstrcat(payload, (char *)","); |
436 if (comma) |
433 payload = xstrcat(payload, unit_data(unit, true)); |
437 payload = xstrcat(payload, (char *)","); |
434 comma = TRUE; |
438 payload = xstrcat(payload, unit_data(unit, true)); |
435 } |
439 comma = TRUE; |
436 payload = xstrcat(payload, (char *)"]}}"); |
440 } |
437 publisher(mosq, topic_base((char *)"DBIRTH"), payload, true); |
441 } |
|
442 if (comma) { // Only publish if there is at least one unit active. |
|
443 payload = xstrcat(payload, (char *)"]}}"); |
|
444 publisher(mosq, topic_base((char *)"DBIRTH"), payload, true); |
|
445 } |
438 free(payload); |
446 free(payload); |
439 payload = NULL; |
447 payload = NULL; |
440 } |
448 } |
441 |
449 |
442 #endif |
450 #endif |
455 topic = xstrcat(topic_base((char *)"DDATA"), (char *)"/"); |
463 topic = xstrcat(topic_base((char *)"DDATA"), (char *)"/"); |
456 topic = xstrcat(topic, unit->alias); |
464 topic = xstrcat(topic, unit->alias); |
457 publisher(mosq, topic, payload, false); |
465 publisher(mosq, topic, payload, false); |
458 free(payload); |
466 free(payload); |
459 payload = NULL; |
467 payload = NULL; |
|
468 free(topic); |
|
469 topic = NULL; |
|
470 } |
|
471 #endif |
|
472 } |
|
473 |
|
474 |
|
475 |
|
476 void publishDBirth(units_list *unit) |
|
477 { |
|
478 #ifdef HAVE_MOSQUITTO_H |
|
479 |
|
480 char *payload = NULL, *topic = NULL; |
|
481 |
|
482 if (mqtt_use) { |
|
483 payload = payload_header(); |
|
484 payload = xstrcat(payload, unit_data(unit, true)); |
|
485 payload = xstrcat(payload, (char *)"}"); |
|
486 topic = xstrcat(topic_base((char *)"DBIRTH"), (char *)"/"); |
|
487 topic = xstrcat(topic, unit->alias); |
|
488 publisher(mosq, topic, payload, true); |
|
489 free(payload); |
|
490 payload = NULL; |
|
491 free(topic); |
|
492 topic = NULL; |
|
493 } |
|
494 #endif |
|
495 } |
|
496 |
|
497 |
|
498 |
|
499 void publishDDeath(units_list *unit) |
|
500 { |
|
501 #ifdef HAVE_MOSQUITTO_H |
|
502 |
|
503 char *topic = NULL; |
|
504 |
|
505 if (mqtt_use) { |
|
506 // First delete presistent DBIRTH topic |
|
507 topic = xstrcat(topic_base((char *)"DBIRTH"), (char *)"/"); |
|
508 topic = xstrcat(topic, unit->alias); |
|
509 publisher(mosq, topic, NULL, true); |
|
510 free(topic); |
|
511 topic = NULL; |
|
512 topic = xstrcat(topic_base((char *)"DDEATH"), (char *)"/"); |
|
513 topic = xstrcat(topic, unit->alias); |
|
514 publisher(mosq, topic, NULL, true); |
460 free(topic); |
515 free(topic); |
461 topic = NULL; |
516 topic = NULL; |
462 } |
517 } |
463 #endif |
518 #endif |
464 } |
519 } |
646 /* |
693 /* |
647 * Final publish 0 to clients/<hostname>/thermferm/state |
694 * Final publish 0 to clients/<hostname>/thermferm/state |
648 * After that, remove the retained topic. |
695 * After that, remove the retained topic. |
649 */ |
696 */ |
650 syslog(LOG_NOTICE, "MQTT disconnecting"); |
697 syslog(LOG_NOTICE, "MQTT disconnecting"); |
651 publisher(mosq, topic_base((char *)"DBIRTH"), NULL, true); |
|
652 publisher(mosq, topic_base((char *)"NBIRTH"), NULL, true); |
698 publisher(mosq, topic_base((char *)"NBIRTH"), NULL, true); |
|
699 publisher(mosq, topic_base((char *)"NDEATH"), NULL, true); |
653 mqtt_last_mid = mqtt_mid_sent; |
700 mqtt_last_mid = mqtt_mid_sent; |
654 mqtt_status = STATUS_WAITING; |
701 mqtt_status = STATUS_WAITING; |
655 mqtt_my_shutdown = TRUE; |
702 mqtt_my_shutdown = TRUE; |
656 |
703 |
657 do { |
704 do { |