bmsd/nodes.c

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 677
6e82fece1f8f
child 686
ca1fbb3bd3db
permissions
-rw-r--r--

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.

/**
 * @file nodes.c
 * @brief Handle nodes status
 * @author Michiel Broek <mbroek at mbse dot eu>
 *
 * Copyright (C) 2018-2020
 *
 * This file is part of the bms (Brewery Management System)
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * bms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */


#include "bms.h"
#include "xutil.h"
#include "nodes.h"
#include "mysql.h"
#include "websocket.h"


sys_node_list			*nodes = NULL;

extern int			debug;
extern sys_fermenter_list	*fermenters;
extern sys_co2meter_list	*co2meters;
extern sys_ispindel_list	*ispindels;


void node_birth_data(char *topic, char *payload)
{
    struct json_object	*jobj, *val, *metric, *metric2;
    sys_node_list	*node, *tmpp;
    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);

    strtok(topic, "/");	// ignore namespace
    group_id = strtok(NULL, "/");
    message_type = strtok(NULL, "/");
    edge_node = strtok(NULL, "/\0");

    /*
     * Search node record in the memory array and use it if found.
     */
    if (nodes) {
	for (tmpp = nodes; tmpp; tmpp = tmpp->next) {
	    if ((strcmp(tmpp->group_id, group_id) == 0) && (strcmp(tmpp->node, edge_node) == 0)) {
		new_node = false;
		node = tmpp;
		node->lastseen = time(NULL);
		node->online = true;
		break;
	    }
	}
    }

    /*
     * Allocate new node if not yet known.
     */
    if (new_node) {
	node = (sys_node_list *)malloc(sizeof(sys_node_list));
    	node->uuid = NULL;
   	node->next = NULL;
    	node->group_id = xstrcpy(group_id);
    	node->node = xstrcpy(edge_node);
    	node->online = true;
    	node->hardwaremake = node->hardwaremodel = node->os = node->os_version = node->firmware = NULL;
    	node->firstseen = node->lastseen = time(NULL);
    	node->temperature = node->humidity = node->barometer = 0.0;
    	node->gps_latitude = node->gps_longitude = node->gps_altitude = 0.0;
	node->net_address = node->net_ifname = NULL;
	node->net_rssi = 0;
	node->interval = 300;
    }

    /*
     * Process the JSON formatted payload. 
     * Update only the fields that are found in the payload.
     */
    jobj = json_tokener_parse(payload);

    if (json_object_object_get_ex(jobj, "timestamp", &val)) {
	if (strcmp((char *)"NDATA", message_type)) {
	    node->firstseen = json_object_get_int(val);
	    syslog(LOG_NOTICE, "Online node `%s/%s'", node->group_id, node->node);
	} else {
	    node->lastseen = json_object_get_int(val);
	}
    }

    if (json_object_object_get_ex(jobj, "metric", &metric)) {
	if (json_object_object_get_ex(metric, "uuid", &val)) {
	    if (node->uuid)
		free(node->uuid);
	    node->uuid = xstrcpy((char *)json_object_get_string(val));
	}
	if (json_object_object_get_ex(metric, "interval", &val)) {
	    node->interval = json_object_get_int(val);
	}
	if (json_object_object_get_ex(metric, "properties", &metric2)) {
	    if (json_object_object_get_ex(metric2, "hardwaremake", &val)) {
		if (node->hardwaremake)
		    free(node->hardwaremake);
		node->hardwaremake = xstrcpy((char *)json_object_get_string(val));
	    }
	    if (json_object_object_get_ex(metric2, "hardwaremodel", &val)) {
		if (node->hardwaremodel)
		    free(node->hardwaremodel);
		node->hardwaremodel = xstrcpy((char *)json_object_get_string(val));
	    }
	    if (json_object_object_get_ex(metric2, "os", &val)) {
		if (node->os)
		    free(node->os);
		node->os = xstrcpy((char *)json_object_get_string(val));
	    }
	    if (json_object_object_get_ex(metric2, "os_version", &val)) {
		if (node->os_version)
		    free(node->os_version);
		node->os_version = xstrcpy((char *)json_object_get_string(val));
	    }
	    if (json_object_object_get_ex(metric2, "FW", &val)) {
		if (node->firmware)
		    free(node->firmware);
		node->firmware = xstrcpy((char *)json_object_get_string(val));
	    }
	}
	if (json_object_object_get_ex(metric, "THB", &metric2)) {
	    if (json_object_object_get_ex(metric2, "temperature", &val)) {
		node->temperature = json_object_get_double(val);
	    }
	    if (json_object_object_get_ex(metric2, "humidity", &val)) {
		node->humidity= json_object_get_double(val);
	    }
	    if (json_object_object_get_ex(metric2, "barometer", &val)) {
		node->barometer = json_object_get_double(val);
	    }
	}
	if (json_object_object_get_ex(metric, "GPS", &metric2)) {
	    if (json_object_object_get_ex(metric2, "latitude", &val)) {
		node->gps_latitude = json_object_get_double(val);
	    }
	    if (json_object_object_get_ex(metric2, "longitude", &val)) {
		node->gps_longitude = json_object_get_double(val);
	    }
	    if (json_object_object_get_ex(metric2, "altitude", &val)) {
		node->gps_altitude = json_object_get_double(val);
	    }
	}
	if (json_object_object_get_ex(metric, "net", &metric2)) {
	    if (json_object_object_get_ex(metric2, "address", &val)) {
		if (node->net_address)
		    free(node->net_address);
		node->net_address = xstrcpy((char *)json_object_get_string(val));
	    }
	    if (json_object_object_get_ex(metric2, "ifname", &val)) {
		if (node->net_ifname)
		    free(node->net_ifname);
		node->net_ifname = xstrcpy((char *)json_object_get_string(val));
	    }
	    if (json_object_object_get_ex(metric2, "rssi", &val)) {
		node->net_rssi = json_object_get_int(val);
	    }
	}
    }
    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) {
    	if (nodes == NULL) {
	    nodes = node;
	} else {
	    for (tmpp = nodes; tmpp; tmpp = tmpp->next) {
		if (tmpp->next == NULL) {
		    tmpp->next = node;
		    break;
		}
	    }
	}
	node_mysql_insert(node);
    } else {
	node_mysql_update(node);
    }
}



void node_dump(sys_node_list *node)
{
    if (debug) {
	printf("online    %s\n", node->online ? "yes":"no");
    	printf("uuid      %s\n", node->uuid);
    	printf("node      %s\n", node->node);
    	printf("group     %s\n", node->group_id);
    	printf("hw make   %s  model %s\n", node->hardwaremake, node->hardwaremodel);
    	printf("os        %s  version %s\n", node->os, node->os_version);
    	printf("firmware  %s\n", node->firmware);
    	printf("first     %ld   last %ld\n", node->firstseen, node->lastseen);
    	printf("THB       %.2f  %.2f  %.2f\n", node->temperature, node->humidity, node->barometer);
    	printf("GPS       %.5f  %.5f  %.5f\n", node->gps_latitude, node->gps_longitude, node->gps_altitude);
	printf("net       %s:%s\n", node->net_ifname, node->net_address);
	printf("rssi      %d\n", node->net_rssi);
	printf("interval  %d\n", node->interval);
    }
}



void node_death(char *topic)
{
    char		*group_id, *edge_node, *msg;
    sys_node_list	*tmpp;

    strtok(topic, "/"); // ignore namespace
    group_id = strtok(NULL, "/");
    strtok(NULL, "/");	// ignore message_type
    edge_node = strtok(NULL, "/\0");

    syslog(LOG_NOTICE, "Offline node `%s/%s'", group_id, edge_node);
    node_mysql_death(edge_node);

    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;
	}
    }
}



void nodes_check_online()
{
    sys_node_list	*tmpn;
    sys_fermenter_list  *tmpf;
    sys_co2meter_list	*tmpc;
    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\":\"");
            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);
syslog(LOG_NOTICE, msg);
            free(msg);
            msg = NULL;

	    for (tmpf = fermenters; tmpf; tmpf = tmpf->next) {
            	if (strcmp(tmpf->node, tmpn->node) == 0) {
                    if (tmpf->online) {
                    	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\":\"fermenters\",\"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);
syslog(LOG_NOTICE, msg);
            		free(msg);
            		msg = NULL;
		    }
            	}
            }

	    for (tmpc = co2meters; tmpc; tmpc = tmpc->next) {
		if (strcmp(tmpc->node, tmpn->node) == 0) {
		    if (tmpc->online) {
			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\":\"co2meters\",\"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);
syslog(LOG_NOTICE, msg);
                        free(msg);
                        msg = NULL;
		    }
		}
	    }

	    for (tmpi = ispindels; tmpi; tmpi = tmpi->next) {
		if (strcmp(tmpi->node, tmpn->node) == 0) {
		    if (tmpi->online) {
                        syslog(LOG_NOTICE, "Timeout ispindel %s", tmpi->node);
                        tmpi->online = false;
                        ispindel_mysql_death(tmpi->node);
			msg = xstrcpy((char *)"{\"device\":\"ispindels\",\"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);
syslog(LOG_NOTICE, msg);
                        free(msg);
                        msg = NULL;
                    }
		}
	    }
	}
    }

    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;
}

mercurial