2 * Copyright (c) 2010-2022 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}).
52 public class Channel {
53 public VeluxExistingScenes existingScenes = new VeluxExistingScenes();
56 private Channel channel;
58 // Constructor methods
63 * Initializes the internal data structure {@link #channel} of Velux scenes,
64 * which is publicly accessible via the method {@link #getChannel()}.
66 public VeluxBridgeScenes() {
67 logger.trace("VeluxBridgeScenes(constructor) called.");
68 channel = new Channel();
71 // Class access methods
74 * Provide access to the internal structure of scenes.
76 * @return a channel describing the overall scenes situation.
78 public Channel getChannel() {
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}.
88 * @param bridge Initialized Velux bridge (communication) handler.
89 * @return true if successful, or false otherwise.
91 public boolean getScenes(VeluxBridge bridge) {
92 logger.trace("getScenes() called.");
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());
99 VeluxScene veluxScene = new VeluxScene(scene);
100 logger.trace("getScenes() storing scene {}.", veluxScene);
101 if (!channel.existingScenes.isRegistered(veluxScene)) {
102 channel.existingScenes.register(veluxScene);
104 logger.trace("getScenes() stored scene {}.", veluxScene);
106 logger.debug("getScenes() finally has found scenes {}.", channel.existingScenes);
109 logger.trace("getScenes() finished with failure.");
115 * In case of an empty list of recognized scenes, the method will
116 * initiate a product retrieval using {@link #getScenes(VeluxBridge)}.
118 * @param bridge Initialized Velux bridge (communication) handler.
119 * @return true if at lease one scene was found, and false otherwise.
121 public boolean autoRefresh(VeluxBridge bridge) {
122 if (channel.existingScenes.getNoMembers() == 0) {
123 logger.trace("autoRefresh(): is about to fetch existing scenes.");
126 return (channel.existingScenes.getNoMembers() > 0);