]> git.basschouten.com Git - openhab-addons.git/blob
571cf972ec6edea3baecf6b795d25860e60b6c15
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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     static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
111             VeluxBridgeHandler thisBridgeHandler) {
112         LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
113         Command newValue = null;
114         // ThingProperty sceneLevels
115         if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
116                 VeluxBindingProperties.PROPERTY_VSHUTTER_SCENELEVELS)) {
117             LOGGER.trace("handleCommand(): aborting processing as scene levels are not set.");
118             return newValue;
119         }
120         String sceneLevels = (String) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
121                 VeluxBindingProperties.PROPERTY_VSHUTTER_SCENELEVELS);
122         // ThingProperty currentLevel
123         if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
124                 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL)) {
125             LOGGER.trace("handleCommand(): aborting processing as current scene level is not set.");
126             return newValue;
127         }
128         // Don't know why OH2 returns BigDecimal.
129         BigDecimal rollershutterLevelBC = (BigDecimal) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
130                 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL);
131         int currentLevel = rollershutterLevelBC.intValue();
132         LOGGER.trace("handleCommand(): current level is {}.", currentLevel);
133
134         VeluxRSBindingConfig thisRSBindingConfig = new VeluxRSBindingConfig(VeluxItemType.VSHUTTER_POSITION,
135                 sceneLevels, currentLevel);
136
137         if ((UpDownType) command == UpDownType.UP) {
138             currentLevel = thisRSBindingConfig.getNextDescendingLevel();
139         } else if ((UpDownType) command == UpDownType.DOWN) {
140             currentLevel = thisRSBindingConfig.getNextAscendingLevel();
141         } else {
142             LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
143             return newValue;
144         }
145         LOGGER.trace("handleCommand(): next level is {}.", currentLevel);
146         String sceneName = thisRSBindingConfig.getSceneName();
147         LOGGER.trace("handleCommand(): scene name is {}.", sceneName);
148         VeluxScene thisScene2 = thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
149                 .get(new SceneName(sceneName));
150         if (VeluxScene.UNKNOWN.equals(thisScene2)) {
151             LOGGER.warn(
152                     "handleCommand(): aborting command as scene with name {} is not registered; please check your KLF scene definitions.",
153                     sceneName);
154             return newValue;
155         }
156         newValue = new PercentType(currentLevel);
157         LOGGER.trace("handleCommand(): executing scene {} with index {}.", thisScene2,
158                 thisScene2.getBridgeSceneIndex().toInt());
159         new VeluxBridgeRunScene().execute(thisBridgeHandler.thisBridge, thisScene2.getBridgeSceneIndex().toInt());
160
161         LOGGER.trace("handleCommand(): updating level to {}.", currentLevel);
162         ThingProperty.setValue(thisBridgeHandler, channelUID, VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL,
163                 thisRSBindingConfig.getLevel().toString());
164         LOGGER.trace("handleCommand() returns {}.", newValue);
165         return newValue;
166     }
167 }