diff --git a/ESH-INF/thing/rts.xml b/ESH-INF/thing/rts.xml index 935a8ca..7bfc78f 100644 --- a/ESH-INF/thing/rts.xml +++ b/ESH-INF/thing/rts.xml @@ -25,9 +25,9 @@ Must contain the Protocol + Address + Index (using '-' as separator). For instance : RTS-123ABC-0 (According to the RFLink protocol, Index is not used for now and '0' should be used) - - - Reference Address Id to project the incoming message (blank by default) + + + Pattern to apply on the incoming message to generate a ECHO command (KEY=newValue, ";" as separator, empty by default) diff --git a/src/main/java/org/openhab/binding/rflink/config/RfLinkDeviceConfiguration.java b/src/main/java/org/openhab/binding/rflink/config/RfLinkDeviceConfiguration.java index 151f17c..6b30ab9 100644 --- a/src/main/java/org/openhab/binding/rflink/config/RfLinkDeviceConfiguration.java +++ b/src/main/java/org/openhab/binding/rflink/config/RfLinkDeviceConfiguration.java @@ -19,8 +19,8 @@ public class RfLinkDeviceConfiguration { // Device Id public String deviceId; - // Linked Adress Id - public String linkedAddressId = null; + // Pattern to echo the input command + public String echoPattern = null; // Number of times to repeat a message public int repeats = 1; @@ -37,8 +37,8 @@ public String toString() { + (shutterDuration > 0 ? "timing=" + shutterDuration + "s" : ""); } - public boolean hasLinkedAddressId() { - return linkedAddressId != null && !linkedAddressId.trim().isEmpty(); + public boolean hasEcho() { + return echoPattern != null && !echoPattern.trim().isEmpty(); } } diff --git a/src/main/java/org/openhab/binding/rflink/device/RfLinkAbstractDevice.java b/src/main/java/org/openhab/binding/rflink/device/RfLinkAbstractDevice.java index 77faec3..0aa236b 100644 --- a/src/main/java/org/openhab/binding/rflink/device/RfLinkAbstractDevice.java +++ b/src/main/java/org/openhab/binding/rflink/device/RfLinkAbstractDevice.java @@ -41,7 +41,7 @@ public abstract class RfLinkAbstractDevice implements RfLinkDevice { public void initializeFromMessage(RfLinkDeviceConfiguration config, RfLinkMessage message) { setConfig(config); this.message = message; - if (getConfig() != null && getConfig().hasLinkedAddressId()) { + if (getConfig() != null && getConfig().hasEcho()) { // must "echo" the input command to the linked DeviceId } } @@ -86,28 +86,19 @@ public Collection buildOutputPackets() { @Override public Collection buildEchoPackets() { - if (config.hasLinkedAddressId()) { + RfLinkPacket packet = null; + if (config.hasEcho()) { if (getMessage() != null) { - String echoMessage = getMessage().getRawMessage(); - if (echoMessage != null) { - String sourceAddressId = "ID=" + getMessage().getDeviceId(); - String targetAddressId = "ID=" + config.linkedAddressId; - if (echoMessage.contains(sourceAddressId)) { - // take the input message and replace the initial AddressId by the linked addressId - echoMessage = echoMessage.replace(sourceAddressId, targetAddressId); - RfLinkPacket packet = new RfLinkPacket(RfLinkPacketType.ECHO, echoMessage); - return Collections.singletonList(packet); - } else { - // original AddressId not found in the input Message - } - } else { - // no initial raw message : unable to build echo message - } + packet = getMessage().buildEchoPacket(config.echoPattern); } else { // no initial message (why are we here ?!?) } } + if (packet != null) { + return Collections.singletonList(packet); + } return Collections.emptyList(); + } // to override in subClasses if needed diff --git a/src/main/java/org/openhab/binding/rflink/message/RfLinkMessage.java b/src/main/java/org/openhab/binding/rflink/message/RfLinkMessage.java index 4270b6c..68fa47b 100644 --- a/src/main/java/org/openhab/binding/rflink/message/RfLinkMessage.java +++ b/src/main/java/org/openhab/binding/rflink/message/RfLinkMessage.java @@ -65,7 +65,7 @@ public RfLinkMessage(RfLinkDeviceConfiguration config, ChannelUID channelUID, Co public RfLinkMessage(RfLinkPacket packet) { rawMessage = packet.getPacket(); - final String[] elements = packet.getPacket().split(FIELDS_DELIMITER); + final String[] elements = packet.getPacket().split(FIELDS_DELIMITER, 4); final int size = elements.length; // Every message should have at least 5 parts // Example : 20;31;Mebus;ID=c201;TEMP=00cf; @@ -77,14 +77,8 @@ public RfLinkMessage(RfLinkPacket packet) { seqNbr = (byte) Integer.parseInt(elements[1], 16); protocol = RfLinkDataParser.cleanString(elements[2]); // build the key>value map - if (size >= 4) { - for (int i = 3; i < size; i++) { - String[] keyValue = elements[i].split(VALUE_DELIMITER, 2); - if (keyValue.length > 1) { - // Raw values are stored, and will be decoded by sub implementations - attributes.put(keyValue[0], keyValue[1]); - } - } + if (size == 4) { + extractAttributes(attributes, elements[3]); } deviceId = attributes.get("ID"); deviceSubId = attributes.get("SWITCH"); @@ -92,6 +86,17 @@ public RfLinkMessage(RfLinkPacket packet) { } } + public static void extractAttributes(Map attributesMap, String attributesAsString) { + String[] elements = attributesAsString.split(FIELDS_DELIMITER); + for (String element : elements) { + String[] keyValue = element.split(VALUE_DELIMITER, 2); + if (keyValue.length > 1) { + // Raw values are stored, and will be decoded by sub implementations + attributesMap.put(keyValue[0], keyValue[1]); + } + } + } + @Override public String toString() { return getDeviceKey(); @@ -175,4 +180,21 @@ private void appendToMessage(StringBuilder message, String element) { message.append(element).append(FIELDS_DELIMITER); } + public RfLinkPacket buildEchoPacket(String echoPattern) { + if (getRawMessage() != null) { + String echoPacket = getRawMessage(); + Map overridenAttributes = new HashMap<>(); + RfLinkMessage.extractAttributes(overridenAttributes, echoPattern); + for (String overridenAttributeKey : overridenAttributes.keySet()) { + String sourceAttribute = overridenAttributeKey + "=" + getAttributes().get(overridenAttributeKey); + String targetAttribute = overridenAttributeKey + "=" + overridenAttributes.get(overridenAttributeKey); + echoPacket = echoPacket.replace(sourceAttribute, targetAttribute); + } + return new RfLinkPacket(RfLinkPacketType.ECHO, echoPacket); + } else { + // no initial raw message : unable to build echo message + } + return null; + } + }