2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal.bridge;
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;
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.
27 * It provides the following methods:
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>
35 * @see VeluxExistingScenes
36 * @see VeluxBridgeProvider
38 * @author Guenther Schreiner - Initial contribution
41 public class VeluxBridgeScenes {
42 private final Logger logger = LoggerFactory.getLogger(VeluxBridgeScenes.class);
44 // Type definitions, class-internal variables
47 * Actuator information consisting of:
49 * <li>existingScenes ({@link VeluxExistingScenes}).
53 public class Channel {
54 public VeluxExistingScenes existingScenes = new VeluxExistingScenes();
57 private Channel channel;
59 // Constructor methods
64 * Initializes the internal data structure {@link #channel} of Velux scenes,
65 * which is publicly accessible via the method {@link #getChannel()}.
67 public VeluxBridgeScenes() {
68 logger.trace("VeluxBridgeScenes(constructor) called.");
69 channel = new Channel();
72 // Class access methods
75 * Provide access to the internal structure of scenes.
77 * @return a channel describing the overall scenes situation.
79 public Channel getChannel() {
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}.
89 * @param bridge Initialized Velux bridge (communication) handler.
90 * @return true if successful, or false otherwise.
92 public boolean getScenes(VeluxBridge bridge) {
93 logger.trace("getScenes() called.");
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());
100 VeluxScene veluxScene = new VeluxScene(scene);
101 logger.trace("getScenes() storing scene {}.", veluxScene);
102 if (!channel.existingScenes.isRegistered(veluxScene)) {
103 channel.existingScenes.register(veluxScene);
105 logger.trace("getScenes() stored scene {}.", veluxScene);
107 logger.debug("getScenes() finally has found scenes {}.", channel.existingScenes);
110 logger.trace("getScenes() finished with failure.");
116 * In case of an empty list of recognized scenes, the method will
117 * initiate a product retrieval using {@link #getScenes(VeluxBridge)}.
119 * @param bridge Initialized Velux bridge (communication) handler.
120 * @return true if at lease one scene was found, and false otherwise.
122 public boolean autoRefresh(VeluxBridge bridge) {
123 if (channel.existingScenes.getNoMembers() == 0) {
124 logger.trace("autoRefresh(): is about to fetch existing scenes.");
127 return (channel.existingScenes.getNoMembers() > 0);