Improved MQTT Broker and added overview page
This commit is contained in:
parent
bda3f270af
commit
926c3e2af2
6 changed files with 103 additions and 18 deletions
23
plugins/hal-mqtt/resources/web/mqtt_overview.tmpl
Normal file
23
plugins/hal-mqtt/resources/web/mqtt_overview.tmpl
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<h1 class="page-header">MQTT Overview</h1>
|
||||
|
||||
<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-body">
|
||||
<table class="table table-hover table-condensed">
|
||||
<tr>
|
||||
<th>Topic</th>
|
||||
<th>Published Data</th>
|
||||
</tr>
|
||||
{{#topics}}
|
||||
<tr>
|
||||
<td style="font-family:monospace;">{{.getKey()}}</td>
|
||||
<td><pre style="white-space: pre-wrap;">{{.getValue()}}</pre></td>
|
||||
</tr>
|
||||
{{/topics}}
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
package se.hal.plugin.mqtt;
|
||||
|
||||
import se.hal.daemon.HalMulticastDnsDaemon;
|
||||
import se.hal.intf.*;
|
||||
import se.hal.plugin.mqtt.device.HalMqttDeviceConfig;
|
||||
import se.hal.plugin.mqtt.device.HalMqttDeviceData;
|
||||
|
|
@ -44,7 +45,6 @@ import java.util.logging.Logger;
|
|||
public class HalMqttController implements HalAutostartController, MqttSubscriptionListener, HalEventController {
|
||||
private final Logger logger = LogUtil.getLogger();
|
||||
|
||||
private MulticastDnsServer mDns;
|
||||
private MqttBroker mqttBroker;
|
||||
|
||||
private HashMap<String, HalMqttDeviceConfig> topics = new HashMap<>();
|
||||
|
|
@ -57,13 +57,13 @@ public class HalMqttController implements HalAutostartController, MqttSubscripti
|
|||
@Override
|
||||
public void initialize() {
|
||||
try {
|
||||
InetAddress serverIp = InetUtil.getLocalInet4Address().get(0);
|
||||
|
||||
logger.info("Starting up mDNS Server");
|
||||
mDns = new MulticastDnsServer();
|
||||
mDns.addEntry("_mqtt._tcp.local", serverIp);
|
||||
mDns.addEntry("_hal._tcp.local", serverIp);
|
||||
mDns.start();
|
||||
if (HalMulticastDnsDaemon.getInstance() != null) {
|
||||
logger.info("Register MQTT in mDNS");
|
||||
InetAddress serverIp = InetUtil.getLocalInet4Address().get(0);
|
||||
HalMulticastDnsDaemon.getInstance().addDnsEntry("_mqtt._tcp.local", serverIp);
|
||||
} else {
|
||||
logger.info("mDNS not available");
|
||||
}
|
||||
|
||||
logger.info("Starting up MQTT Server");
|
||||
mqttBroker = new MqttBroker();
|
||||
|
|
@ -83,12 +83,6 @@ public class HalMqttController implements HalAutostartController, MqttSubscripti
|
|||
|
||||
@Override
|
||||
public void close(){
|
||||
if (mDns != null) {
|
||||
logger.info("Shutting down mDNS Server");
|
||||
mDns.close();
|
||||
mDns = null;
|
||||
}
|
||||
|
||||
if (mqttBroker != null) {
|
||||
logger.info("Shutting down MQTT Server");
|
||||
mqttBroker.close();
|
||||
|
|
@ -115,6 +109,10 @@ public class HalMqttController implements HalAutostartController, MqttSubscripti
|
|||
}
|
||||
}
|
||||
|
||||
public MqttBroker getBroker() {
|
||||
return mqttBroker;
|
||||
}
|
||||
|
||||
// --------------------------
|
||||
// Hal Methods
|
||||
// --------------------------
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
package se.hal.plugin.mqtt.page;
|
||||
|
||||
import se.hal.HalContext;
|
||||
import se.hal.intf.HalAbstractControllerManager;
|
||||
import se.hal.intf.HalWebPage;
|
||||
import se.hal.plugin.mqtt.HalMqttController;
|
||||
import zutil.io.file.FileUtil;
|
||||
import zutil.net.mqtt.MqttBroker;
|
||||
import zutil.net.mqtt.MqttSubscriptionListener;
|
||||
import zutil.parser.Templator;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
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<>();
|
||||
|
||||
|
||||
public MqttOverviewPage() {
|
||||
super("mqtt_overview");
|
||||
super.getRootNav().createSubNav("Settings").createSubNav(this.getId(), "MQTT Overview").setWeight(9_000);
|
||||
|
||||
HalMqttController controller = HalAbstractControllerManager.getController(HalMqttController.class);
|
||||
MqttBroker broker = controller.getBroker();
|
||||
broker.addGlobalSubscriber(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized void dataPublished(String topic, byte[] data) {
|
||||
topicData.put(topic, new String(data, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Templator httpRespond(
|
||||
Map<String, Object> session,
|
||||
Map<String, String> cookie,
|
||||
Map<String, String> request)
|
||||
throws Exception {
|
||||
|
||||
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
|
||||
tmpl.set("topics", topicData.entrySet());
|
||||
return tmpl;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
"version": 0.1,
|
||||
"name": "Hal-MQTT",
|
||||
"interfaces": [
|
||||
{"se.hal.intf.HalAutostartController": "se.hal.plugin.mqtt.HalMqttController"}
|
||||
{"se.hal.intf.HalAutostartController": "se.hal.plugin.mqtt.HalMqttController"},
|
||||
|
||||
{"se.hal.intf.HalWebPage": "se.hal.plugin.mqtt.page.MqttOverviewPage"}
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue