# HG changeset patch # User Michiel Broek # Date 1590004149 -7200 # Node ID ccb9f24d0fe9b35b63b9e126845843599ed8b4a0 # Parent 420bf5caf53ea1a26a4a09c3a5d21b5f9de57475 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. diff -r 420bf5caf53e -r ccb9f24d0fe9 bmsd/Makefile --- 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 diff -r 420bf5caf53e -r ccb9f24d0fe9 bmsd/co2meters.c --- 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(); } diff -r 420bf5caf53e -r ccb9f24d0fe9 bmsd/fermenters.c --- 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(); } diff -r 420bf5caf53e -r ccb9f24d0fe9 bmsd/ispindels.c --- 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. diff -r 420bf5caf53e -r ccb9f24d0fe9 bmsd/mysql.c --- 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); diff -r 420bf5caf53e -r ccb9f24d0fe9 bmsd/nodes.c --- 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; +} + + diff -r 420bf5caf53e -r ccb9f24d0fe9 bmsd/nodes.h --- 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 diff -r 420bf5caf53e -r ccb9f24d0fe9 www/includes/global.inc.php --- 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 @@ -
+
-
  • ws: nc
  • +
    +
    +
    '.$row['node'].''; + } + 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 .= '
  • '.$row['node'].'/'.$row['alias'].'
  • '; + } + 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 .= '
  • '.$row['node'].'/'.$row['alias'].'
  • '; + } + 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 .= '
  • '.$row['alias'].'
  • '; + } + mysqli_free_result($result); +?> + +