Added websockets for node status changes.

Mon, 11 May 2020 22:09:41 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 11 May 2020 22:09:41 +0200
changeset 672
23f959713fcb
parent 671
4b54d6f79d25
child 673
9924b1218d39

Added websockets for node status changes.

bmsd/Makefile file | annotate | diff | comparison | revisions
bmsd/nodes.c file | annotate | diff | comparison | revisions
bmsd/websocket.c file | annotate | diff | comparison | revisions
--- a/bmsd/Makefile	Mon May 11 17:32:08 2020 +0200
+++ b/bmsd/Makefile	Mon May 11 22:09:41 2020 +0200
@@ -57,7 +57,7 @@
 # Dependencies generated by make depend
 mqtt.o: bms.h xutil.h mqtt.h nodes.h fermenters.h co2meters.h ispindels.h
 lock.o: lock.h bms.h futil.h
-nodes.o: bms.h xutil.h nodes.h mysql.h
+nodes.o: bms.h xutil.h nodes.h mysql.h websocket.h
 futil.o: bms.h futil.h
 fermenters.o: bms.h xutil.h fermenters.h mysql.h websocket.h
 co2meters.o: bms.h xutil.h co2meters.h mysql.h
--- 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) {
--- a/bmsd/websocket.c	Mon May 11 17:32:08 2020 +0200
+++ b/bmsd/websocket.c	Mon May 11 22:09:41 2020 +0200
@@ -158,6 +158,7 @@
 
 
 /*
+ *  {"node":"host","group":"group","online":1,"lastseen":"datetime","temperature":20.5,"humidity":47,"ip":"ipaddr","rssi":-1}
  *  {"device":"fermenter","node":"seaport","unit":"unit0","online":1,"mode":"FRIDGE","yeast_lo":12.0,"yeast_hi":24.0,"air":19.875,"beer":19.812,"chiller":1.500,"heater":100,"cooler":0,"fan":100,"light":0,"door":0,"sp_lo":17.0,"sp_hi":17.5,"alarm":0,"stage":"PRIMARY"}
  */
 void ws_broadcast(char *msg)

mercurial