# HG changeset patch # User Michiel Broek # Date 1657547178 -7200 # Node ID 9472106a314363810b295b4bdd1311c02525f9d1 # Parent 37e1704fd11f06649f3b3a81d3cf108c466f0dfe Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes. diff -r 37e1704fd11f -r 9472106a3143 bmsd/Makefile --- a/bmsd/Makefile Wed Jul 06 09:28:41 2022 +0200 +++ b/bmsd/Makefile Mon Jul 11 15:46:18 2022 +0200 @@ -63,8 +63,8 @@ lock.o: lock.h bms.h futil.h mqtt.o: bms.h xutil.h mqtt.h nodes.h fermenters.h co2meters.h ispindels.h mysql.o: bms.h xutil.h mysql.h nodes.h -nodes.o: bms.h xutil.h nodes.h mysql.h websocket.h +nodes.o: bms.h xutil.h nodes.h mqtt.h mysql.h websocket.h rdconfig.o: bms.h xutil.h futil.h rdconfig.h -websocket.o: bms.h xutil.h websocket.h fermenters.h co2meters.h ispindels.h +websocket.o: bms.h xutil.h websocket.h fermenters.h co2meters.h ispindels.h nodes.h xutil.o: bms.h xutil.h # End of generated dependencies diff -r 37e1704fd11f -r 9472106a3143 bmsd/nodes.c --- a/bmsd/nodes.c Wed Jul 06 09:28:41 2022 +0200 +++ b/bmsd/nodes.c Mon Jul 11 15:46:18 2022 +0200 @@ -26,6 +26,7 @@ #include "bms.h" #include "xutil.h" #include "nodes.h" +#include "mqtt.h" #include "mysql.h" #include "websocket.h" @@ -412,4 +413,55 @@ } +void node_ws_receive(char *payload) +{ + struct json_object *jobj, *val; + char *node = NULL, *group = NULL, *control = NULL, *topic = NULL, *pay = NULL, buf[33]; + syslog(LOG_NOTICE, "node_ws %s", payload); + /* + * Process the JSON formatted payload. + */ + jobj = json_tokener_parse(payload); + if (json_object_object_get_ex(jobj, "node", &val)) + node = xstrcpy((char *)json_object_get_string(val)); + if (json_object_object_get_ex(jobj, "group_id", &val)) + group = xstrcpy((char *)json_object_get_string(val)); + if (json_object_object_get_ex(jobj, "control", &val)) + control = xstrcpy((char *)json_object_get_string(val)); + + if (node && group && control) { + + /* + * Prepare MQTT topic + */ + topic = xstrcpy((char *)"mbv1.0/"); + topic = xstrcat(topic, group); + topic = xstrcat(topic, (char *)"/NCMD/"); + topic = xstrcat(topic, node); + + pay = xstrcpy((char *)"{\"timestamp\":"); + snprintf(buf, 32, "%ld", time(NULL)); + pay = xstrcat(pay, buf); + pay = xstrcat(pay, (char *)",\"metric\":"); + + if (strcmp(control, (char *)"reboot") == 0) { + pay = xstrcat(pay, (char *)"{\"Node Control/Reboot\":true}}"); + } else if (strcmp(control, (char *)"rebirth") == 0) { + pay = xstrcat(pay, (char *)"{\"Node Control/Rebirth\":true}}"); + } + syslog(LOG_NOTICE, "%s %s", topic, pay); + mqtt_publish(topic, pay); + } + + if (node) + free(node); + if (group) + free(group); + if (control) + free(control); + if (topic) + free(topic); + if (pay) + free(pay); +} diff -r 37e1704fd11f -r 9472106a3143 bmsd/nodes.h --- a/bmsd/nodes.h Wed Jul 06 09:28:41 2022 +0200 +++ b/bmsd/nodes.h Mon Jul 11 15:46:18 2022 +0200 @@ -27,4 +27,10 @@ */ void nodes_check_online(void); +/** + * @brief Process received command from a websocket. + * @param payload The received data in JSON format. + */ +void node_ws_receive(char *payload); + #endif diff -r 37e1704fd11f -r 9472106a3143 bmsd/websocket.c --- a/bmsd/websocket.c Wed Jul 06 09:28:41 2022 +0200 +++ b/bmsd/websocket.c Mon Jul 11 15:46:18 2022 +0200 @@ -30,6 +30,7 @@ #include "fermenters.h" #include "co2meters.h" #include "ispindels.h" +#include "nodes.h" #include @@ -126,6 +127,8 @@ co2meter_ws_receive(buf); } else if (strncmp(buf, (char *)"{\"device\":\"ispindels\",", 22) == 0) { ispindel_ws_receive(buf); + } else if (strncmp(buf, (char *)"{\"node\":\"", 9) == 0) { + node_ws_receive(buf); } break; diff -r 37e1704fd11f -r 9472106a3143 config.status --- a/config.status Wed Jul 06 09:28:41 2022 +0200 +++ b/config.status Mon Jul 11 15:46:18 2022 +0200 @@ -620,8 +620,8 @@ "uid" S["CC"]="gcc" S["CYEARS"]="2016-2022" -S["COPYRIGHT"]="Copyright (C) 2016-2021 Michiel Broek, All Rights Reserved" -S["VERSION"]="0.3.39" +S["COPYRIGHT"]="Copyright (C) 2016-2022 Michiel Broek, All Rights Reserved" +S["VERSION"]="0.3.40" S["PACKAGE"]="bms" S["SUBDIRS"]="bmsd doc script tools www" S["target_alias"]="" @@ -709,8 +709,8 @@ D["PACKAGE_STRING"]=" \"\"" D["PACKAGE_BUGREPORT"]=" \"\"" D["PACKAGE_URL"]=" \"\"" -D["VERSION"]=" \"0.3.39\"" -D["COPYRIGHT"]=" \"Copyright (C) 2016-2021 Michiel Broek, All Rights Reserved\"" +D["VERSION"]=" \"0.3.40\"" +D["COPYRIGHT"]=" \"Copyright (C) 2016-2022 Michiel Broek, All Rights Reserved\"" D["STDC_HEADERS"]=" 1" D["HAVE_SYS_TYPES_H"]=" 1" D["HAVE_SYS_STAT_H"]=" 1" diff -r 37e1704fd11f -r 9472106a3143 configure --- a/configure Wed Jul 06 09:28:41 2022 +0200 +++ b/configure Mon Jul 11 15:46:18 2022 +0200 @@ -2031,8 +2031,8 @@ PACKAGE="bms" -VERSION="0.3.39" -COPYRIGHT="Copyright (C) 2016-2021 Michiel Broek, All Rights Reserved" +VERSION="0.3.40" +COPYRIGHT="Copyright (C) 2016-2022 Michiel Broek, All Rights Reserved" CYEARS="2016-2022" diff -r 37e1704fd11f -r 9472106a3143 configure.ac --- a/configure.ac Wed Jul 06 09:28:41 2022 +0200 +++ b/configure.ac Mon Jul 11 15:46:18 2022 +0200 @@ -8,8 +8,8 @@ dnl General settings dnl After changeing the version number, run autoconf! PACKAGE="bms" -VERSION="0.3.39" -COPYRIGHT="Copyright (C) 2016-2021 Michiel Broek, All Rights Reserved" +VERSION="0.3.40" +COPYRIGHT="Copyright (C) 2016-2022 Michiel Broek, All Rights Reserved" CYEARS="2016-2022" AC_SUBST(PACKAGE) AC_SUBST(VERSION)