2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal;
15 import java.util.Comparator;
17 import java.util.SortedMap;
18 import java.util.TreeMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * This represents the configuration of an openHAB item that is binded to a Velux
26 * KLF200 Gateway. It contains the following information:
29 * <li><B>bindingItemType</B>
32 * {@link org.openhab.binding.velux.internal.VeluxRSBindingConfig#getBindingItemType
33 * getBindingItemType} as representation of the Velux device is filed in the Velux bridge.</li>
34 * <li><B>bindingConfig</B>
37 * {@link org.openhab.binding.velux.internal.VeluxRSBindingConfig#getBindingConfig getBindingConfig} containing the
38 * device-specific binding configuration
39 * as declared in the binding configuration (possibly adapted by preprocessing).</li>
42 * @author Guenther Schreiner - Initial contribution
45 public class VeluxRSBindingConfig extends VeluxBindingConfig {
47 private final Logger logger = LoggerFactory.getLogger(VeluxRSBindingConfig.class);
50 * The ascending sorted list of generic Objects indexed by an Integer
52 private SortedMap<Integer, String> mapAscending = new TreeMap<>(new Comparator<Integer>() {
54 public int compare(Integer o1, Integer o2) {
55 return o1.compareTo(o2);
59 * The descending sorted list of generic Objects indexed by an Integer
61 private SortedMap<Integer, String> mapDescending = new TreeMap<>(new Comparator<Integer>() {
63 public int compare(Integer o1, Integer o2) {
64 return o2.compareTo(o1);
69 * The sorted list of generic Objects indexed by an Integer
71 private Integer rollershutterLevel = 0;
73 private void veluxRollershutterBindingParser(final String channelValue) {
74 logger.debug("VeluxRollershutterBindingParser({}) called.", channelValue);
76 String[] channelValueParts = channelValue.trim().split(VeluxBindingConstants.BINDING_VALUES_SEPARATOR);
77 if ((channelValueParts.length % 2) != 0) {
78 throw new IllegalArgumentException(
79 "Velux Rollershutter binding must contain an even number of configuration parts separated by '"
80 + VeluxBindingConstants.BINDING_VALUES_SEPARATOR + "' (ignoring binding '" + channelValue
84 for (int idx = 0; idx < channelValueParts.length; idx++) {
85 logger.trace("VeluxRollershutterBindingParser() processing {}.", channelValueParts[idx]);
89 degree = Integer.parseInt(channelValueParts[idx]);
90 } catch (NumberFormatException e) {
91 throw new IllegalArgumentException(
92 "Velux Rollershutter binding must contain an even number of configuration parts separated by '"
93 + VeluxBindingConstants.BINDING_VALUES_SEPARATOR
94 + "' each consisting of a shutter level followed by a scene name (ignoring binding '"
95 + channelValue + "').");
98 mapAscending.put(degree, channelValueParts[idx]);
99 mapDescending.put(degree, channelValueParts[idx]);
101 for (Map.Entry<Integer, String> nextEntry : mapAscending.entrySet()) {
102 logger.trace("VeluxRollershutterBindingParser({},{}) processed.", nextEntry.getKey(), nextEntry.getValue());
107 * Constructor of the VeluxBindingConfig.
109 * @param bindingItemType
110 * The Velux item type {@link org.openhab.binding.velux.internal.VeluxItemType
111 * VeluxItemType} which the Velux device is filed in the Velux bridge.
112 * @param channelValue
113 * The optional configuration type of the Velux binding.
114 * @param rollershutterLevel of type Integer with current position.
116 public VeluxRSBindingConfig(VeluxItemType bindingItemType, String channelValue, Integer rollershutterLevel) {
117 super(bindingItemType, channelValue);
118 logger.trace("VeluxRSBindingConfig(constructor:{},{},{}) called.", bindingItemType, channelValue,
120 this.rollershutterLevel = rollershutterLevel;
121 veluxRollershutterBindingParser(channelValue);
125 * Constructor of the VeluxBindingConfig.
127 * @param bindingItemType
128 * The Velux item type {@link org.openhab.binding.velux.internal.VeluxItemType
129 * VeluxItemType} which the Velux device is filed in the Velux bridge.
130 * @param channelValue
131 * The optional configuration type of the Velux binding.
133 public VeluxRSBindingConfig(VeluxItemType bindingItemType, String channelValue) {
134 super(bindingItemType, channelValue);
135 logger.trace("VeluxRSBindingConfig(constructor:{},{}) called.", bindingItemType, channelValue);
136 veluxRollershutterBindingParser(channelValue);
140 * Returns the next shutter level for a DOWN command w/ adjusting the actual position.
142 * @return <b>rollershutterLevel</b> of type Integer with next position after DOWN command.
144 public Integer getNextAscendingLevel() {
145 logger.trace("getNextAscendingLevel() called.");
147 for (Map.Entry<Integer, String> nextEntry : mapAscending.entrySet()) {
148 if (nextEntry.getKey() > this.rollershutterLevel) {
149 this.rollershutterLevel = nextEntry.getKey();
153 logger.trace("getNextAscendingLevel() returning {}.", this.rollershutterLevel);
154 return this.rollershutterLevel;
158 * Returns the next shutter level for an UP command w/ adjusting the actual position.
160 * @return <b>rollershutterLevel</b> of type Integer with next position after UP command.
162 public Integer getNextDescendingLevel() {
163 logger.trace("getNextDescendingLevel() called.");
165 for (Map.Entry<Integer, String> nextEntry : mapDescending.entrySet()) {
166 if (nextEntry.getKey() < this.rollershutterLevel) {
167 this.rollershutterLevel = nextEntry.getKey();
171 logger.trace("getNextDescendingLevel() returning {}.", this.rollershutterLevel);
172 return this.rollershutterLevel;
176 * Returns the current shutter level w/o adjusting the actual positioning.
178 * @return <b>rollershutterLevel</b> of type Integer with current position.
181 public Integer getLevel() {
182 logger.trace("getLevel() returning {}.", this.rollershutterLevel);
183 return this.rollershutterLevel;
187 * Returns the scene name of the current shutter level w/o adjusting the actual positioning.
189 * @return <B>sceneName</B>
190 * A String describing the next scene.
192 public String getSceneName() {
193 return getSceneName(this.rollershutterLevel);
197 * Returns the scene name w/o adjusting the actual positioning.
200 * The shutter level is be queried.
201 * @return <B>sceneName</B>
202 * A String describing the next scene.
204 public String getSceneName(Integer level) {
205 logger.trace("getSceneName({}) called.", level);
206 logger.trace("getSceneName() returning {}.", mapDescending.get(level));
207 return mapDescending.getOrDefault(level, "null");