Skip to content

Commit

Permalink
[RTS] cyrilcc#48 : Shutter position tracking - echo physical remote t…
Browse files Browse the repository at this point in the history
…o tracker
  • Loading branch information
cartemere committed Jun 27, 2019
1 parent 62a526b commit 16e9a6c
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 18 deletions.
5 changes: 5 additions & 0 deletions ESH-INF/thing/rts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<label>Device Id</label>
<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>

<parameter name="isCommandReversed" type="boolean" required="false">
<label>Reverse Command</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class RfLinkDeviceConfiguration {
// Device Id
public String deviceId;

// Linked Adress Id
public String linkedAddressId = null;

// Number of times to repeat a message
public int repeats = 1;

Expand All @@ -34,4 +37,8 @@ public String toString() {
+ (shutterDuration > 0 ? "timing=" + shutterDuration + "s" : "");
}

public boolean hasLinkedAddressId() {
return linkedAddressId != null && !linkedAddressId.trim().isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public abstract class RfLinkAbstractDevice implements RfLinkDevice {
public void initializeFromMessage(RfLinkDeviceConfiguration config, RfLinkMessage message) {
setConfig(config);
this.message = message;
if (getConfig() != null && getConfig().hasLinkedAddressId()) {
// must "echo" the input command to the linked DeviceId
}
}

@Override
Expand Down Expand Up @@ -77,10 +80,36 @@ public RfLinkDeviceConfiguration getConfig() {
}

@Override
public Collection<RfLinkPacket> buildPackets() {
public Collection<RfLinkPacket> buildOutputPackets() {
return Collections.singleton(getMessage().buildRfLinkPacket(RfLinkPacketType.OUTPUT, getCommandSuffix()));
}

@Override
public Collection<RfLinkPacket> buildEchoPackets() {
if (config.hasLinkedAddressId()) {
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
}
} else {
// no initial message (why are we here ?!?)
}
}
return Collections.emptyList();
}

// to override in subClasses if needed
public String getCommandSuffix() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected boolean handleCommandTransmission() {
}

@Override
public Collection<RfLinkPacket> buildPackets() {
public Collection<RfLinkPacket> buildOutputPackets() {
logger.debug("Color decodeMessage: command={}, stateColor={}, stateOnOff={}", command, stateColor, stateOnOff);

if (command == null) {
Expand Down Expand Up @@ -231,4 +231,5 @@ public Collection<RfLinkPacket> buildPackets() {
}
return messages;
}

}
15 changes: 11 additions & 4 deletions src/main/java/org/openhab/binding/rflink/device/RfLinkDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ public interface RfLinkDevice {
/**
* Procedure generate RfLinkPacket[s] to send to the bridge
*
* @return Collection of RfLinkPacket messages to be send over serial (OUTPUT type) or to handle as incoming events
* (ECHO type). Several elements in case of composite command
* @return Collection of RfLinkPacket messages to be send over serial (OUTPUT type)
*/

public Collection<RfLinkPacket> buildPackets();
public Collection<RfLinkPacket> buildOutputPackets();

/**
* Procedure generate RfLinkPackets[s] to send to the handler as Incoming messages (notification service)
*
* @return Collection of RfLinkPacket[s] to handle as incoming events
* (ECHO type). Several elements in case of composite command
*/
public Collection<RfLinkPacket> buildEchoPackets();

/**
* Procedure to get device unique Identifier
Expand Down Expand Up @@ -93,7 +100,7 @@ void initializeFromChannel(RfLinkDeviceConfiguration config, ChannelUID channelU

/**
* Initializes Device from reception message
*
*
* @param config TODO
* @param message the RfLink message received from the bridge
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,12 @@ public void handleCommand(ChannelUID channelUID, Command command) {
try {
RfLinkDevice device = RfLinkDeviceFactory.createDeviceFromType(getThing().getThingTypeUID());
device.initializeFromChannel(config, channelUID, command);
processEchoPackets(device);
if (isRtsPositionTrackerEnabled(device)) {
// need specific handling : the command is processed by the tracker
handleRtsPositionTracker(this, device);
} else {
int repeats = Math.min(Math.max(getConfiguration().repeats, 1), 20);
Collection<RfLinkPacket> packets = device.buildPackets();
for (int i = 0; i < repeats; i++) {
bridgeHandler.processPackets(packets);
}
processOutputPackets(device);
updateThingStates(device);
}
} catch (RfLinkNotImpException e) {
Expand All @@ -112,9 +109,10 @@ public void handleCommand(ChannelUID channelUID, Command command) {
@Override
public boolean handleIncomingMessage(ThingUID bridge, RfLinkMessage incomingMessage) throws Exception {
String id = incomingMessage.getDeviceKey();
if (config != null && id.equals(config.deviceId)) {
if (config != null && id.equalsIgnoreCase(config.deviceId)) {
RfLinkDevice device = RfLinkDeviceFactory.createDeviceFromMessage(incomingMessage);
device.initializeFromMessage(config, incomingMessage);
processEchoPackets(device);
updateStatus(ThingStatus.ONLINE);
if (isRtsPositionTrackerEnabled(device)) {
handleRtsPositionTracker(this, device);
Expand All @@ -124,7 +122,19 @@ public boolean handleIncomingMessage(ThingUID bridge, RfLinkMessage incomingMess
return true;
}
return false;
}

private void processOutputPackets(RfLinkDevice device) throws RfLinkException {
int repeats = Math.min(Math.max(getConfiguration().repeats, 1), 20);
Collection<RfLinkPacket> packets = device.buildOutputPackets();
for (int i = 0; i < repeats; i++) {
bridgeHandler.processPackets(packets);
}
}

private void processEchoPackets(RfLinkDevice device) throws RfLinkException {
Collection<RfLinkPacket> echoPackets = device.buildEchoPackets();
bridgeHandler.processPackets(echoPackets);
}

@Override
Expand Down Expand Up @@ -221,4 +231,9 @@ private RfLinkRtsPositionHandler getShutterInfos(RfLinkHandler handler, RfLinkDe
return shutterInfos;
}

@Override
public String toString() {
return "RfLinkHandler [" + config + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void sendCommand(Command command) {

private void sendDeviceCommand(RfLinkDevice device) {
try {
handler.getBridgeHandler().processPackets(device.buildPackets());
handler.getBridgeHandler().processPackets(device.buildOutputPackets());
} catch (RfLinkException e) {
logger.error("Could not send Device event " + device + " on bridge " + handler.getBridgeHandler(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,18 @@ public String getRawMessage() {
}

public String getDeviceKey() {
String deviceIdKey = protocol + ID_DELIMITER + deviceId;
if (deviceSubId != null) {
deviceIdKey += ID_DELIMITER + deviceSubId;
String deviceIdKey = getBaseDeviceKey();
if (getDeviceSubId() != null) {
deviceIdKey += ID_DELIMITER + getDeviceSubId();
}
return deviceIdKey;
}

public String getBaseDeviceKey() {
String deviceIdKey = getProtocol() + ID_DELIMITER + getDeviceId();
return deviceIdKey;
}

public String getProtocol() {
return protocol;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class RfLinkPacket {
private RfLinkPacketType type;
private String packet;

public static String PING = "10;PING;";

public RfLinkPacket(RfLinkPacketType type, String packet) {
super();
this.type = type;
Expand Down

0 comments on commit 16e9a6c

Please sign in to comment.