Report any online status change via websocket broadcast. When starting, load all in memory tables sorted. The websocket status on the web pages has it's own panel. Prepared the menu system for dynamic updates.

Wed, 20 May 2020 21:49:09 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 20 May 2020 21:49:09 +0200
changeset 684
ccb9f24d0fe9
parent 683
420bf5caf53e
child 685
a4fcdf24b5e7

Report any online status change via websocket broadcast. When starting, load all in memory tables sorted. The websocket status on the web pages has it's own panel. Prepared the menu system for dynamic updates.

bmsd/Makefile file | annotate | diff | comparison | revisions
bmsd/co2meters.c file | annotate | diff | comparison | revisions
bmsd/fermenters.c file | annotate | diff | comparison | revisions
bmsd/ispindels.c file | annotate | diff | comparison | revisions
bmsd/mysql.c file | annotate | diff | comparison | revisions
bmsd/nodes.c file | annotate | diff | comparison | revisions
bmsd/nodes.h file | annotate | diff | comparison | revisions
www/includes/global.inc.php file | annotate | diff | comparison | revisions
www/js/global.js file | annotate | diff | comparison | revisions
--- a/bmsd/Makefile	Tue May 19 22:48:59 2020 +0200
+++ b/bmsd/Makefile	Wed May 20 21:49:09 2020 +0200
@@ -59,8 +59,8 @@
 lock.o: lock.h bms.h futil.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 mqtt.h
-co2meters.o: bms.h xutil.h co2meters.h mysql.h websocket.h
+fermenters.o: bms.h xutil.h fermenters.h mysql.h nodes.h websocket.h mqtt.h
+co2meters.o: bms.h xutil.h co2meters.h mysql.h nodes.h websocket.h
 ispindels.o: bms.h xutil.h ispindels.h mysql.h nodes.h websocket.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	Tue May 19 22:48:59 2020 +0200
+++ b/bmsd/co2meters.c	Wed May 20 21:49:09 2020 +0200
@@ -28,6 +28,7 @@
 #include "co2meters.h"
 #include "mysql.h"
 #include "websocket.h"
+#include "nodes.h"
 
 
 sys_co2meter_list	*co2meters = NULL;
@@ -187,7 +188,7 @@
 {
     struct json_object	*jobj, *val, *sensor;
     sys_co2meter_list	*co2meter, *tmpp;
-    bool		new_co2meter = true;
+    bool		new_co2meter = true, report = false;
 
 //    fprintf(stdout, "co2meter_set: %s/%s %s\n", edge_node, alias, payload);
 
@@ -219,6 +220,7 @@
 
     if (! co2meter->online) {
     	co2meter->online = true;
+	report = true;
     	syslog(LOG_NOTICE, "Online co2meter %s/%s mode %s", edge_node, alias, co2meter->mode);
     }
 
@@ -301,6 +303,8 @@
 	co2meter_mysql_update(co2meter);
     }
 
+    if (report)
+	status_change();
 }
 
 
--- a/bmsd/fermenters.c	Tue May 19 22:48:59 2020 +0200
+++ b/bmsd/fermenters.c	Wed May 20 21:49:09 2020 +0200
@@ -29,6 +29,7 @@
 #include "mysql.h"
 #include "mqtt.h"
 #include "websocket.h"
+#include "nodes.h"
 
 
 sys_fermenter_list	*fermenters = NULL;
@@ -721,7 +722,7 @@
     } else {
 	fermenter_mysql_update(fermenter);
     }
-
+    status_change();
 }
 
 
@@ -1059,6 +1060,7 @@
 {
     char		*edge_node, *alias, *msg = NULL;
     sys_fermenter_list	*tmpp;
+    bool		report = false;
 
     printf("fermenter_death: %s\n", topic);
     strtok(topic, "/"); // ignore namespace
@@ -1083,6 +1085,7 @@
 		    ws_broadcast(msg);
     		    free(msg);
     		    msg = NULL;
+		    report = true;
 		}
 	    	break;
 	    }
@@ -1101,10 +1104,13 @@
                     ws_broadcast(msg);
                     free(msg);
                     msg = NULL;
+		    report = true;
 		}
 	    }
 	}
     }
+    if (report)
+	status_change();
 }
 
 
--- a/bmsd/ispindels.c	Tue May 19 22:48:59 2020 +0200
+++ b/bmsd/ispindels.c	Wed May 20 21:49:09 2020 +0200
@@ -202,7 +202,7 @@
 {
     sys_ispindel_list	*ispindel, *tmpp;
     struct json_object  *jobj, *metric, *val;
-    bool		new_ispindel = true;
+    bool		new_ispindel = true, report = false;
     char		*datetime, buf[65], *line, *logfile;
     struct tm           *mytime;
     time_t              timestamp;
@@ -238,6 +238,7 @@
 
     if (! ispindel->online) {
     	ispindel->online = true;
+	report = true;
     	syslog(LOG_NOTICE, "Online ispindel %s mode %s", node, ispindel->mode);
     }
 
@@ -294,6 +295,8 @@
 	ispindel_mysql_update(ispindel);
     }
     ispindel_ws_send(ispindel);
+    if (report)
+	status_change();
 
     /*
      * The data is complete, see if we can write a log entry.
--- a/bmsd/mysql.c	Tue May 19 22:48:59 2020 +0200
+++ b/bmsd/mysql.c	Wed May 20 21:49:09 2020 +0200
@@ -85,7 +85,7 @@
     /*
      * Restore nodes from the database
      */
-    if (mysql_query(con, "SELECT * FROM mon_nodes")) {
+    if (mysql_query(con, "SELECT * FROM mon_nodes ORDER BY node")) {
 	syslog(LOG_NOTICE, "MySQL: SELECT * FROM mon_nodes error %u (%s))", mysql_errno(con), mysql_error(con));
     } else {
 	res_set = mysql_store_result(con);
@@ -135,7 +135,7 @@
 	}
     }
 
-    if (mysql_query(con, "SELECT * FROM mon_fermenters")) {
+    if (mysql_query(con, "SELECT * FROM mon_fermenters ORDER BY node,alias")) {
 	syslog(LOG_NOTICE, "MySQL: SELECT * FROM mon_fermenters error %u (%s))", mysql_errno(con), mysql_error(con));
     } else {
 	res_set = mysql_store_result(con);
@@ -231,7 +231,7 @@
 	}
     }
 
-    if (mysql_query(con, "SELECT * FROM mon_co2meters")) {
+    if (mysql_query(con, "SELECT * FROM mon_co2meters ORDER BY node,alias")) {
         syslog(LOG_NOTICE, "MySQL: SELECT * FROM mon_co2meters error %u (%s))", mysql_errno(con), mysql_error(con));
     } else {
         res_set = mysql_store_result(con);
@@ -275,7 +275,7 @@
 	}
     }
 
-    if (mysql_query(con, "SELECT * FROM mon_ispindels")) {
+    if (mysql_query(con, "SELECT * FROM mon_ispindels ORDER BY node,alias")) {
         syslog(LOG_NOTICE, "MySQL: SELECT * FROM mon_ispindels error %u (%s))", mysql_errno(con), mysql_error(con));
     } else {
         res_set = mysql_store_result(con);
--- a/bmsd/nodes.c	Tue May 19 22:48:59 2020 +0200
+++ b/bmsd/nodes.c	Wed May 20 21:49:09 2020 +0200
@@ -294,12 +294,14 @@
     sys_ispindel_list	*tmpi;
     time_t		now = time(NULL);
     char		*msg = NULL;
+    bool		report = false;
 
     for (tmpn = nodes; tmpn; tmpn = tmpn->next) {
 //	if (debug)
 //	    printf("%-20s online %s  %ld  %d\n", tmpn->node, tmpn->online ? "yes":"no ", tmpn->lastseen, tmpn->interval);
 	if (tmpn->online && ((now - tmpn->lastseen) > (tmpn->interval * 2 + 5))) { // 2 times interval + 5 seconds
 	    syslog(LOG_NOTICE, "Timeout node `%s/%s' after %ld seconds", tmpn->group_id, tmpn->node, (now - tmpn->lastseen));
+	    report = true;
 	    tmpn->online = false;
 	    node_mysql_death(tmpn->node);
 	    msg = xstrcpy((char *)"{\"node\":\"");
@@ -370,6 +372,93 @@
 	    }
 	}
     }
+
+    if (report)
+	status_change();
 }
 
 
+
+void status_change(void)
+{
+    sys_node_list       *tmpn;
+    sys_fermenter_list  *tmpf;
+    sys_co2meter_list   *tmpc;
+    sys_ispindel_list   *tmpi;
+    char                *msg = NULL;
+    bool                comma = false;
+
+    msg = xstrcpy((char *)"{\"status\":{\"nodes\":[");
+
+    for (tmpn = nodes; tmpn; tmpn = tmpn->next) {
+	if (comma)
+	    msg = xstrcat(msg, (char *)",");
+	comma = true;
+	msg = xstrcat(msg, (char *)"{\"uuid\":\"");
+	msg = xstrcat(msg, tmpn->uuid);
+	msg = xstrcat(msg, (char *)"\",\"node\":\"");
+	msg = xstrcat(msg, tmpn->node);
+	msg = xstrcat(msg, (char *)"\",\"online\":");
+	msg = xstrcat(msg, (tmpn->online) ? (char *)"1":(char *)"0");
+	msg = xstrcat(msg, (char *)"}");
+    }
+
+    msg = xstrcat(msg, (char *)"],\"fermenters\":[");
+    comma = false;
+    for (tmpf = fermenters; tmpf; tmpf = tmpf->next) {
+	if (comma)
+            msg = xstrcat(msg, (char *)",");
+        comma = true;
+	msg = xstrcat(msg, (char *)"{\"uuid\":\"");
+        msg = xstrcat(msg, tmpf->uuid);
+        msg = xstrcat(msg, (char *)"\",\"node\":\"");
+        msg = xstrcat(msg, tmpf->node);
+        msg = xstrcat(msg, (char *)"\",\"unit\":\"");
+        msg = xstrcat(msg, tmpf->alias);
+        msg = xstrcat(msg, (char *)"\",\"online\":");
+        msg = xstrcat(msg, (tmpf->online) ? (char *)"1":(char *)"0");
+        msg = xstrcat(msg, (char *)"}");
+    }
+
+    msg = xstrcat(msg, (char *)"],\"co2meters\":[");
+    comma = false;
+    for (tmpc = co2meters; tmpc; tmpc = tmpc->next) {
+	if (comma)
+            msg = xstrcat(msg, (char *)",");
+        comma = true;
+        msg = xstrcat(msg, (char *)"{\"uuid\":\"");
+        msg = xstrcat(msg, tmpc->uuid);
+        msg = xstrcat(msg, (char *)"\",\"node\":\"");
+        msg = xstrcat(msg, tmpc->node);
+        msg = xstrcat(msg, (char *)"\",\"unit\":\"");
+        msg = xstrcat(msg, tmpc->alias);
+        msg = xstrcat(msg, (char *)"\",\"online\":");
+        msg = xstrcat(msg, (tmpc->online) ? (char *)"1":(char *)"0");
+        msg = xstrcat(msg, (char *)"}");
+    }
+
+    msg = xstrcat(msg, (char *)"],\"ispindels\":[");
+    comma = false;
+    for (tmpi = ispindels; tmpi; tmpi = tmpi->next) {
+	if (comma)
+            msg = xstrcat(msg, (char *)",");
+        comma = true;
+        msg = xstrcat(msg, (char *)"{\"uuid\":\"");
+        msg = xstrcat(msg, tmpi->uuid);
+        msg = xstrcat(msg, (char *)"\",\"node\":\"");
+        msg = xstrcat(msg, tmpi->node);
+        msg = xstrcat(msg, (char *)"\",\"unit\":\"");
+        msg = xstrcat(msg, tmpi->alias);
+        msg = xstrcat(msg, (char *)"\",\"online\":");
+        msg = xstrcat(msg, (tmpi->online) ? (char *)"1":(char *)"0");
+        msg = xstrcat(msg, (char *)"}");
+    }
+
+    msg = xstrcat(msg, (char *)"]}}");
+//    syslog(LOG_NOTICE, msg);
+    ws_broadcast(msg);
+    free(msg);
+    msg = NULL;
+}
+
+
--- a/bmsd/nodes.h	Tue May 19 22:48:59 2020 +0200
+++ b/bmsd/nodes.h	Wed May 20 21:49:09 2020 +0200
@@ -23,8 +23,13 @@
 
 /**
  * @brief Check if nodes are still online and mark them offline if not
- *        been seen for 10 minutes.
+ *        been seen for twice the node interval time plus a few seconds..
  */
 void nodes_check_online(void);
 
+/**
+ * @brief Report status change of nodes so that the client menus are updated.
+ */
+void status_change(void);
+
 #endif
--- a/www/includes/global.inc.php	Tue May 19 22:48:59 2020 +0200
+++ b/www/includes/global.inc.php	Wed May 20 21:49:09 2020 +0200
@@ -193,7 +193,7 @@
    </div>
 
    <!-- Menu -->
-   <div id="jqxMenu">
+   <div id="jqxMenu" style='float: left;'>
     <ul>
      <li style='width: 80px;'>
       <a href="index.php">Home</a>
@@ -202,63 +202,22 @@
       <ul style='width: 200px;'>
        <li><img style='float: left; margin-right: 5px;' src='images/computer.png' />Systemen
         <ul style='width: 200px;'>
-<?php
-$connect = mysqli_connect(DBASE_HOST,DBASE_USER,DBASE_PASS,DBASE_NAME);
-$result = mysqli_query($connect, "SELECT uuid,node,online,net_address FROM mon_nodes ORDER BY node");
-while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
-    if ($row['online'] == 1)
-	$img = "network-idle.png";
-    else
-	$img = "network-error.png";
-    echo '         <li><img style="float: left; margin-right: 5px;" src="images/'.$img.'" /><a href="mon_node.php?uuid='.$row['uuid'].'">'.$row['node'].'</a></li>'.PHP_EOL;
-}
-mysqli_free_result($result);
-?>
+         <div id="m_nodes"></div>
         </ul>
        </li>
        <li><img style='float: left; margin-right: 5px;' src='images/fridge.png' />Klimaatkasten
         <ul style='width: 200px;'>
-<?php
-$result = mysqli_query($connect, "SELECT uuid,node,alias,online,mode FROM mon_fermenters ORDER BY node,alias");
-while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
-    if ($row['online'] == 1)
-        $img = "network-idle.png";
-    else
-        $img = "network-error.png";
-    echo '         <li><img style="float: left; margin-right: 5px;" src="images/'.$img.'" /><a href="mon_fermenter.php?uuid='.$row['uuid'].'">'.$row['node'].'/'.$row['alias'].'</a></li>'.PHP_EOL;
-}
-mysqli_free_result($result);
-?>
+         <div id="m_fermenters"></div>
         </ul>
        </li>
        <li><img style='float: left; margin-right: 5px;' src='images/beerbottle.png' />CO2 meters
         <ul style='width: 250px;'>
-<?php
-$result = mysqli_query($connect, "SELECT uuid,node,alias,online,mode FROM mon_co2meters ORDER BY node,alias");
-while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
-    if ($row['online'] == 1)
-        $img = "network-idle.png";
-    else
-        $img = "network-error.png";
-    echo '         <li><img style="float: left; margin-right: 5px;" src="images/'.$img.'" /><a href="mon_co2meter.php?uuid='.$row['uuid'].'">'.$row['node'].'/'.$row['alias'].'</a></li>'.PHP_EOL;
-}
-mysqli_free_result($result);
-?>
+         <div id="m_co2meters"></div>
         </ul>
        </li>
        <li><img style='float: left; margin-right: 5px;' src='images/fermenter.png' />iSpindels
         <ul style='width: 250px;'>
-<?php
-$result = mysqli_query($connect, "SELECT uuid,node,alias,online FROM mon_ispindels ORDER BY node,alias");
-while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
-    if ($row['online'] == 1)
-        $img = "network-idle.png";
-    else
-        $img = "network-error.png";
-    echo '         <li><img style="float: left; margin-right: 5px;" src="images/'.$img.'" /><a href="mon_ispindel.php?uuid='.$row['uuid'].'">'.$row['alias'].'</a></li>'.PHP_EOL;
-}
-mysqli_free_result($result);
-?>
+         <div id="m_ispindels"></div>
         </ul>
        </li>
       </ul>
@@ -323,11 +282,62 @@
        <li><img style='float: left; margin-right: 5px;' src='images/help-about.png' /><a href="gen_about.php">Informatie</a></li>
       </ul>
      </li>
-     <li style='width: 300px;'><div id="wsstatus">ws: nc</div></li>
     </ul>
    </div> <!-- End menu -->
+   <div id="jqxStatus">
+    <div id="wsstatus" style="padding: 5px;"></div>
+   </div>
 
 <?php
+ $connect = mysqli_connect(DBASE_HOST,DBASE_USER,DBASE_PASS,DBASE_NAME);
+ $result = mysqli_query($connect, "SELECT uuid,node,online FROM mon_nodes ORDER BY node");
+ $menu_nodes = '';
+ while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
+    if ($row['online'] == 1)
+        $img = "network-idle.png";
+    else
+        $img = "network-error.png";
+    $menu_nodes .= '<li><img style="float: left; margin-right: 5px;" src="images/'.$img.'" /><a href="mon_node.php?uuid='.$row['uuid'].'">'.$row['node'].'</a></li>';
+ }
+ mysqli_free_result($result);
+ $menu_fermenters = '';
+ $result = mysqli_query($connect, "SELECT uuid,node,alias,online FROM mon_fermenters ORDER BY node,alias");
+ while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
+    if ($row['online'] == 1)
+        $img = "network-idle.png";
+    else
+        $img = "network-error.png";
+    $menu_fermenters .= '<li><img style="float: left; margin-right: 5px;" src="images/'.$img.'" /><a href="mon_fermenter.php?uuid='.$row['uuid'].'">'.$row['node'].'/'.$row['alias'].'</a></li>';
+ }
+ mysqli_free_result($result);
+ $menu_co2meters = '';
+ $result = mysqli_query($connect, "SELECT uuid,node,alias,online FROM mon_co2meters ORDER BY node,alias");
+ while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
+    if ($row['online'] == 1)
+        $img = "network-idle.png";
+    else
+        $img = "network-error.png";
+    $menu_co2meters .= '<li><img style="float: left; margin-right: 5px;" src="images/'.$img.'" /><a href="mon_co2meter.php?uuid='.$row['uuid'].'">'.$row['node'].'/'.$row['alias'].'</a></li>';
+ }
+ mysqli_free_result($result);
+ $menu_ispindels = '';
+ $result = mysqli_query($connect, "SELECT uuid,node,alias,online FROM mon_ispindels ORDER BY node,alias");
+ while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
+    if ($row['online'] == 1)
+        $img = "network-idle.png";
+    else
+        $img = "network-error.png";
+    $menu_ispindels .= '<li><img style="float: left; margin-right: 5px;" src="images/'.$img.'" /><a href="mon_ispindel.php?uuid='.$row['uuid'].'">'.$row['alias'].'</a></li>';
+ }
+ mysqli_free_result($result);
+?>
+ <script>
+  $('#m_nodes').html('<?php echo $menu_nodes; ?>');
+  $('#m_fermenters').html('<?php echo $menu_fermenters; ?>');
+  $('#m_co2meters').html('<?php echo $menu_co2meters; ?>');
+  $('#m_ispindels').html('<?php echo $menu_ispindels; ?>');
+ </script>
+<?php
 }
 
 
--- a/www/js/global.js	Tue May 19 22:48:59 2020 +0200
+++ b/www/js/global.js	Wed May 20 21:49:09 2020 +0200
@@ -771,20 +771,21 @@
 
 /* Handle global menu manipulation called by the user script. */
 function ws_global(msg) {
-
+ console.log('ws_global(' + msg + ')');
 }
 
 
 $(document).ready(function() {
 
  $('#jqxMenu').jqxMenu({
-  width: 1280,
+  width: 800,
   height: '30px',
   autoOpen: false,
   clickToOpen: true,
   theme: theme
  });
  $('#jqxWidget').css('visibility', 'visible');
+ $('#jqxStatus').jqxPanel({ width: 478, height: 30, theme: theme });
 });
 
 

mercurial