]> git.basschouten.com Git - openhab-addons.git/blob
ec934c8e1f23f0b5e9f01ad8bc1b8580cccd4410
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.VeluxBindingConstants;
17 import org.openhab.binding.velux.internal.bridge.common.GetLANConfig;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.StringType;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link VeluxBridgeLANConfig} represents a complete set of transactions
25  * for retrieving the network configuration of the <B>Velux</B> bridge.
26  * <P>
27  * It provides the following methods:
28  * <UL>
29  * <LI>{@link #retrieve} for retrieval of information.
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 VeluxBridgeLANConfig {
39     private final Logger logger = LoggerFactory.getLogger(VeluxBridgeLANConfig.class);
40
41     // Type definitions, class-internal variables
42
43     /**
44      * IP Network configuration, consisting of:
45      * <ul>
46      * <li>isRetrieved (boolean flag),
47      * <li>ipAddress,
48      * <li>subnetMask,
49      * <li>defaultGW and
50      * <li>enabledDHCP.
51      * </ul>
52      */
53     public class Channel {
54         public boolean isRetrieved = false;
55         public StringType openHABipAddress = new StringType(VeluxBindingConstants.UNKNOWN);
56         public StringType openHABsubnetMask = new StringType(VeluxBindingConstants.UNKNOWN);
57         public StringType openHABdefaultGW = new StringType(VeluxBindingConstants.UNKNOWN);
58         public OnOffType openHABenabledDHCP = OnOffType.OFF;
59     }
60
61     private Channel channel;
62
63     // Constructor methods
64
65     /**
66      * Constructor.
67      * <P>
68      * Initializes the internal data structure {@link #channel} of Velux LAN information,
69      * which is publicly accessible via the method {@link #getChannel()}.
70      */
71     public VeluxBridgeLANConfig() {
72         logger.trace("VeluxBridgeLANConfig(constructor) called.");
73         channel = new Channel();
74     }
75
76     // Class access methods
77
78     /**
79      * Provide access to the internal structure of LAN information.
80      *
81      * @return a channel describing the overall actual LAN information.
82      */
83     public Channel getChannel() {
84         return channel;
85     }
86
87     /**
88      * Complete workflow for retrieving the network configuration, consisting of Login into bridge, querying
89      * the network configuration and logout from bridge based on a well-prepared environment of a
90      * {@link VeluxBridgeProvider}, where the results are stored within as well in
91      * {@link VeluxBridgeLANConfig#channel}.
92      *
93      * @param bridge Initialized Velux bridge handler.
94      * @return <b>channel</b> of type {@link VeluxBridgeLANConfig.Channel} describing the overall result of this
95      *         interaction.
96      */
97     public Channel retrieve(VeluxBridge bridge) {
98         logger.trace("retrieve() called.");
99
100         GetLANConfig bcp = bridge.bridgeAPI().getLANConfig();
101         if (bridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
102             logger.trace("retrieve() found successfully configuration {}.", bcp.getLANConfig());
103             channel.openHABipAddress = new StringType(bcp.getLANConfig().getIpAddress());
104             channel.openHABsubnetMask = new StringType(bcp.getLANConfig().getSubnetMask());
105             channel.openHABdefaultGW = new StringType(bcp.getLANConfig().getDefaultGW());
106             channel.openHABenabledDHCP = bcp.getLANConfig().getDHCP() ? OnOffType.ON : OnOffType.OFF;
107             channel.isRetrieved = true;
108         } else {
109             channel.isRetrieved = false;
110             logger.trace("retrieve() finished with failure.");
111         }
112         return channel;
113     }
114 }