Added co2meters to the websockets. Added node and units offline messages to the websockets.

Tue, 12 May 2020 13:16:25 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 12 May 2020 13:16:25 +0200
changeset 673
9924b1218d39
parent 672
23f959713fcb
child 674
c865e3cdee04

Added co2meters to the websockets. Added node and units offline messages to the websockets.

bmsd/Makefile file | annotate | diff | comparison | revisions
bmsd/co2meters.c 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 22:09:41 2020 +0200
+++ b/bmsd/Makefile	Tue May 12 13:16:25 2020 +0200
@@ -60,7 +60,7 @@
 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
+co2meters.o: bms.h xutil.h co2meters.h mysql.h websocket.h
 ispindels.o: bms.h xutil.h ispindels.h mysql.h nodes.h
 bms.o: bms.h xutil.h futil.h rdconfig.h lock.h mqtt.h mysql.h nodes.h websocket.h
 xutil.o: bms.h xutil.h
--- a/bmsd/co2meters.c	Mon May 11 22:09:41 2020 +0200
+++ b/bmsd/co2meters.c	Tue May 12 13:16:25 2020 +0200
@@ -3,7 +3,7 @@
  * @brief Handle co2meters status
  * @author Michiel Broek <mbroek at mbse dot eu>
  *
- * Copyright (C) 2019
+ * Copyright (C) 2019-2020
  *
  * This file is part of the bms (Brewery Management System)
  *
@@ -27,6 +27,7 @@
 #include "xutil.h"
 #include "co2meters.h"
 #include "mysql.h"
+#include "websocket.h"
 
 
 sys_co2meter_list	*co2meters = NULL;
@@ -44,6 +45,7 @@
     struct json_object	*jobj, *val, *sensor;
     sys_co2meter_list	*co2meter, *tmpp;
     bool		new_co2meter = true;
+    char		*msg = NULL, buf[65];
 
 //    fprintf(stdout, "co2meter_set: %s/%s %s\n", edge_node, alias, payload);
 
@@ -137,6 +139,37 @@
     }
     json_object_put(jobj);
 
+    msg = xstrcpy((char *)"{\"device\":\"co2meter\",\"node\":\"");
+    msg = xstrcat(msg, edge_node);
+    msg = xstrcat(msg, (char *)"\",\"unit\":\"");
+    msg = xstrcat(msg, alias);
+    msg = xstrcat(msg, (char *)"\",\"online\":");
+    msg = xstrcat(msg, co2meter->online ? (char *)"1":(char *)"0");
+    msg = xstrcat(msg, (char *)",\"mode\":\"");
+    msg = xstrcat(msg, co2meter->mode);
+    msg = xstrcat(msg, (char *)"\",\"temperature\":");
+    snprintf(buf, 64, "%.3f", co2meter->temperature);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"pressure_channel\":");
+    snprintf(buf, 64, "%d", co2meter->pressure_channel);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"pressure_voltage\":");
+    snprintf(buf, 64, "%.3f", co2meter->pressure_voltage);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"pressure_zero\":");
+    snprintf(buf, 64, "%.3f", co2meter->pressure_zero);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"pressure_bar\":");
+    snprintf(buf, 64, "%.3f", co2meter->pressure_bar);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"alarm\":");
+    snprintf(buf, 64, "%d", co2meter->alarm);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)"}");
+    ws_broadcast(msg);
+    free(msg);
+    msg = NULL;
+
 //    co2meter_dump(co2meter);
 
     if (new_co2meter) {
--- a/bmsd/nodes.c	Mon May 11 22:09:41 2020 +0200
+++ b/bmsd/nodes.c	Tue May 12 13:16:25 2020 +0200
@@ -317,6 +317,14 @@
                     	syslog(LOG_NOTICE, "Timeout fermenter %s/%s", tmpf->node, tmpf->alias);
                 	tmpf->online = false;
 			fermenter_mysql_death(tmpf->node, tmpf->alias);
+			msg = xstrcpy((char *)"{\"device\":\"fermenter\",\"node\":\"");
+            		msg = xstrcat(msg, tmpf->node);
+			msg = xstrcat(msg, (char *)"\",\"unit\":\"");
+    			msg = xstrcat(msg, tmpf->alias);
+            		msg = xstrcat(msg, (char *)"\",\"online\":0}");
+            		ws_broadcast(msg);
+            		free(msg);
+            		msg = NULL;
 		    }
             	}
             }
@@ -327,6 +335,14 @@
 			syslog(LOG_NOTICE, "Timeout co2meter %s/%s", tmpc->node, tmpc->alias);
 			tmpc->online = false;
 			co2meter_mysql_death(tmpc->node, tmpc->alias);
+			msg = xstrcpy((char *)"{\"device\":\"co2meter\",\"node\":\"");
+                        msg = xstrcat(msg, tmpc->node);
+                        msg = xstrcat(msg, (char *)"\",\"unit\":\"");
+                        msg = xstrcat(msg, tmpc->alias);
+                        msg = xstrcat(msg, (char *)"\",\"online\":0}");
+                        ws_broadcast(msg);
+                        free(msg);
+                        msg = NULL;
 		    }
 		}
 	    }
@@ -337,6 +353,14 @@
                         syslog(LOG_NOTICE, "Timeout ispindel %s", tmpi->node);
                         tmpi->online = false;
                         ispindel_mysql_death(tmpi->node);
+			msg = xstrcpy((char *)"{\"device\":\"ispindel\",\"node\":\"");
+                        msg = xstrcat(msg, tmpi->node);
+                        msg = xstrcat(msg, (char *)"\",\"unit\":\"");
+                        msg = xstrcat(msg, tmpi->alias);
+                        msg = xstrcat(msg, (char *)"\",\"online\":0}");
+                        ws_broadcast(msg);
+                        free(msg);
+                        msg = NULL;
                     }
 		}
 	    }
--- a/bmsd/websocket.c	Mon May 11 22:09:41 2020 +0200
+++ b/bmsd/websocket.c	Tue May 12 13:16:25 2020 +0200
@@ -158,8 +158,10 @@
 
 
 /*
- *  {"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"}
+ * {"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"}
+ * {"device":"co2meter","node":"seaport","unit":"unit0","online":1,"mode":"ON","temperature":20.875,"pressure_channel":6,"pressure_voltage":0.834,"pressure_zero":0.110,"pressure_bar":2.3,"alarm":0}
+ * {"device":"ispindel","node":"seaport","unit":"unit0","online":1,"mode":"ON","temperature":20.875,"tilt":45.223,"battery":4.121,"plato":14.832,"alarm":0}
  */
 void ws_broadcast(char *msg)
 {

mercurial