]> git.basschouten.com Git - openhab-addons.git/commitdiff
[nikobus] added option to reverse rollershutter commands (#10047)
authorBoris Krivonog <boris.krivonog@inova.si>
Thu, 4 Feb 2021 21:59:44 +0000 (22:59 +0100)
committerGitHub <noreply@github.com>
Thu, 4 Feb 2021 21:59:44 +0000 (13:59 -0800)
* Added option to reverse rollershutter commands - UP -> DOWN and DOWN -> UP - might happen if rollershuter module's output is wired differently.

* Fixed PR comments.

Signed-off-by: Boris Krivonog <boris.krivonog@inova.si>
bundles/org.openhab.binding.nikobus/README.md
bundles/org.openhab.binding.nikobus/src/main/java/org/openhab/binding/nikobus/internal/handler/NikobusDimmerModuleHandler.java
bundles/org.openhab.binding.nikobus/src/main/java/org/openhab/binding/nikobus/internal/handler/NikobusModuleHandler.java
bundles/org.openhab.binding.nikobus/src/main/java/org/openhab/binding/nikobus/internal/handler/NikobusRollershutterModuleHandler.java
bundles/org.openhab.binding.nikobus/src/main/java/org/openhab/binding/nikobus/internal/handler/NikobusSwitchModuleHandler.java
bundles/org.openhab.binding.nikobus/src/main/resources/OH-INF/config/config.xml

index b75868e5ffec503d6de14f816b3d2021506d3241..76a57cf0af855202b0da91643dd31db5e1b0d5a1 100644 (file)
@@ -131,6 +131,8 @@ Defines a `rollershutter-module` with address `4C6C`.
 | output-5  | Rollershutter | Output 5     |
 | output-6  | Rollershutter | Output 6     |
 
+In case rollershutters are moving in the oposite direction when sending `UP` or `DOWN` commands, there is a `reverse` parameter, which can be set to `true` in this case to reverse the rollershutter's direction. Defaults to `false`.
+
 ##### Estimating Position
 
 Nikobus rollershuter module does not provide information about rollershutter's position. In order to bridge this gap, an optional parameter `duration` can be set per channel, describing the amount of time needed by a rollershutter to get from open to closed state (or vice-versa).
index cf2f3cb69f4664bb97d3aa18b497a9f236130502..45fe860602f153351da054fa16d8be4f41d7df05 100644 (file)
@@ -53,16 +53,16 @@ public class NikobusDimmerModuleHandler extends NikobusSwitchModuleHandler {
     }
 
     @Override
-    protected int valueFromCommand(Command command) {
+    protected int valueFromCommand(String channelId, Command command) {
         if (command instanceof PercentType) {
             return Math.round(((PercentType) command).floatValue() / 100f * 255f);
         }
 
-        return super.valueFromCommand(command);
+        return super.valueFromCommand(channelId, command);
     }
 
     @Override
-    protected State stateFromValue(int value) {
+    protected State stateFromValue(String channelId, int value) {
         int result = Math.round(value * 100f / 255f);
         return new PercentType(result);
     }
index 07c0bddddf5490a9b7b7508c2f6cb7e542b91d9f..5a7762fba032ab08a0cb359e7f0dca9ec7d40971 100644 (file)
@@ -182,7 +182,7 @@ abstract class NikobusModuleHandler extends NikobusBaseThingHandler {
         }
 
         if (previousValue == null || previousValue.intValue() != value) {
-            updateState(channelId, stateFromValue(value));
+            updateState(channelId, stateFromValue(channelId, value));
         }
     }
 
@@ -195,7 +195,7 @@ abstract class NikobusModuleHandler extends NikobusBaseThingHandler {
             Integer digits;
 
             if (channelId.equals(channelUID.getId())) {
-                digits = valueFromCommand(command);
+                digits = valueFromCommand(channelId, command);
                 updateStateAndCacheValue(channelId, digits.intValue());
             } else {
                 synchronized (cachedStates) {
@@ -233,7 +233,7 @@ abstract class NikobusModuleHandler extends NikobusBaseThingHandler {
         }
     }
 
-    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);
 }
index 95a986c6239af7794651670c6818d8c3f1ba5aa3..9637557638564a42aeeaa85faa3d3e733194307d 100644 (file)
@@ -13,6 +13,8 @@
 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;
@@ -44,6 +46,8 @@ public class NikobusRollershutterModuleHandler extends NikobusModuleHandler {
     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);
     }
@@ -57,43 +61,50 @@ public class NikobusRollershutterModuleHandler extends NikobusModuleHandler {
         }
 
         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");
     }
 
@@ -119,9 +130,18 @@ public class NikobusRollershutterModuleHandler extends NikobusModuleHandler {
         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 {
@@ -203,4 +223,17 @@ public class NikobusRollershutterModuleHandler extends NikobusModuleHandler {
                     + 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;
+        }
+    }
 }
index d23cc467a803a28e887568dba8cd1f2829ce3174..449f2ae784c61dfcd1e2f23f7d3a4789ab029d1e 100644 (file)
@@ -30,7 +30,7 @@ public class NikobusSwitchModuleHandler extends NikobusModuleHandler {
     }
 
     @Override
-    protected int valueFromCommand(Command command) {
+    protected int valueFromCommand(String channelId, Command command) {
         if (command == OnOffType.ON) {
             return 0xff;
         }
@@ -43,7 +43,7 @@ public class NikobusSwitchModuleHandler extends NikobusModuleHandler {
     }
 
     @Override
-    protected State stateFromValue(int value) {
+    protected State stateFromValue(String channelId, int value) {
         return value != 0 ? OnOffType.ON : OnOffType.OFF;
     }
 }
index c7353d3e59d0fac9bf1cd9885fb21012888f3459..eb767784f13755df38847f72ff05905fd938fcfe 100644 (file)
                        <label>Delay</label>
                        <description>Delay specifying how many seconds after duration module's output is set to OFF. Defaults to 5 seconds</description>
                </parameter>
+               <parameter name="reverse" type="boolean">
+                       <label>Reverse Direction</label>
+                       <description>Reverse direction of rollershutters. Defaults to false</description>
+               </parameter>
        </config-description>
 
 </config-description:config-descriptions>