]> git.basschouten.com Git - openhab-addons.git/blob
7cf595efae882d578f2db9bcbfa6395bc1b8ddd6
[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.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     public class Channel {
53         public VeluxExistingScenes existingScenes = new VeluxExistingScenes();
54     }
55
56     private Channel channel;
57
58     // Constructor methods
59
60     /**
61      * Constructor.
62      * <P>
63      * Initializes the internal data structure {@link #channel} of Velux scenes,
64      * which is publicly accessible via the method {@link #getChannel()}.
65      */
66     public VeluxBridgeScenes() {
67         logger.trace("VeluxBridgeScenes(constructor) called.");
68         channel = new Channel();
69     }
70
71     // Class access methods
72
73     /**
74      * Provide access to the internal structure of scenes.
75      *
76      * @return a channel describing the overall scenes situation.
77      */
78     public Channel getChannel() {
79         return channel;
80     }
81
82     /**
83      * Login into bridge, retrieve all scenes and logout from bridge based
84      * on a well-prepared environment of a {@link VeluxBridgeProvider}. The results
85      * are stored within a public structure {@link org.openhab.binding.velux.internal.things.VeluxExistingScenes
86      * VeluxExistingScenes}.
87      *
88      * @param bridge Initialized Velux bridge (communication) handler.
89      * @return true if successful, or false otherwise.
90      */
91     public boolean getScenes(VeluxBridge bridge) {
92         logger.trace("getScenes() called.");
93
94         GetScenes bcp = bridge.bridgeAPI().getScenes();
95         if (bridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
96             for (VeluxScene scene : bcp.getScenes()) {
97                 logger.trace("getScenes() found scene {}.", scene.toString());
98
99                 VeluxScene veluxScene = new VeluxScene(scene);
100                 logger.trace("getScenes() storing scene {}.", veluxScene);
101                 if (!channel.existingScenes.isRegistered(veluxScene)) {
102                     channel.existingScenes.register(veluxScene);
103                 }
104                 logger.trace("getScenes() stored scene {}.", veluxScene);
105             }
106             logger.debug("getScenes() finally has found scenes {}.", channel.existingScenes);
107             return true;
108         } else {
109             logger.trace("getScenes() finished with failure.");
110             return false;
111         }
112     }
113
114     /**
115      * In case of an empty list of recognized scenes, the method will
116      * initiate a product retrieval using {@link #getScenes(VeluxBridge)}.
117      *
118      * @param bridge Initialized Velux bridge (communication) handler.
119      * @return true if at lease one scene was found, and false otherwise.
120      */
121     public boolean autoRefresh(VeluxBridge bridge) {
122         if (channel.existingScenes.getNoMembers() == 0) {
123             logger.trace("autoRefresh(): is about to fetch existing scenes.");
124             getScenes(bridge);
125         }
126         return (channel.existingScenes.getNoMembers() > 0);
127     }
128 }