]> git.basschouten.com Git - openhab-addons.git/blob
ae82277c197287cdb08a1f7f9e39e0be5abdccb0
[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.bridge;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.velux.internal.bridge.common.GetScenes;
17 import org.openhab.binding.velux.internal.things.VeluxExistingScenes;
18 import org.openhab.binding.velux.internal.things.VeluxScene;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * The {@link VeluxBridgeScenes} represents a complete set of transactions
24  * for retrieving of any available scenes into a structure {@link VeluxExistingScenes}
25  * defined on the <B>Velux</B> bridge.
26  * <P>
27  * It provides the following methods:
28  * <UL>
29  * <LI>{@link #getScenes} for retrieval of information.</LI>
30  * <LI>{@link #getChannel} for accessing the retrieved information.</LI>
31  * <LI>{@link #autoRefresh} for retrieval of information in case of an empty list of actuators.</LI>
32  * </UL>
33  *
34  * @see VeluxScene
35  * @see VeluxExistingScenes
36  * @see VeluxBridgeProvider
37  *
38  * @author Guenther Schreiner - Initial contribution
39  */
40 @NonNullByDefault
41 public class VeluxBridgeScenes {
42     private final Logger logger = LoggerFactory.getLogger(VeluxBridgeScenes.class);
43
44     // Type definitions, class-internal variables
45
46     /**
47      * Actuator information consisting of:
48      * <ul>
49      * <li>existingScenes ({@link VeluxExistingScenes}).
50      * </ul>
51      */
52     @NonNullByDefault
53     public class Channel {
54         public VeluxExistingScenes existingScenes = new VeluxExistingScenes();
55     }
56
57     private Channel channel;
58
59     // Constructor methods
60
61     /**
62      * Constructor.
63      * <P>
64      * Initializes the internal data structure {@link #channel} of Velux scenes,
65      * which is publicly accessible via the method {@link #getChannel()}.
66      */
67     public VeluxBridgeScenes() {
68         logger.trace("VeluxBridgeScenes(constructor) called.");
69         channel = new Channel();
70     }
71
72     // Class access methods
73
74     /**
75      * Provide access to the internal structure of scenes.
76      *
77      * @return a channel describing the overall scenes situation.
78      */
79     public Channel getChannel() {
80         return channel;
81     }
82
83     /**
84      * Login into bridge, retrieve all scenes and logout from bridge based
85      * on a well-prepared environment of a {@link VeluxBridgeProvider}. The results
86      * are stored within a public structure {@link org.openhab.binding.velux.internal.things.VeluxExistingScenes
87      * VeluxExistingScenes}.
88      *
89      * @param bridge Initialized Velux bridge (communication) handler.
90      * @return true if successful, or false otherwise.
91      */
92     public boolean getScenes(VeluxBridge bridge) {
93         logger.trace("getScenes() called.");
94
95         GetScenes bcp = bridge.bridgeAPI().getScenes();
96         if (bridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
97             for (VeluxScene scene : bcp.getScenes()) {
98                 logger.trace("getScenes() found scene {}.", scene.toString());
99
100                 VeluxScene veluxScene = new VeluxScene(scene);
101                 logger.trace("getScenes() storing scene {}.", veluxScene);
102                 if (!channel.existingScenes.isRegistered(veluxScene)) {
103                     channel.existingScenes.register(veluxScene);
104                 }
105                 logger.trace("getScenes() stored scene {}.", veluxScene);
106             }
107             logger.debug("getScenes() finally has found scenes {}.", channel.existingScenes);
108             return true;
109         } else {
110             logger.trace("getScenes() finished with failure.");
111             return false;
112         }
113     }
114
115     /**
116      * In case of an empty list of recognized scenes, the method will
117      * initiate a product retrieval using {@link #getScenes(VeluxBridge)}.
118      *
119      * @param bridge Initialized Velux bridge (communication) handler.
120      * @return true if at lease one scene was found, and false otherwise.
121      */
122     public boolean autoRefresh(VeluxBridge bridge) {
123         if (channel.existingScenes.getNoMembers() == 0) {
124             logger.trace("autoRefresh(): is about to fetch existing scenes.");
125             getScenes(bridge);
126         }
127         return (channel.existingScenes.getNoMembers() > 0);
128     }
129 }