From 5103fe6c44c475903f983dfba56657971f869e35 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sat, 27 Dec 2025 23:44:03 +0100 Subject: [PATCH] Added ability send MQTT messages in the MQTT page --- hal-core/src/se/hal/HalContext.java | 6 +- .../hal-mqtt/resources/web/mqtt_overview.tmpl | 27 ++++++++- .../plugin/mqtt/page/MqttOverviewPage.java | 55 +++++++++++++++++-- 3 files changed, 78 insertions(+), 10 deletions(-) diff --git a/hal-core/src/se/hal/HalContext.java b/hal-core/src/se/hal/HalContext.java index 0483d0f9..3752c9d2 100644 --- a/hal-core/src/se/hal/HalContext.java +++ b/hal-core/src/se/hal/HalContext.java @@ -61,7 +61,7 @@ public class HalContext { logger.info("Runtime folder: " + new File("").getAbsolutePath()); if (FileUtil.find("build/install/Hal") != null) // Development environment - RUNTIME_ROOT = "build/install/Hal"; + RUNTIME_ROOT = "./build/install/Hal"; else RUNTIME_ROOT = "."; } @@ -70,8 +70,8 @@ public class HalContext { public static final String RESOURCE_WEB_ROOT = HalContext.RUNTIME_ROOT + "/web"; public static final String RESOURCE_BIN_ROOT = HalContext.RUNTIME_ROOT + "/bin"; - private static final String CONF_FILE = RUNTIME_ROOT + "/hal.conf"; - static final String DB_FILE = RUNTIME_ROOT + "/hal.db"; + private static final String CONF_FILE = "./hal.conf"; + static final String DB_FILE = "./hal.db"; // Variables diff --git a/plugins/hal-mqtt/resources/web/mqtt_overview.tmpl b/plugins/hal-mqtt/resources/web/mqtt_overview.tmpl index 548070c3..5bc861aa 100644 --- a/plugins/hal-mqtt/resources/web/mqtt_overview.tmpl +++ b/plugins/hal-mqtt/resources/web/mqtt_overview.tmpl @@ -3,17 +3,38 @@
-
Published Topic Data
+
Publish Data
+
+
+
+ + +
+
+ + +
+ +
+
+
+ +
+ +
+
Broker Topic Data History
+ {{#topics}} - - + + + {{/topics}}
Topic Published DataSub-Count
{{.getKey()}}
{{.getValue()}}
{{.topicName}}
{{.value}}
{{.getSubscriberCount()}}
diff --git a/plugins/hal-mqtt/src/se/hal/plugin/mqtt/page/MqttOverviewPage.java b/plugins/hal-mqtt/src/se/hal/plugin/mqtt/page/MqttOverviewPage.java index d43d1017..2fd26a4d 100644 --- a/plugins/hal-mqtt/src/se/hal/plugin/mqtt/page/MqttOverviewPage.java +++ b/plugins/hal-mqtt/src/se/hal/plugin/mqtt/page/MqttOverviewPage.java @@ -1,3 +1,27 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2025 Ziver Koc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + package se.hal.plugin.mqtt.page; import se.hal.HalContext; @@ -17,7 +41,8 @@ import java.util.Map; public class MqttOverviewPage extends HalWebPage implements MqttSubscriptionListener { private static final String TEMPLATE = HalContext.RESOURCE_WEB_ROOT + "/mqtt_overview.tmpl"; - private Map topicData = new HashMap<>(); + private MqttBroker broker; + private Map topicData = new HashMap<>(); public MqttOverviewPage() { @@ -25,14 +50,18 @@ public class MqttOverviewPage extends HalWebPage implements MqttSubscriptionList super.getRootNav().createSubNav("Settings").createSubNav(this.getId(), "MQTT Overview").setWeight(9_000); HalMqttController controller = HalAbstractControllerManager.getController(HalMqttController.class); - MqttBroker broker = controller.getBroker(); + broker = controller.getBroker(); broker.addGlobalSubscriber(this); } @Override public synchronized void dataPublished(String topic, byte[] data) { - topicData.put(topic, new String(data, StandardCharsets.UTF_8)); + if (!topicData.containsKey(topic)) { + topicData.put(topic, new TopicDTO(topic)); + } + + topicData.get(topic).value = new String(data, StandardCharsets.UTF_8); } @Override @@ -42,8 +71,26 @@ public class MqttOverviewPage extends HalWebPage implements MqttSubscriptionList Map request) throws Exception { + if (request.containsKey("topic") && request.containsKey("value")) { + broker.publish(request.get("topic"), request.get("value")); + } + Templator tmpl = new Templator(FileUtil.find(TEMPLATE)); - tmpl.set("topics", topicData.entrySet()); + tmpl.set("topics", topicData.values()); return tmpl; } + + + public class TopicDTO { + public String topicName; + public String value; + + public TopicDTO(String topicName) { + this.topicName = topicName; + } + + public int getSubscriberCount() { + return broker.getSubscriberCount(topicName); + } + } }