Added support for setting event values through MQTT

This commit is contained in:
Ziver Koc 2024-09-26 00:31:25 +02:00
parent cc0b50f288
commit d049e524d5
5 changed files with 44 additions and 4 deletions

View file

@ -29,6 +29,7 @@ import se.hal.daemon.HalMulticastDnsDaemon;
import se.hal.intf.*; import se.hal.intf.*;
import se.hal.plugin.mqtt.detector.HalMqttDetector; import se.hal.plugin.mqtt.detector.HalMqttDetector;
import se.hal.plugin.mqtt.device.HalMqttDeviceConfig; import se.hal.plugin.mqtt.device.HalMqttDeviceConfig;
import se.hal.plugin.mqtt.device.HalMqttEventConfig;
import zutil.InetUtil; import zutil.InetUtil;
import zutil.ObjectUtil; import zutil.ObjectUtil;
import zutil.log.LogUtil; import zutil.log.LogUtil;
@ -173,7 +174,10 @@ public class HalMqttController implements HalAutostartController, MqttSubscripti
@Override @Override
public void send(HalEventConfig eventConfig, HalEventData eventData) { 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; HalMqttDeviceConfig mqttEvent = (HalMqttDeviceConfig) eventConfig;
mqttBroker.publish(mqttEvent.getTopicName(), Double.toString(eventData.getData()).getBytes()); mqttBroker.publish(mqttEvent.getTopicName(), Double.toString(eventData.getData()).getBytes());
} else } else

View file

@ -22,6 +22,7 @@ public class Zigbee2mqttDetector implements HalMqttDetector {
public List<HalMqttDeviceConfig> parseTopic(String topic, byte[] data) { public List<HalMqttDeviceConfig> parseTopic(String topic, byte[] data) {
if (ObjectUtil.isEmpty(topic) || if (ObjectUtil.isEmpty(topic) ||
! topic.startsWith("zigbee2mqtt/") || ! topic.startsWith("zigbee2mqtt/") ||
topic.endsWith("/set") ||
topic.startsWith("zigbee2mqtt/bridge/")) topic.startsWith("zigbee2mqtt/bridge/"))
return Collections.emptyList(); return Collections.emptyList();

View file

@ -1,5 +1,6 @@
package se.hal.plugin.mqtt.device; package se.hal.plugin.mqtt.device;
import se.hal.intf.HalDeviceData;
import se.hal.intf.HalEventConfig; import se.hal.intf.HalEventConfig;
import se.hal.intf.HalEventController; import se.hal.intf.HalEventController;
import se.hal.intf.HalSensorConfig; import se.hal.intf.HalSensorConfig;
@ -31,6 +32,14 @@ public abstract class HalMqttEventConfig extends HalMqttDeviceConfig implements
this.writeTopicName = writeTopicName; 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 // Java Methods
// -------------------------- // --------------------------
@ -51,4 +60,5 @@ public abstract class HalMqttEventConfig extends HalMqttDeviceConfig implements
result = 31 * result + Objects.hashCode(writeTopicName); result = 31 * result + Objects.hashCode(writeTopicName);
return result; return result;
} }
} }

View file

@ -1,11 +1,8 @@
package se.hal.plugin.mqtt.device; package se.hal.plugin.mqtt.device;
import se.hal.intf.HalAbstractController;
import se.hal.intf.HalDeviceData; import se.hal.intf.HalDeviceData;
import se.hal.struct.devicedata.HumiditySensorData;
import se.hal.struct.devicedata.OnOffEventData; import se.hal.struct.devicedata.OnOffEventData;
import zutil.ObjectUtil; import zutil.ObjectUtil;
import zutil.converter.Converter;
import zutil.parser.DataNode; import zutil.parser.DataNode;
import zutil.parser.DataNodePath; import zutil.parser.DataNodePath;
import zutil.parser.json.JSONParser; import zutil.parser.json.JSONParser;
@ -82,4 +79,17 @@ public class HalMqttOnOffEventConfig extends HalMqttEventConfig {
return null; 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;
}
} }

View file

@ -5,6 +5,7 @@ import se.hal.struct.devicedata.OnOffEventData;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static org.junit.Assert.*;
import static se.hal.test.HalAssert.assertEqualsIgnoreTimestamp; import static se.hal.test.HalAssert.assertEqualsIgnoreTimestamp;
public class HalMqttOnOffEventConfigTest { public class HalMqttOnOffEventConfigTest {
@ -53,4 +54,18 @@ public class HalMqttOnOffEventConfigTest {
assertEqualsIgnoreTimestamp(ON_EVENT, config.getDeviceData("{\"power\":10.48,\"state\":\"online\"}".getBytes(StandardCharsets.UTF_8))); 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)));
}
} }