thermferm/mqtt.c

changeset 533
49580ca85ab7
parent 518
fd36bedab944
child 534
92b546d4a839
equal deleted inserted replaced
532:34bf9f389749 533:49580ca85ab7
1 /***************************************************************************** 1 /*****************************************************************************
2 * Copyright (C) 2016-2017 2 * Copyright (C) 2016-2018
3 * 3 *
4 * Michiel Broek <mbroek at mbse dot eu> 4 * Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * This file is part of the mbsePi-apps 6 * This file is part of the mbsePi-apps
7 * 7 *
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 }
548 #endif 603 #endif
549 } 604 }
550 605
551 606
552 607
553 void publishBirth(void)
554 {
555 publishNData(true, 0);
556 publishDBirth();
557 }
558
559
560
561
562 void mqtt_connect(void) 608 void mqtt_connect(void)
563 { 609 {
564 #ifdef HAVE_MOSQUITTO_H 610 #ifdef HAVE_MOSQUITTO_H
565 char *id = NULL; 611 char *id = NULL;
566 char err[1024]; 612 char err[1024];
628 674
629 /* 675 /*
630 * Initialise is complete, report our presence state 676 * Initialise is complete, report our presence state
631 */ 677 */
632 mosquitto_loop_start(mosq); 678 mosquitto_loop_start(mosq);
633 publishBirth(); 679 publishNData(true, 0);
680 publishDBirthAll();
634 } 681 }
635 #endif 682 #endif
636 } 683 }
637 684
638 685
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 {

mercurial