bmsd/nodes.c

Tue, 04 Jun 2019 19:50:06 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 04 Jun 2019 19:50:06 +0200
changeset 396
804800d8e885
parent 384
442d23455ae4
child 502
a8a6901b5a99
permissions
-rw-r--r--

Added console logging in the grid write callback functions. Removed grid sorting, it is done on the server side. Alert popup for block row edit functions. Better grid live updates. In recipe and product print show the whirlpool time. The checklist now shows misc ingredients added in the mash. Show hops added in the whirlpool. Most ingredient names are now quoted.

/**
 * @file nodes.c
 * @brief Handle nodes status
 * @author Michiel Broek <mbroek at mbse dot eu>
 *
 * Copyright (C) 2018
 *
 * 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"


sys_node_list			*nodes = NULL;

extern int			debug;
extern sys_fermenter_list	*fermenters;


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

    /*
     * 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, "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)
		free(node->uuid);
	    node->uuid = xstrcpy((char *)json_object_get_string(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);

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



void node_death(char *topic)
{
    char		*group_id, *edge_node;
    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;
	    break;
	}
    }
}



void nodes_check_online()
{
    sys_node_list	*tmpn;
    sys_fermenter_list  *tmpf;
    time_t		now = time(NULL);

    for (tmpn = nodes; tmpn; tmpn = tmpn->next) {
	if (tmpn->online && ((now - tmpn->lastseen) > 600)) {
	    syslog(LOG_NOTICE, "Timeout node `%s/%s'", tmpn->group_id, tmpn->node);
	    tmpn->online = false;
	    node_mysql_death(tmpn->node);

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

mercurial