Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.

Mon, 11 Jul 2022 15:46:18 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 11 Jul 2022 15:46:18 +0200
changeset 795
9472106a3143
parent 794
37e1704fd11f
child 796
6ce2c2e6796e

Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.

bmsd/Makefile file | annotate | diff | comparison | revisions
bmsd/nodes.c file | annotate | diff | comparison | revisions
bmsd/nodes.h file | annotate | diff | comparison | revisions
bmsd/websocket.c file | annotate | diff | comparison | revisions
config.status file | annotate | diff | comparison | revisions
configure file | annotate | diff | comparison | revisions
configure.ac file | annotate | diff | comparison | revisions
--- 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
--- 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);
+}
--- 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
--- 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 <libwebsockets.h>
 
 
@@ -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;
--- 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"
--- 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"
 
 
--- 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)

mercurial