diff -r b4bfed3684d0 -r e75ce5bbda73 bmsd/ispindels.c --- a/bmsd/ispindels.c Fri Jan 03 15:21:22 2020 +0100 +++ b/bmsd/ispindels.c Sun Jan 05 11:42:02 2020 +0100 @@ -3,7 +3,7 @@ * @brief Handle ispindels data * @author Michiel Broek * - * Copyright (C) 2019 + * Copyright (C) 2019-2020 * * This file is part of the bms (Brewery Management System) * @@ -40,18 +40,18 @@ -void ispindel_set(char *node, char *key, char *payload) +void ispindel_set(char *node, char *payload) { sys_ispindel_list *ispindel, *tmpp; - bool new_ispindel = true, do_update = false; - char *t, *p, *datetime, buf[65], *line, *logfile; - float temperature = 20; - uint8_t temp_units = 'C'; + struct json_object *jobj, *metric, *val; + bool new_ispindel = true; + char *datetime, buf[65], *line, *logfile; struct tm *mytime; time_t timestamp; FILE *fp; - fprintf(stdout, "ispindel_set: %s %s\n", node, payload); + if (debug) + printf("ispindel_set: %s %s\n", node, payload); /* * Search ispindel record in the memory array and use it if found. @@ -66,7 +66,8 @@ } } - printf("new_ispindel %s\n", new_ispindel ? "true":"false"); + if (debug) + printf("new_ispindel %s\n", new_ispindel ? "true":"false"); /* * Allocate new ispindel if not yet known. @@ -75,64 +76,53 @@ ispindel = (sys_ispindel_list *)malloc(sizeof(sys_ispindel_list)); memset(ispindel, 0, sizeof(sys_ispindel_list)); ispindel->node = xstrcpy(node); + ispindel->mode = xstrcpy((char *)"OFF"); } if (! ispindel->online) { ispindel->online = true; - syslog(LOG_NOTICE, "Online ispindel %s", node); + syslog(LOG_NOTICE, "Online ispindel %s mode %s", node, ispindel->mode); } /* - * Process the simple iSpindel MQTT payload. + * Process the JSON formatted payload. + * Update only the fields that are found in the payload. */ - if (strcmp(key, "tilt") == 0) { - ispindel->tilt = atof(payload); - } else if (strcmp(key, "temperature") == 0) { - temperature = atof(payload); - } else if (strcmp(key, "temp_units") == 0) { - temp_units = payload[0]; - } else if (strcmp(key, "battery") == 0) { - ispindel->battery = atof(payload); - } else if (strcmp(key, "gravity") == 0) { - ispindel->gravity = atof(payload); - } else if (strcmp(key, "interval") == 0) { - ispindel->interval = atoi(payload); - } else if (strcmp(key, "RSSI") == 0) { - ispindel->rssi = atoi(payload); - do_update = true; - if (temp_units == 'C') { - ispindel->temperature = temperature; - } else if (temp_units == 'F') { - ispindel->temperature = temperature / 1.8 - 32; - } else if (temp_units == 'K') { - ispindel->temperature = temperature - 273.15; - } else { - ispindel->temperature = temperature; - } - } else { - syslog(LOG_NOTICE, "Unknown keyword `%s' from `%s'", key, node); + jobj = json_tokener_parse(payload); + + if (json_object_object_get_ex(jobj, "unit", &metric)) { + if (json_object_object_get_ex(metric, "uuid", &val)) { + if (ispindel->uuid) + free(ispindel->uuid); + ispindel->uuid = xstrcpy((char *)json_object_get_string(val)); + } + if (json_object_object_get_ex(metric, "alias", &val)) { + if (ispindel->alias) + free(ispindel->alias); + ispindel->alias = xstrcpy((char *)json_object_get_string(val)); + } + if (json_object_object_get_ex(metric, "alarm", &val)) { + ispindel->alarm = json_object_get_int(val); + } + if (json_object_object_get_ex(metric, "interval", &val)) { + ispindel->interval = json_object_get_int(val); + } + if (json_object_object_get_ex(metric, "angle", &val)) { + ispindel->angle = json_object_get_double(val); + } + if (json_object_object_get_ex(metric, "temperature", &val)) { + ispindel->temperature = json_object_get_double(val); + } + if (json_object_object_get_ex(metric, "battery", &val)) { + ispindel->battery = json_object_get_double(val); + } + if (json_object_object_get_ex(metric, "gravity", &val)) { + ispindel->gravity = json_object_get_double(val); + } } - if (ispindel->battery < LOWBATT) - ispindel->alarm |= ALARM_FLAG_BATTERY; - else - ispindel->alarm &= ~ALARM_FLAG_BATTERY; - ispindel_dump(ispindel); - if (new_ispindel || do_update) { - t = xstrcpy((char *)"mbv1.0/ispindels/NBIRTH/"); - t = xstrcat(t, node); - - p = xstrcpy((char *)"{\"metric\":{\"properties\":{\"hardwaremake\":\"MBSE\",\"hardwaremodel\":\"Wemos D1 mini\"},\"net\":{\"rssi\":"); - sprintf(buf, "%d", ispindel->rssi); - p = xstrcat(p, buf); - p = xstrcat(p, (char *)"}}}"); - node_birth_data(t, p); - free(t); - free(p); - } - if (new_ispindel) { if (ispindels == NULL) { ispindels = ispindel; @@ -145,14 +135,15 @@ } } ispindel_mysql_insert(ispindel); - } else if (do_update) { + } else { ispindel_mysql_update(ispindel); } /* * The data is complete, see if we can write a log entry. */ - if (do_update && ispindel->beercode && strlen(ispindel->beercode) && ispindel->beername && strlen(ispindel->beername)) { + if (ispindel->beercode && strlen(ispindel->beercode) && ispindel->beername && strlen(ispindel->beername) && + (strcmp(ispindel->mode, (char *)"ON") == 0)) { datetime = malloc(72); mytime = localtime(×tamp); snprintf(datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d", @@ -160,23 +151,22 @@ line = xstrcpy(datetime); line = xstrcat(line, (char *)","); - snprintf(buf, 64, "%.3f", ispindel->tilt); - line = xstrcat(line, buf); - line = xstrcat(line, (char *)","); - snprintf(buf, 64, "%.3f", ispindel->temperature); + snprintf(buf, 64, "%.4f", ispindel->temperature); line = xstrcat(line, buf); line = xstrcat(line, (char *)","); - snprintf(buf, 64, "%.3f", ispindel->battery); + snprintf(buf, 64, "%.5f", ispindel->gravity); line = xstrcat(line, buf); line = xstrcat(line, (char *)","); - snprintf(buf, 64, "%.3f", ispindel->gravity); + snprintf(buf, 64, "%.5f", ispindel->battery); + line = xstrcat(line, buf); + line = xstrcat(line, (char *)","); + snprintf(buf, 64, "%.5f", ispindel->angle); line = xstrcat(line, buf); line = xstrcat(line, (char *)","); snprintf(buf, 64, "%d", ispindel->interval); line = xstrcat(line, buf); line = xstrcat(line, (char *)","); - snprintf(buf, 64, "%d", ispindel->rssi); - line = xstrcat(line, buf); + line = xstrcat(line, ispindel->uuid); /* * Build logfile name @@ -210,20 +200,18 @@ /* * Process iSpindel MQTT message. */ -void ispindel_mqtt(char *topic, char *payload) +void ispindel_birth_data(char *topic, char *payload) { - char *namespace, *node, *keyword; + char *message_type, *edge_node; - namespace = strtok(topic, "/"); // must be ispindel - node = strtok(NULL, "/"); - keyword = strtok(NULL, "/\0"); + strtok(topic, "/"); // ignore namespace + strtok(NULL, "/"); + message_type = strtok(NULL, "/"); + edge_node = strtok(NULL, "/\0"); - if (strcmp(namespace, "ispindels")) { - syslog(LOG_NOTICE, "ispindel_mqtt(%s, %s) error", topic, payload); - return; + if (strcmp("DBIRTH", message_type) == 0) { + ispindel_set(edge_node, payload); } - - ispindel_set(node, keyword, payload); } @@ -231,15 +219,16 @@ void ispindel_dump(sys_ispindel_list *ispindel) { if (debug) { - printf("node %s\n", ispindel->node); + printf("node/alias %s / %s\n", ispindel->node, ispindel->alias); + printf("uuid %s\n", ispindel->uuid); printf("online %s\n", ispindel->online ? "yes":"no"); + printf("mode %s\n", ispindel->mode); printf("product %s / %s\n", ispindel->beercode, ispindel->beername); - printf("tilt %.3f\n", ispindel->tilt); - printf("temperature %.3f\n", ispindel->temperature); - printf("battery %.3f\n", ispindel->battery); - printf("gravity %.3f\n", ispindel->gravity); + printf("tilt %.5f\n", ispindel->angle); + printf("temperature %.5f\n", ispindel->temperature); + printf("battery %.5f\n", ispindel->battery); + printf("gravity %.5f\n", ispindel->gravity); printf("interval %d\n", ispindel->interval); - printf("rssi %d\n", ispindel->rssi); } }