Skip to content

Commit

Permalink
[RTS] cyrilcc#48 : Shutter position tracking - echo enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
cartemere committed Jun 28, 2019
1 parent 16e9a6c commit 5a8cd40
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
6 changes: 3 additions & 3 deletions ESH-INF/thing/rts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
<description>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)</description>
</parameter>

<parameter name="linkedAddressId" type="text" required="false">
<label>Linked Address Id</label>
<description>Reference Address Id to project the incoming message (blank by default)</description>
<parameter name="echoPattern" type="text" required="false">
<label>Echo Pattern</label>
<description>Pattern to apply on the incoming message to generate a ECHO command (KEY=newValue, ";" as separator, empty by default)</description>
</parameter>

<parameter name="isCommandReversed" type="boolean" required="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -86,28 +86,19 @@ public Collection<RfLinkPacket> buildOutputPackets() {

@Override
public Collection<RfLinkPacket> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -77,21 +77,26 @@ 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");
}
}
}

public static void extractAttributes(Map<String, String> 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();
Expand Down Expand Up @@ -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<String, String> 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;
}

}

0 comments on commit 5a8cd40

Please sign in to comment.