New MQTT plugin
This commit is contained in:
parent
cb9838181f
commit
584760b6ba
6 changed files with 319 additions and 0 deletions
23
plugins/hal-mqtt/build.xml
Normal file
23
plugins/hal-mqtt/build.xml
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project name="Hal MQTT Plugin" >
|
||||
|
||||
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
|
||||
|
||||
<!--common properties-->
|
||||
<property name="root" value="." />
|
||||
<property name="srcDir" value="${root}/src" />
|
||||
<property name="testDir" value="${root}/test" />
|
||||
<property name="libDir" value="${root}/lib" />
|
||||
|
||||
<property name="buildRoot" value="${root}/build" />
|
||||
<property name="compileDir" value="${buildRoot}/production" />
|
||||
<property name="compileTestDir" value="${buildRoot}/test" />
|
||||
<property name="releaseDir" value="${buildRoot}/release" />
|
||||
<property name="releaseJar" value="hal-mqtt.jar" />
|
||||
<property name="reportsDir" value="../../${buildRoot}/reports" /> <!-- Use Hal reports folder -->
|
||||
|
||||
<!-- ________________________ TARGETS ________________________ -->
|
||||
|
||||
<import file="../../build_plugin.xml"/>
|
||||
|
||||
</project>
|
||||
147
plugins/hal-mqtt/src/se/hal/plugin/mqtt/HalMqttController.java
Normal file
147
plugins/hal-mqtt/src/se/hal/plugin/mqtt/HalMqttController.java
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 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;
|
||||
|
||||
import se.hal.intf.*;
|
||||
import se.hal.plugin.mqtt.device.HalMqttDeviceConfig;
|
||||
import se.hal.plugin.mqtt.device.HalMqttDeviceData;
|
||||
import zutil.InetUtil;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.net.dns.MulticastDnsServer;
|
||||
import zutil.net.mqtt.MqttBroker;
|
||||
import zutil.net.mqtt.MqttSubscriptionListener;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class HalMqttController implements HalAutoScannableController, MqttSubscriptionListener, HalEventController {
|
||||
private final Logger logger = LogUtil.getLogger();
|
||||
|
||||
private MulticastDnsServer mDns;
|
||||
private MqttBroker mqttBroker;
|
||||
|
||||
private HashMap<String, HalMqttDeviceConfig> topics = new HashMap<>();
|
||||
private HalEventReportListener eventListener;
|
||||
|
||||
// --------------------------
|
||||
// Lifecycle methods
|
||||
// --------------------------
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
try {
|
||||
InetAddress serverIp = InetUtil.getLocalInet4Address().get(0);
|
||||
|
||||
mDns = new MulticastDnsServer();
|
||||
mDns.addEntry("_mqtt.tcp", serverIp);
|
||||
mDns.addEntry("hal.local", serverIp);
|
||||
|
||||
mqttBroker = new MqttBroker();
|
||||
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Unable to initialize MQTT plugin.", e);
|
||||
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return mDns != null && mqttBroker != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(){
|
||||
if (mDns != null) {
|
||||
mDns.close();
|
||||
mDns = null;
|
||||
}
|
||||
|
||||
if (mqttBroker != null) {
|
||||
mqttBroker.close();
|
||||
mqttBroker = null;
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------
|
||||
// MQTT Methods
|
||||
// --------------------------
|
||||
|
||||
@Override
|
||||
public void dataPublished(String topic, byte[] data) {
|
||||
HalMqttDeviceConfig eventConfig = topics.get(topic);
|
||||
|
||||
if (eventConfig != null && data.length > 0) {
|
||||
HalMqttDeviceData eventData = new HalMqttDeviceData(data);
|
||||
|
||||
if (eventListener != null) {
|
||||
eventListener.reportReceived(eventConfig, eventData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------
|
||||
// Hal Methods
|
||||
// --------------------------
|
||||
|
||||
@Override
|
||||
public void register(HalEventConfig eventConfig) {
|
||||
if(eventConfig instanceof HalMqttDeviceConfig) {
|
||||
HalMqttDeviceConfig mqttEvent = (HalMqttDeviceConfig) eventConfig;
|
||||
topics.put(mqttEvent.getTopic(), mqttEvent);
|
||||
} else throw new IllegalArgumentException(
|
||||
"Device config is not an instance of " + HalMqttDeviceConfig.class + ": " + eventConfig.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deregister(HalEventConfig eventConfig) {
|
||||
if(eventConfig instanceof HalMqttDeviceConfig) {
|
||||
HalMqttDeviceConfig mqttEvent = (HalMqttDeviceConfig) eventConfig;
|
||||
topics.remove(mqttEvent.getTopic());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(HalEventConfig eventConfig, HalEventData eventData) {
|
||||
if(eventConfig instanceof HalMqttDeviceConfig) {
|
||||
HalMqttDeviceConfig mqttEvent = (HalMqttDeviceConfig) eventConfig;
|
||||
mqttBroker.publish(mqttEvent.getTopic(), Double.toString(eventData.getData()).getBytes());
|
||||
} else throw new IllegalArgumentException(
|
||||
"Device config is not an instance of " + HalMqttDeviceConfig.class + ": " + eventConfig.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return topics.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setListener(HalEventReportListener listener) {
|
||||
eventListener = listener;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 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.device;
|
||||
|
||||
import se.hal.intf.HalEventConfig;
|
||||
import se.hal.intf.HalEventController;
|
||||
import se.hal.intf.HalEventData;
|
||||
import se.hal.plugin.mqtt.HalMqttController;
|
||||
|
||||
public class HalMqttDeviceConfig implements HalEventConfig {
|
||||
private final String topic;
|
||||
|
||||
|
||||
public HalMqttDeviceConfig(String topic) {
|
||||
this.topic = topic;
|
||||
}
|
||||
|
||||
|
||||
public String getTopic() {
|
||||
return topic;
|
||||
}
|
||||
|
||||
// --------------------------
|
||||
// Hal Methods
|
||||
// --------------------------
|
||||
|
||||
@Override
|
||||
public Class<? extends HalEventController> getEventControllerClass() {
|
||||
return HalMqttController.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends HalEventData> getEventDataClass() {
|
||||
return HalMqttDeviceData.class;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 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.device;
|
||||
|
||||
import se.hal.intf.HalEventData;
|
||||
import zutil.StringUtil;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class HalMqttDeviceData extends HalEventData {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
private double data;
|
||||
|
||||
|
||||
public HalMqttDeviceData(byte[] byteData) {
|
||||
String str = new String(byteData);
|
||||
|
||||
if (StringUtil.isDecimalNumber(str)) {
|
||||
data = Double.parseDouble(str);
|
||||
} else {
|
||||
logger.warning("Received non numeric MQTT data.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setData(double data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
7
plugins/hal-mqtt/src/se/hal/plugin/mqtt/plugin.json
Normal file
7
plugins/hal-mqtt/src/se/hal/plugin/mqtt/plugin.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"version": 1.0,
|
||||
"name": "Hal-MQTT",
|
||||
"interfaces": [
|
||||
{"se.hal.intf.HalAutoScannableController": "se.hal.plugin.mqtt.HalMqttController"}
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue