]> git.basschouten.com Git - openhab-addons.git/blob
876704a50d78ccd5398906e0d46130fae6c94c63
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.VeluxBridgeSetSceneVelocity;
19 import org.openhab.binding.velux.internal.handler.utils.ThingConfiguration;
20 import org.openhab.binding.velux.internal.things.VeluxScene;
21 import org.openhab.binding.velux.internal.things.VeluxScene.SceneName;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.types.Command;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * <B>Channel-specific retrieval and modification.</B>
30  * <P>
31  * This class implements the Channel <B>silentMode</B> of the Thing <B>scene</B> :
32  * <UL>
33  * <LI><I>Velux</I> <B>bridge</B> &rarr; <B>OpenHAB</B>:
34  * <P>
35  * Information retrieval by method {@link #handleRefresh}.</LI>
36  * </UL>
37  * <UL>
38  * <LI><B>OpenHAB</B> Event Bus &rarr; <I>Velux</I> <B>bridge</B>
39  * <P>
40  * Sending commands and value updates by method {@link #handleCommand}.</LI>
41  * </UL>
42  *
43  * @author Guenther Schreiner - Initial contribution.
44  */
45 @NonNullByDefault
46 final class ChannelSceneSilentmode extends ChannelHandlerTemplate {
47     private static final Logger LOGGER = LoggerFactory.getLogger(ChannelSceneSilentmode.class);
48
49     /*
50      * ************************
51      * ***** Constructors *****
52      */
53
54     // Suppress default constructor for non-instantiability
55
56     private ChannelSceneSilentmode() {
57         throw new AssertionError();
58     }
59
60     /**
61      * Communication method to update the real world according to the passed channel value (or command).
62      *
63      * @param channelUID The item passed as type {@link ChannelUID} for which to following command is addressed to.
64      * @param channelId The same item passed as type {@link String} for which a refresh is intended.
65      * @param command The command passed as type {@link Command} for the mentioned item.
66      * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
67      *            information for this channel.
68      * @return newValue ...
69      */
70     static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
71             VeluxBridgeHandler thisBridgeHandler) {
72         LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
73         Command newValue = null;
74         do { // just for common exit
75             assert thisBridgeHandler.bridgeParameters.scenes
76                     .getChannel().existingScenes != null : "VeluxBridgeHandler.existingScenes not initialized.";
77             if (!ThingConfiguration.exists(thisBridgeHandler, channelUID, VeluxBindingProperties.PROPERTY_SCENE_NAME)) {
78                 LOGGER.trace("handleCommand(): aborting processing as scene name is not set.");
79                 break;
80             }
81             String sceneName = (String) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
82                     VeluxBindingProperties.PROPERTY_SCENE_NAME);
83             if (!thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
84                     .isRegistered(new SceneName(sceneName))) {
85                 LOGGER.info("handleCommand({},{}): cannot modify unknown scene: {}.", channelUID.getAsString(), command,
86                         sceneName);
87                 break;
88             }
89             boolean silentMode = command.equals(OnOffType.ON);
90             LOGGER.debug("handleCommand(): setting silent mode to {}.", silentMode);
91
92             VeluxScene thisScene = thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
93                     .get(new SceneName(sceneName));
94             LOGGER.trace("handleCommand(): working on scene {}.", thisScene);
95             int sceneNumber = thisScene.getBridgeSceneIndex().toInt();
96             new VeluxBridgeSetSceneVelocity().setSilentMode(thisBridgeHandler.thisBridge, sceneNumber, silentMode);
97             LOGGER.trace("handleCommand(): change of velocity done.");
98         } while (false); // common exit
99         return newValue;
100     }
101 }