}
if (previousValue == null || previousValue.intValue() != value) {
- updateState(channelId, stateFromValue(value));
+ updateState(channelId, stateFromValue(channelId, value));
}
}
Integer digits;
if (channelId.equals(channelUID.getId())) {
- digits = valueFromCommand(command);
+ digits = valueFromCommand(channelId, command);
updateStateAndCacheValue(channelId, digits.intValue());
} else {
synchronized (cachedStates) {
}
}
- protected abstract int valueFromCommand(Command command);
+ protected abstract int valueFromCommand(String channelId, Command command);
- protected abstract State stateFromValue(int value);
+ protected abstract State stateFromValue(String channelId, int value);
}
package org.openhab.binding.nikobus.internal.handler;
import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
private final Logger logger = LoggerFactory.getLogger(NikobusRollershutterModuleHandler.class);
private final List<PositionEstimator> positionEstimators = new CopyOnWriteArrayList<>();
+ private final Map<String, DirectionConfiguration> directionConfigurations = new ConcurrentHashMap<>();
+
public NikobusRollershutterModuleHandler(Thing thing) {
super(thing);
}
}
positionEstimators.clear();
+ directionConfigurations.clear();
for (Channel channel : thing.getChannels()) {
PositionEstimatorConfig config = channel.getConfiguration().as(PositionEstimatorConfig.class);
if (config.delay >= 0 && config.duration > 0) {
positionEstimators.add(new PositionEstimator(channel.getUID(), config));
}
+
+ DirectionConfiguration configuration = config.reverse ? DirectionConfiguration.REVERSED
+ : DirectionConfiguration.NORMAL;
+ directionConfigurations.put(channel.getUID().getId(), configuration);
}
logger.debug("Position estimators for {} = {}", thing.getUID(), positionEstimators);
}
@Override
- protected int valueFromCommand(Command command) {
+ protected int valueFromCommand(String channelId, Command command) {
+ if (command == StopMoveType.STOP) {
+ return 0x00;
+ }
if (command == UpDownType.DOWN || command == StopMoveType.MOVE) {
- return 0x02;
+ return getDirectionConfiguration(channelId).down;
}
if (command == UpDownType.UP) {
- return 0x01;
+ return getDirectionConfiguration(channelId).up;
}
- if (command == StopMoveType.STOP) {
- return 0x00;
- }
-
throw new IllegalArgumentException("Command '" + command + "' not supported");
}
@Override
- protected State stateFromValue(int value) {
+ protected State stateFromValue(String channelId, int value) {
if (value == 0x00) {
return OnOffType.OFF;
}
- if (value == 0x01) {
+
+ DirectionConfiguration configuration = getDirectionConfiguration(channelId);
+ if (value == configuration.up) {
return UpDownType.UP;
}
- if (value == 0x02) {
+ if (value == configuration.down) {
return UpDownType.DOWN;
}
+
throw new IllegalArgumentException("Unexpected value " + value + " received");
}
super.updateState(channelUID, new PercentType(percent));
}
+ private DirectionConfiguration getDirectionConfiguration(String channelId) {
+ DirectionConfiguration configuration = directionConfigurations.get(channelId);
+ if (configuration == null) {
+ throw new IllegalArgumentException("Direction configuration not found for " + channelId);
+ }
+ return configuration;
+ }
+
public static class PositionEstimatorConfig {
public int duration = -1;
public int delay = 5;
+ public boolean reverse = false;
}
private class PositionEstimator {
+ delayInMillis + "ms)";
}
}
+
+ private static class DirectionConfiguration {
+ final int up;
+ final int down;
+
+ final static DirectionConfiguration NORMAL = new DirectionConfiguration(1, 2);
+ final static DirectionConfiguration REVERSED = new DirectionConfiguration(2, 1);
+
+ private DirectionConfiguration(int up, int down) {
+ this.up = up;
+ this.down = down;
+ }
+ }
}