Added ability send MQTT messages in the MQTT page

This commit is contained in:
Ziver Koc 2025-12-27 23:44:03 +01:00
parent 92d6d0886f
commit 5103fe6c44
3 changed files with 78 additions and 10 deletions

View file

@ -61,7 +61,7 @@ public class HalContext {
logger.info("Runtime folder: " + new File("").getAbsolutePath()); logger.info("Runtime folder: " + new File("").getAbsolutePath());
if (FileUtil.find("build/install/Hal") != null) // Development environment if (FileUtil.find("build/install/Hal") != null) // Development environment
RUNTIME_ROOT = "build/install/Hal"; RUNTIME_ROOT = "./build/install/Hal";
else else
RUNTIME_ROOT = "."; 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_WEB_ROOT = HalContext.RUNTIME_ROOT + "/web";
public static final String RESOURCE_BIN_ROOT = HalContext.RUNTIME_ROOT + "/bin"; public static final String RESOURCE_BIN_ROOT = HalContext.RUNTIME_ROOT + "/bin";
private static final String CONF_FILE = RUNTIME_ROOT + "/hal.conf"; private static final String CONF_FILE = "./hal.conf";
static final String DB_FILE = RUNTIME_ROOT + "/hal.db"; static final String DB_FILE = "./hal.db";
// Variables // Variables

View file

@ -3,17 +3,38 @@
<div class="row"> <div class="row">
<div class="col-md-8"> <div class="col-md-8">
<div class="panel panel-default drop-shadow"> <div class="panel panel-default drop-shadow">
<div class="panel-heading">Published Topic Data</div> <div class="panel-heading">Publish Data</div>
<div class="panel-body">
<form method="POST">
<div class="form-group">
<label for="topic">Topic:</label>
<input type="text" class="form-control" id="topic" name="topic">
</div>
<div class="form-group">
<label for="topic">Value:</label>
<input type="text" class="form-control" id="value" name="value">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
<br>
<div class="panel panel-default drop-shadow">
<div class="panel-heading">Broker Topic Data History</div>
<div class="panel-body"> <div class="panel-body">
<table class="table table-hover table-condensed"> <table class="table table-hover table-condensed">
<tr> <tr>
<th>Topic</th> <th>Topic</th>
<th>Published Data</th> <th>Published Data</th>
<th>Sub-Count</th>
</tr> </tr>
{{#topics}} {{#topics}}
<tr> <tr>
<td style="font-family:monospace;">{{.getKey()}}</td> <td style="font-family:monospace;">{{.topicName}}</td>
<td><pre style="white-space: pre-wrap;">{{.getValue()}}</pre></td> <td><pre style="white-space: pre-wrap;">{{.value}}</pre></td>
<td>{{.getSubscriberCount()}}</td>
</tr> </tr>
{{/topics}} {{/topics}}
</table> </table>

View file

@ -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; package se.hal.plugin.mqtt.page;
import se.hal.HalContext; import se.hal.HalContext;
@ -17,7 +41,8 @@ import java.util.Map;
public class MqttOverviewPage extends HalWebPage implements MqttSubscriptionListener { public class MqttOverviewPage extends HalWebPage implements MqttSubscriptionListener {
private static final String TEMPLATE = HalContext.RESOURCE_WEB_ROOT + "/mqtt_overview.tmpl"; private static final String TEMPLATE = HalContext.RESOURCE_WEB_ROOT + "/mqtt_overview.tmpl";
private Map<String, String> topicData = new HashMap<>(); private MqttBroker broker;
private Map<String, TopicDTO> topicData = new HashMap<>();
public MqttOverviewPage() { 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); super.getRootNav().createSubNav("Settings").createSubNav(this.getId(), "MQTT Overview").setWeight(9_000);
HalMqttController controller = HalAbstractControllerManager.getController(HalMqttController.class); HalMqttController controller = HalAbstractControllerManager.getController(HalMqttController.class);
MqttBroker broker = controller.getBroker(); broker = controller.getBroker();
broker.addGlobalSubscriber(this); broker.addGlobalSubscriber(this);
} }
@Override @Override
public synchronized void dataPublished(String topic, byte[] data) { 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 @Override
@ -42,8 +71,26 @@ public class MqttOverviewPage extends HalWebPage implements MqttSubscriptionList
Map<String, String> request) Map<String, String> request)
throws Exception { throws Exception {
if (request.containsKey("topic") && request.containsKey("value")) {
broker.publish(request.get("topic"), request.get("value"));
}
Templator tmpl = new Templator(FileUtil.find(TEMPLATE)); Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
tmpl.set("topics", topicData.entrySet()); tmpl.set("topics", topicData.values());
return tmpl; 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);
}
}
} }