Added support for setting event values through MQTT
This commit is contained in:
parent
cc0b50f288
commit
d049e524d5
5 changed files with 44 additions and 4 deletions
|
|
@ -29,6 +29,7 @@ import se.hal.daemon.HalMulticastDnsDaemon;
|
|||
import se.hal.intf.*;
|
||||
import se.hal.plugin.mqtt.detector.HalMqttDetector;
|
||||
import se.hal.plugin.mqtt.device.HalMqttDeviceConfig;
|
||||
import se.hal.plugin.mqtt.device.HalMqttEventConfig;
|
||||
import zutil.InetUtil;
|
||||
import zutil.ObjectUtil;
|
||||
import zutil.log.LogUtil;
|
||||
|
|
@ -173,7 +174,10 @@ public class HalMqttController implements HalAutostartController, MqttSubscripti
|
|||
|
||||
@Override
|
||||
public void send(HalEventConfig eventConfig, HalEventData eventData) {
|
||||
if (eventConfig instanceof HalMqttDeviceConfig) {
|
||||
if (eventConfig instanceof HalMqttEventConfig) {
|
||||
HalMqttEventConfig mqttEvent = (HalMqttEventConfig) eventConfig;
|
||||
mqttBroker.publish(mqttEvent.getWriteTopicName(), mqttEvent.getMqttPublishPayload(eventData));
|
||||
} else if (eventConfig instanceof HalMqttDeviceConfig) {
|
||||
HalMqttDeviceConfig mqttEvent = (HalMqttDeviceConfig) eventConfig;
|
||||
mqttBroker.publish(mqttEvent.getTopicName(), Double.toString(eventData.getData()).getBytes());
|
||||
} else
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ public class Zigbee2mqttDetector implements HalMqttDetector {
|
|||
public List<HalMqttDeviceConfig> parseTopic(String topic, byte[] data) {
|
||||
if (ObjectUtil.isEmpty(topic) ||
|
||||
! topic.startsWith("zigbee2mqtt/") ||
|
||||
topic.endsWith("/set") ||
|
||||
topic.startsWith("zigbee2mqtt/bridge/"))
|
||||
return Collections.emptyList();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package se.hal.plugin.mqtt.device;
|
||||
|
||||
import se.hal.intf.HalDeviceData;
|
||||
import se.hal.intf.HalEventConfig;
|
||||
import se.hal.intf.HalEventController;
|
||||
import se.hal.intf.HalSensorConfig;
|
||||
|
|
@ -31,6 +32,14 @@ public abstract class HalMqttEventConfig extends HalMqttDeviceConfig implements
|
|||
this.writeTopicName = writeTopicName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the payload that will be added to a MQTT publish message.
|
||||
*
|
||||
* @param data the data that should be converted to a payload.
|
||||
* @return a byte array representing the given data, null if the conversion was not possible.
|
||||
*/
|
||||
public abstract byte[] getMqttPublishPayload(HalDeviceData data);
|
||||
|
||||
// --------------------------
|
||||
// Java Methods
|
||||
// --------------------------
|
||||
|
|
@ -51,4 +60,5 @@ public abstract class HalMqttEventConfig extends HalMqttDeviceConfig implements
|
|||
result = 31 * result + Objects.hashCode(writeTopicName);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,8 @@
|
|||
package se.hal.plugin.mqtt.device;
|
||||
|
||||
import se.hal.intf.HalAbstractController;
|
||||
import se.hal.intf.HalDeviceData;
|
||||
import se.hal.struct.devicedata.HumiditySensorData;
|
||||
import se.hal.struct.devicedata.OnOffEventData;
|
||||
import zutil.ObjectUtil;
|
||||
import zutil.converter.Converter;
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.DataNodePath;
|
||||
import zutil.parser.json.JSONParser;
|
||||
|
|
@ -82,4 +79,17 @@ public class HalMqttOnOffEventConfig extends HalMqttEventConfig {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getMqttPublishPayload(HalDeviceData data) {
|
||||
if (data instanceof OnOffEventData) {
|
||||
if (((OnOffEventData) data).isOn()) {
|
||||
return valueOnString.getBytes(StandardCharsets.UTF_8);
|
||||
} else {
|
||||
return valueOffString.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import se.hal.struct.devicedata.OnOffEventData;
|
|||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static se.hal.test.HalAssert.assertEqualsIgnoreTimestamp;
|
||||
|
||||
public class HalMqttOnOffEventConfigTest {
|
||||
|
|
@ -53,4 +54,18 @@ public class HalMqttOnOffEventConfigTest {
|
|||
assertEqualsIgnoreTimestamp(ON_EVENT, config.getDeviceData("{\"power\":10.48,\"state\":\"online\"}".getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMqttPublishPayload() {
|
||||
HalMqttOnOffEventConfig config = new HalMqttOnOffEventConfig();
|
||||
|
||||
assertEquals("ON", new String(config.getMqttPublishPayload(ON_EVENT)));
|
||||
assertEquals("OFF", new String(config.getMqttPublishPayload(OFF_EVENT)));
|
||||
|
||||
config.setValueOnString("online");
|
||||
config.setValueOffString("offline");
|
||||
|
||||
assertEquals("online", new String(config.getMqttPublishPayload(ON_EVENT)));
|
||||
assertEquals("offline", new String(config.getMqttPublishPayload(OFF_EVENT)));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue