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
|
|
@ -11,16 +11,18 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static se.hal.HalContext.CONFIG_DNS_LOCAL_DOMAIN;
|
||||
|
||||
|
||||
public class HalMulticastDnsDaemon implements HalDaemon {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static HalMulticastDnsDaemon instance;
|
||||
|
||||
private MulticastDnsServer server;
|
||||
|
||||
@Override
|
||||
public void initiate(ScheduledExecutorService executor) {
|
||||
public synchronized void initiate(ScheduledExecutorService executor) {
|
||||
if (instance != null)
|
||||
return;
|
||||
|
||||
String localDomain = HalContext.getStringProperty(HalContext.CONFIG_DNS_LOCAL_DOMAIN, "hal.local");
|
||||
|
||||
if (!localDomain.isEmpty()) {
|
||||
|
|
@ -30,6 +32,8 @@ public class HalMulticastDnsDaemon implements HalDaemon {
|
|||
server = new MulticastDnsServer();
|
||||
server.addEntry(localDomain, InetAddress.getLocalHost());
|
||||
server.start();
|
||||
|
||||
instance = this;
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Was unable to start mDNS Server.", e);
|
||||
}
|
||||
|
|
@ -38,4 +42,12 @@ public class HalMulticastDnsDaemon implements HalDaemon {
|
|||
}
|
||||
}
|
||||
|
||||
public void addDnsEntry(String name, InetAddress ip) {
|
||||
server.addEntry(name, ip);
|
||||
}
|
||||
|
||||
|
||||
public static HalMulticastDnsDaemon getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ java.util.logging.FileHandler.formatter = zutil.log.CompactLogFormatter
|
|||
|
||||
zutil.level = ALL
|
||||
zutil.db.bean.level = INFO
|
||||
zutil.net.http.HttpServer.level = FINE
|
||||
zutil.net.http.page.HttpFilePage.level = INFO
|
||||
zutil.net.dns.MulticastDnsServer.level = FINE
|
||||
|
||||
|
|
|
|||
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 {
|
||||
if (HalMulticastDnsDaemon.getInstance() != null) {
|
||||
logger.info("Register MQTT in mDNS");
|
||||
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();
|
||||
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