]> git.basschouten.com Git - openhab-addons.git/blob
7ec76f70b7b0dab6abfe300ee7a950225acec7a8
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.velux.internal.VeluxBindingProperties;
18 import org.openhab.binding.velux.internal.bridge.VeluxBridgeRunScene;
19 import org.openhab.binding.velux.internal.handler.utils.ThingConfiguration;
20 import org.openhab.binding.velux.internal.things.VeluxProductVelocity;
21 import org.openhab.binding.velux.internal.things.VeluxScene;
22 import org.openhab.binding.velux.internal.things.VeluxScene.SceneName;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * <B>Channel-specific retrieval and modification.</B>
31  * <P>
32  * This class implements the Channel <B>action</B> of the Thing <B>scene</B> :
33  * <UL>
34  * <LI><I>Velux</I> <B>bridge</B> &rarr; <B>OpenHAB</B>:
35  * <P>
36  * Information retrieval by method {@link #handleRefresh}.</LI>
37  * </UL>
38  * <UL>
39  * <LI><B>OpenHAB</B> Event Bus &rarr; <I>Velux</I> <B>bridge</B>
40  * <P>
41  * Sending commands and value updates by method {@link #handleCommand}.</LI>
42  * </UL>
43  *
44  * @author Guenther Schreiner - Initial contribution.
45  */
46 @NonNullByDefault
47 final class ChannelSceneAction extends ChannelHandlerTemplate {
48     private static final Logger LOGGER = LoggerFactory.getLogger(ChannelSceneAction.class);
49
50     /*
51      * ************************
52      * ***** Constructors *****
53      */
54
55     // Suppress default constructor for non-instantiability
56
57     private ChannelSceneAction() {
58         throw new AssertionError();
59     }
60
61     /**
62      * Communication method to retrieve information to update the channel value.
63      *
64      * @param channelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
65      * @param channelId The same item passed as type {@link String} for which a refresh is intended.
66      * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
67      *            information for this channel.
68      * @return newState The value retrieved for the passed channel, or <I>null</I> in case if there is no (new) value.
69      */
70
71     /**
72      * Communication method to update the real world according to the passed channel value (or command).
73      *
74      * @param channelUID The item passed as type {@link ChannelUID} for which to following command is addressed to.
75      * @param channelId The same item passed as type {@link String} for which a refresh is intended.
76      * @param command The command passed as type {@link Command} for the mentioned item.
77      * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
78      *            information for this channel.
79      * @return newValue ...
80      */
81     static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
82             VeluxBridgeHandler thisBridgeHandler) {
83         LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
84         Command newValue = null;
85         do { // just for common exit
86             if (command != OnOffType.ON) {
87                 LOGGER.trace("handleCommand(): ignoring OFF command.");
88                 break;
89             }
90             if (!ThingConfiguration.exists(thisBridgeHandler, channelUID, VeluxBindingProperties.PROPERTY_SCENE_NAME)) {
91                 LOGGER.trace("handleCommand(): aborting processing as scene name is not set.");
92                 break;
93             }
94             String sceneName = (String) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
95                     VeluxBindingProperties.PROPERTY_SCENE_NAME);
96             if (!thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
97                     .isRegistered(new SceneName(sceneName))) {
98                 LOGGER.info("handleCommand({},{}): cannot activate unknown scene: {}.", channelUID.getAsString(),
99                         command, sceneName);
100                 break;
101             }
102             String velocityName = (String) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
103                     VeluxBindingProperties.PROPERTY_SCENE_VELOCITY);
104             LOGGER.debug("handleCommand(): activating known scene {}.", sceneName);
105             VeluxScene thisScene = thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
106                     .get(new SceneName(sceneName));
107             LOGGER.trace("handleCommand(): execution of scene {}.", thisScene);
108             if (VeluxProductVelocity.getByName(velocityName) == VeluxProductVelocity.UNDEFTYPE) {
109                 new VeluxBridgeRunScene().execute(thisBridgeHandler.thisBridge,
110                         thisScene.getBridgeSceneIndex().toInt());
111             } else {
112                 LOGGER.trace("handleCommand(): using velocity {}.",
113                         VeluxProductVelocity.getByName(velocityName).toString());
114                 new VeluxBridgeRunScene().execute(thisBridgeHandler.thisBridge, thisScene.getBridgeSceneIndex().toInt(),
115                         VeluxProductVelocity.getByName(velocityName).getVelocity());
116             }
117             LOGGER.trace("handleCommand(): execution of scene finished.");
118         } while (false); // common exit
119         return newValue;
120     }
121 }