]> git.basschouten.com Git - openhab-addons.git/blob
e5def170ec70772fa7550ac948f1a2915e3495ec
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.velux.internal;
14
15 import java.util.Comparator;
16 import java.util.Map;
17 import java.util.SortedMap;
18 import java.util.TreeMap;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * This represents the configuration of an openHAB item that is binded to a Velux
26  * KLF200 Gateway. It contains the following information:
27  *
28  * <ul>
29  * <li><B>bindingItemType</B>
30  * <P>
31  * accessable via
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>
35  * <P>
36  * accessable via
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>
40  * </ul>
41  *
42  * @author Guenther Schreiner - Initial contribution
43  */
44 @NonNullByDefault
45 public class VeluxRSBindingConfig extends VeluxBindingConfig {
46
47     private final Logger logger = LoggerFactory.getLogger(VeluxRSBindingConfig.class);
48
49     /**
50      * The ascending sorted list of generic Objects indexed by an Integer
51      */
52     private SortedMap<Integer, String> mapAscending = new TreeMap<>(new Comparator<Integer>() {
53         @Override
54         public int compare(Integer o1, Integer o2) {
55             return o1.compareTo(o2);
56         }
57     });
58     /**
59      * The descending sorted list of generic Objects indexed by an Integer
60      */
61     private SortedMap<Integer, String> mapDescending = new TreeMap<>(new Comparator<Integer>() {
62         @Override
63         public int compare(Integer o1, Integer o2) {
64             return o2.compareTo(o1);
65         }
66     });
67
68     /**
69      * The sorted list of generic Objects indexed by an Integer
70      */
71     private Integer rollershutterLevel = 0;
72
73     private void veluxRollershutterBindingParser(final String channelValue) {
74         logger.debug("VeluxRollershutterBindingParser({}) called.", channelValue);
75
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
81                             + "').");
82         }
83
84         for (int idx = 0; idx < channelValueParts.length; idx++) {
85             logger.trace("VeluxRollershutterBindingParser() processing {}.", channelValueParts[idx]);
86
87             int degree;
88             try {
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 + "').");
96             }
97             idx++;
98             mapAscending.put(degree, channelValueParts[idx]);
99             mapDescending.put(degree, channelValueParts[idx]);
100         }
101         for (Map.Entry<Integer, String> nextEntry : mapAscending.entrySet()) {
102             logger.trace("VeluxRollershutterBindingParser({},{}) processed.", nextEntry.getKey(), nextEntry.getValue());
103         }
104     }
105
106     /**
107      * Constructor of the VeluxBindingConfig.
108      *
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.
115      */
116     public VeluxRSBindingConfig(VeluxItemType bindingItemType, String channelValue, Integer rollershutterLevel) {
117         super(bindingItemType, channelValue);
118         logger.trace("VeluxRSBindingConfig(constructor:{},{},{}) called.", bindingItemType, channelValue,
119                 rollershutterLevel);
120         this.rollershutterLevel = rollershutterLevel;
121         veluxRollershutterBindingParser(channelValue);
122     }
123
124     /**
125      * Constructor of the VeluxBindingConfig.
126      *
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.
132      */
133     public VeluxRSBindingConfig(VeluxItemType bindingItemType, String channelValue) {
134         super(bindingItemType, channelValue);
135         logger.trace("VeluxRSBindingConfig(constructor:{},{}) called.", bindingItemType, channelValue);
136         veluxRollershutterBindingParser(channelValue);
137     }
138
139     /**
140      * Returns the next shutter level for a DOWN command w/ adjusting the actual position.
141      *
142      * @return <b>rollershutterLevel</b> of type Integer with next position after DOWN command.
143      */
144     public Integer getNextAscendingLevel() {
145         logger.trace("getNextAscendingLevel() called.");
146
147         for (Map.Entry<Integer, String> nextEntry : mapAscending.entrySet()) {
148             if (nextEntry.getKey() > this.rollershutterLevel) {
149                 this.rollershutterLevel = nextEntry.getKey();
150                 break;
151             }
152         }
153         logger.trace("getNextAscendingLevel() returning {}.", this.rollershutterLevel);
154         return this.rollershutterLevel;
155     }
156
157     /**
158      * Returns the next shutter level for an UP command w/ adjusting the actual position.
159      *
160      * @return <b>rollershutterLevel</b> of type Integer with next position after UP command.
161      */
162     public Integer getNextDescendingLevel() {
163         logger.trace("getNextDescendingLevel() called.");
164
165         for (Map.Entry<Integer, String> nextEntry : mapDescending.entrySet()) {
166             if (nextEntry.getKey() < this.rollershutterLevel) {
167                 this.rollershutterLevel = nextEntry.getKey();
168                 break;
169             }
170         }
171         logger.trace("getNextDescendingLevel() returning {}.", this.rollershutterLevel);
172         return this.rollershutterLevel;
173     }
174
175     /**
176      * Returns the current shutter level w/o adjusting the actual positioning.
177      *
178      * @return <b>rollershutterLevel</b> of type Integer with current position.
179      *
180      */
181     public Integer getLevel() {
182         logger.trace("getLevel() returning {}.", this.rollershutterLevel);
183         return this.rollershutterLevel;
184     }
185
186     /**
187      * Returns the scene name of the current shutter level w/o adjusting the actual positioning.
188      *
189      * @return <B>sceneName</B>
190      *         A String describing the next scene.
191      */
192     public String getSceneName() {
193         return getSceneName(this.rollershutterLevel);
194     }
195
196     /**
197      * Returns the scene name w/o adjusting the actual positioning.
198      *
199      * @param level
200      *            The shutter level is be queried.
201      * @return <B>sceneName</B>
202      *         A String describing the next scene.
203      */
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");
208     }
209 }