]> git.basschouten.com Git - openhab-addons.git/blob
445a37d320902103545a3178deabae9f28683639
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.handler;
14
15 import static org.openhab.binding.velux.internal.VeluxBindingConstants.CHANNEL_VSHUTTER_POSITION;
16
17 import java.math.BigDecimal;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.velux.internal.VeluxBindingProperties;
22 import org.openhab.binding.velux.internal.VeluxItemType;
23 import org.openhab.binding.velux.internal.VeluxRSBindingConfig;
24 import org.openhab.binding.velux.internal.bridge.VeluxBridgeRunScene;
25 import org.openhab.binding.velux.internal.handler.utils.ThingConfiguration;
26 import org.openhab.binding.velux.internal.handler.utils.ThingProperty;
27 import org.openhab.binding.velux.internal.things.VeluxScene;
28 import org.openhab.binding.velux.internal.things.VeluxScene.SceneName;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.library.types.UpDownType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * <B>Channel-specific retrieval and modification.</B>
39  * <P>
40  * This class implements the Channel <B>position</B> of the Thing <B>vshutter</B> :
41  * <UL>
42  * <LI><I>Velux</I> <B>bridge</B> &rarr; <B>OpenHAB</B>:
43  * <P>
44  * Information retrieval by method {@link #handleRefresh}.</LI>
45  * </UL>
46  * <UL>
47  * <LI><B>OpenHAB</B> Event Bus &rarr; <I>Velux</I> <B>bridge</B>
48  * <P>
49  * Sending commands and value updates by method {@link #handleCommand}.</LI>
50  * </UL>
51  *
52  * @author Guenther Schreiner - Initial contribution.
53  */
54 @NonNullByDefault
55 final class ChannelVShutterPosition extends ChannelHandlerTemplate {
56     private static final Logger LOGGER = LoggerFactory.getLogger(ChannelVShutterPosition.class);
57
58     /*
59      * ************************
60      * ***** Constructors *****
61      */
62
63     // Suppress default constructor for non-instantiability
64
65     private ChannelVShutterPosition() {
66         throw new AssertionError();
67     }
68
69     /**
70      * Communication method to retrieve information to update the channel value.
71      *
72      * @param channelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
73      * @param channelId The same item passed as type {@link String} for which a refresh is intended.
74      * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
75      *            information for this channel.
76      * @return newState The value retrieved for the passed channel, or <I>null</I> in case if there is no (new) value.
77      */
78     static @Nullable State handleRefresh(ChannelUID channelUID, String channelId,
79             VeluxBridgeHandler thisBridgeHandler) {
80         LOGGER.debug("handleRefresh({},{},{}) called.", channelUID, channelId, thisBridgeHandler);
81         assert (channelId == CHANNEL_VSHUTTER_POSITION);
82         State newState = null;
83         do { // just for common exit
84             if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
85                     VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL)) {
86                 LOGGER.trace("handleRefresh(): aborting processing as current scene level is not set.");
87                 break;
88             }
89             // Don't know why OH2 returns BigDecimal.
90             BigDecimal rollershutterLevelBC = (BigDecimal) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
91                     VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL);
92             int rollershutterLevel = rollershutterLevelBC.intValue();
93             LOGGER.trace("handleRefresh(): current level is {}.", rollershutterLevel);
94             newState = new PercentType(rollershutterLevel);
95         } while (false); // common exit
96         LOGGER.trace("handleRefresh() returns {}.", newState);
97         return newState;
98     }
99
100     /**
101      * Communication method to update the real world according to the passed channel value (or command).
102      *
103      * @param channelUID The item passed as type {@link ChannelUID} for which to following command is addressed to.
104      * @param channelId The same item passed as type {@link String} for which a refresh is intended.
105      * @param command The command passed as type {@link Command} for the mentioned item.
106      * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
107      *            information for this channel.
108      * @return newValue ...
109      */
110     @SuppressWarnings("PMD.CompareObjectsWithEquals")
111     static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
112             VeluxBridgeHandler thisBridgeHandler) {
113         LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
114         Command newValue = null;
115         // ThingProperty sceneLevels
116         if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
117                 VeluxBindingProperties.PROPERTY_VSHUTTER_SCENELEVELS)) {
118             LOGGER.trace("handleCommand(): aborting processing as scene levels are not set.");
119             return newValue;
120         }
121         String sceneLevels = (String) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
122                 VeluxBindingProperties.PROPERTY_VSHUTTER_SCENELEVELS);
123         // ThingProperty currentLevel
124         if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
125                 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL)) {
126             LOGGER.trace("handleCommand(): aborting processing as current scene level is not set.");
127             return newValue;
128         }
129         // Don't know why OH2 returns BigDecimal.
130         BigDecimal rollershutterLevelBC = (BigDecimal) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
131                 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL);
132         int currentLevel = rollershutterLevelBC.intValue();
133         LOGGER.trace("handleCommand(): current level is {}.", currentLevel);
134
135         VeluxRSBindingConfig thisRSBindingConfig = new VeluxRSBindingConfig(VeluxItemType.VSHUTTER_POSITION,
136                 sceneLevels, currentLevel);
137
138         if ((UpDownType) command == UpDownType.UP) {
139             currentLevel = thisRSBindingConfig.getNextDescendingLevel();
140         } else if ((UpDownType) command == UpDownType.DOWN) {
141             currentLevel = thisRSBindingConfig.getNextAscendingLevel();
142         } else {
143             LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
144             return newValue;
145         }
146         LOGGER.trace("handleCommand(): next level is {}.", currentLevel);
147         String sceneName = thisRSBindingConfig.getSceneName();
148         LOGGER.trace("handleCommand(): scene name is {}.", sceneName);
149         VeluxScene thisScene2 = thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
150                 .get(new SceneName(sceneName));
151         if (thisScene2 == VeluxScene.UNKNOWN) {
152             LOGGER.warn(
153                     "handleCommand(): aborting command as scene with name {} is not registered; please check your KLF scene definitions.",
154                     sceneName);
155             return newValue;
156         }
157         newValue = new PercentType(currentLevel);
158         LOGGER.trace("handleCommand(): executing scene {} with index {}.", thisScene2,
159                 thisScene2.getBridgeSceneIndex().toInt());
160         new VeluxBridgeRunScene().execute(thisBridgeHandler.thisBridge, thisScene2.getBridgeSceneIndex().toInt());
161
162         LOGGER.trace("handleCommand(): updating level to {}.", currentLevel);
163         ThingProperty.setValue(thisBridgeHandler, channelUID, VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL,
164                 thisRSBindingConfig.getLevel().toString());
165         LOGGER.trace("handleCommand() returns {}.", newValue);
166         return newValue;
167     }
168 }