Added ability send MQTT messages in the MQTT page
This commit is contained in:
parent
92d6d0886f
commit
5103fe6c44
3 changed files with 78 additions and 10 deletions
|
|
@ -3,17 +3,38 @@
|
|||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
<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">
|
||||
<table class="table table-hover table-condensed">
|
||||
<tr>
|
||||
<th>Topic</th>
|
||||
<th>Published Data</th>
|
||||
<th>Sub-Count</th>
|
||||
</tr>
|
||||
{{#topics}}
|
||||
<tr>
|
||||
<td style="font-family:monospace;">{{.getKey()}}</td>
|
||||
<td><pre style="white-space: pre-wrap;">{{.getValue()}}</pre></td>
|
||||
<td style="font-family:monospace;">{{.topicName}}</td>
|
||||
<td><pre style="white-space: pre-wrap;">{{.value}}</pre></td>
|
||||
<td>{{.getSubscriberCount()}}</td>
|
||||
</tr>
|
||||
{{/topics}}
|
||||
</table>
|
||||
|
|
|
|||
|
|
@ -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<String, String> topicData = new HashMap<>();
|
||||
private MqttBroker broker;
|
||||
private Map<String, TopicDTO> 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<String, String> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue