diff -r 4b54d6f79d25 -r 23f959713fcb bmsd/nodes.c --- a/bmsd/nodes.c Mon May 11 17:32:08 2020 +0200 +++ b/bmsd/nodes.c Mon May 11 22:09:41 2020 +0200 @@ -27,6 +27,7 @@ #include "xutil.h" #include "nodes.h" #include "mysql.h" +#include "websocket.h" sys_node_list *nodes = NULL; @@ -41,7 +42,8 @@ { struct json_object *jobj, *val, *metric, *metric2; sys_node_list *node, *tmpp; - char *group_id, *message_type, *edge_node; + struct tm *mytime; + char *group_id, *message_type, *edge_node, *msg = NULL, buf[74]; bool new_node = true; // fprintf(stdout, "node_birth: %s %s\n", topic, payload); @@ -100,12 +102,6 @@ } } - /* - if (json_object_object_get_ex(jobj, "seq", &val)) { - printf("seq: %s\n", json_object_to_json_string_ext(val, 0)); // Do we need it? - } - */ - if (json_object_object_get_ex(jobj, "metric", &metric)) { if (json_object_object_get_ex(metric, "uuid", &val)) { if (node->uuid) @@ -182,6 +178,41 @@ } json_object_put(jobj); + msg = xstrcpy((char *)"{\"node\":\""); + msg = xstrcat(msg, edge_node); + msg = xstrcat(msg, (char *)"\",\"group\":\""); + msg = xstrcat(msg, group_id); + msg = xstrcat(msg, (char *)"\",\"online\":"); + msg = xstrcat(msg, node->online ? (char *)"1":(char *)"0"); + msg = xstrcat(msg, (char *)",\"lastseen\":\""); + mytime = localtime(&node->lastseen); + snprintf(buf, 73, "%04d-%02d-%02d %02d:%02d:%02d", + mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); + msg = xstrcat(msg, buf); + msg = xstrcat(msg, (char *)"\""); + if (node->temperature) { + msg = xstrcat(msg, (char *)",\"temperature\":"); + snprintf(buf, 64, "%.1f", node->temperature); + msg = xstrcat(msg, buf); + } + if (node->humidity) { + msg = xstrcat(msg, (char *)",\"humidity\":"); + snprintf(buf, 64, "%.1f", node->humidity); + msg = xstrcat(msg, buf); + } + msg = xstrcat(msg, (char *)",\"ip\":\""); + msg = xstrcat(msg, node->net_address); + msg = xstrcat(msg, (char *)"\""); + if (node->net_rssi != 0) { + msg = xstrcat(msg, (char *)",\"rssi\":"); + snprintf(buf, 64, "%d", node->net_rssi); + msg = xstrcat(msg, buf); + } + msg = xstrcat(msg, (char *)"}"); + ws_broadcast(msg); + free(msg); + msg = NULL; + // node_dump(node); if (new_node) { @@ -226,7 +257,7 @@ void node_death(char *topic) { - char *group_id, *edge_node; + char *group_id, *edge_node, *msg; sys_node_list *tmpp; strtok(topic, "/"); // ignore namespace @@ -240,6 +271,14 @@ for (tmpp = nodes; tmpp; tmpp = tmpp->next) { if (strcmp(tmpp->node, edge_node) == 0) { tmpp->online = false; + msg = xstrcpy((char *)"{\"node\":\""); + msg = xstrcat(msg, edge_node); + msg = xstrcat(msg, (char *)"\",\"group\":\""); + msg = xstrcat(msg, group_id); + msg = xstrcat(msg, (char *)"\",\"online\":0}"); + ws_broadcast(msg); + free(msg); + msg = NULL; break; } } @@ -254,6 +293,7 @@ sys_co2meter_list *tmpc; sys_ispindel_list *tmpi; time_t now = time(NULL); + char *msg = NULL; for (tmpn = nodes; tmpn; tmpn = tmpn->next) { // if (debug) @@ -262,6 +302,14 @@ syslog(LOG_NOTICE, "Timeout node `%s/%s' after %ld seconds", tmpn->group_id, tmpn->node, (now - tmpn->lastseen)); tmpn->online = false; node_mysql_death(tmpn->node); + msg = xstrcpy((char *)"{\"node\":\""); + msg = xstrcat(msg, tmpn->node); + msg = xstrcat(msg, (char *)"\",\"group\":\""); + msg = xstrcat(msg, tmpn->group_id); + msg = xstrcat(msg, (char *)"\",\"online\":0}"); + ws_broadcast(msg); + free(msg); + msg = NULL; for (tmpf = fermenters; tmpf; tmpf = tmpf->next) { if (strcmp(tmpf->node, tmpn->node) == 0) {