]> git.basschouten.com Git - openhab-addons.git/blob
e4a5df00f32dfdc45b669b0fc5e3313c8858a740
[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.core.library.types.StringType;
17 import org.openhab.binding.velux.internal.VeluxBindingConstants;
18 import org.openhab.binding.velux.internal.bridge.common.GetDeviceStatus;
19 import org.openhab.binding.velux.internal.things.VeluxGwState;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link VeluxBridgeDeviceStatus} represents a complete set of transactions
25  * for querying device status on the <B>Velux</B> bridge.
26  * <P>
27  * It therefore provides a method
28  * <UL>
29  * <LI>{@link #retrieve} for starting the detection.
30  * <LI>{@link #getChannel} for accessing the retrieved information.
31  * </UL>
32  *
33  * @see VeluxBridgeProvider
34  *
35  * @author Guenther Schreiner - Initial contribution
36  */
37 @NonNullByDefault
38 public class VeluxBridgeDeviceStatus {
39     private final Logger logger = LoggerFactory.getLogger(VeluxBridgeDeviceStatus.class);
40
41     // Type definitions, class-internal variables
42
43     /**
44      * Bridge information consisting of:
45      * <ul>
46      * <li>{@link #isRetrieved} describing the retrieval state,
47      * <li>{@link #gwState} containing the brief gateway state,
48      * <li>{@link #gwStateDescription} containing the verbose gateway state.
49      * </ul>
50      */
51     @NonNullByDefault
52     public class Channel {
53         public boolean isRetrieved = false;
54         public StringType gwState = new StringType(VeluxBindingConstants.UNKNOWN);
55         public StringType gwStateDescription = new StringType(VeluxBindingConstants.UNKNOWN);
56     }
57
58     private Channel channel;
59
60     // Constructor methods
61
62     /**
63      * Constructor.
64      * <P>
65      * Initializes the internal data structure {@link #channel} of Velux actuators/products,
66      * which is publicly accessible via the method {@link #getChannel()}.
67      */
68     public VeluxBridgeDeviceStatus() {
69         logger.trace("VeluxBridgeDeviceStatus(constructor) called.");
70         channel = new Channel();
71     }
72
73     // Class access methods
74
75     /**
76      * Provide access to the internal structure of the device status.
77      *
78      * @return a channel describing the overall actual device status.
79      */
80     public Channel getChannel() {
81         return channel;
82     }
83
84     /**
85      * Complete workflow for retrieving the firmware version, consisting of Login into bridge, querying the firmware
86      * version and logout from bridge based on a well-prepared environment of a {@link VeluxBridgeProvider}, where the
87      * results are stored in {@link VeluxBridgeDeviceStatus#channel}.
88      *
89      * @param bridge Initialized Velux bridge handler.
90      * @return <b>channel</b> of type {@link VeluxBridgeDeviceStatus.Channel} describing the overall result of this
91      *         interaction.
92      */
93     public Channel retrieve(VeluxBridge bridge) {
94         logger.trace("retrieve() called. About to query device status.");
95         GetDeviceStatus bcp = bridge.bridgeAPI().getDeviceStatus();
96         if (bridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
97             VeluxGwState state = bcp.getState();
98             channel.gwState = new StringType(state.toString());
99             channel.gwStateDescription = new StringType(state.toDescription());
100             channel.isRetrieved = true;
101             logger.trace("retrieve() finished successfully with result {}.", state.toDescription());
102         } else {
103             channel.isRetrieved = false;
104             logger.trace("retrieve() finished with failure.");
105         }
106         return channel;
107     }
108 }