]> git.basschouten.com Git - openhab-addons.git/blob
b483c9ff8fd13eab5cdf0e502ade846ee7c0410d
[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;
14
15 import static org.apache.commons.lang.StringUtils.isNotBlank;
16
17 import java.lang.reflect.Field;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.velux.internal.config.VeluxBridgeConfiguration;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /***
26  * <B>Class for Velux binding which validates the bridge configuration parameters.</B>
27  *
28  * <ul>
29  * <li>{@link #VeluxBinding constructor}</li>
30  * <li>{@link #checked }</li>
31  * </ul>
32  *
33  * @author Guenther Schreiner - Initial contribution
34  * @author Joachim Sauer (@Saua) - fix for isBulkRetrievalEnabled, isSequentialEnforced
35  */
36 @NonNullByDefault
37 public class VeluxBinding extends VeluxBridgeConfiguration {
38     private final Logger logger = LoggerFactory.getLogger(getClass());
39
40     /***
41      *** Startup methods
42      ***/
43
44     /**
45      * Constructor
46      *
47      * initializes the interface towards the Velux bridge. Furthermore, the checked configuration can be retrieved by
48      * the method {@link #checked checked}.
49      *
50      * @param uncheckedConfiguration
51      *            The configuration of type {@link VeluxBridgeConfiguration}
52      *            which shall be checked.
53      */
54     public VeluxBinding(@Nullable VeluxBridgeConfiguration uncheckedConfiguration) {
55         logger.trace("VeluxBinding(constructor) called.");
56         if (logger.isTraceEnabled()) {
57             for (Field field : VeluxBridgeConfiguration.class.getFields()) {
58                 String fName = field.getName();
59                 if ((fName.length() > 0) && Character.isUpperCase(fName.charAt(0))) {
60                     logger.trace("VeluxBinding(): FYI: a potential configuration string is '{}'.", fName);
61                 }
62             }
63         }
64         if (uncheckedConfiguration == null) {
65             logger.debug("No configuration found, using default values.");
66         } else {
67             logger.trace("VeluxBinding(): checking {}.", VeluxBridgeConfiguration.BRIDGE_PROTOCOL);
68             if (isNotBlank(uncheckedConfiguration.protocol)) {
69                 this.protocol = uncheckedConfiguration.protocol;
70             }
71             logger.trace("VeluxBinding(): checking {}.", VeluxBridgeConfiguration.BRIDGE_IPADDRESS);
72             if (isNotBlank(uncheckedConfiguration.ipAddress)) {
73                 this.ipAddress = uncheckedConfiguration.ipAddress;
74             }
75             logger.trace("VeluxBinding(): checking {}.", VeluxBridgeConfiguration.BRIDGE_TCPPORT);
76             if ((uncheckedConfiguration.tcpPort > 0) && (uncheckedConfiguration.tcpPort <= 65535)) {
77                 this.tcpPort = uncheckedConfiguration.tcpPort;
78             }
79             logger.trace("VeluxBinding(): checking {}.", VeluxBridgeConfiguration.BRIDGE_PASSWORD);
80             if (isNotBlank(uncheckedConfiguration.password)) {
81                 this.password = uncheckedConfiguration.password;
82             }
83             logger.trace("VeluxBinding(): checking {}.", VeluxBridgeConfiguration.BRIDGE_TIMEOUT_MSECS);
84             if ((uncheckedConfiguration.timeoutMsecs > 0) && (uncheckedConfiguration.timeoutMsecs <= 10000)) {
85                 this.timeoutMsecs = uncheckedConfiguration.timeoutMsecs;
86             }
87             logger.trace("VeluxBinding(): checking {}.", VeluxBridgeConfiguration.BRIDGE_RETRIES);
88             if ((uncheckedConfiguration.retries >= 0) && (uncheckedConfiguration.retries <= 10)) {
89                 this.retries = uncheckedConfiguration.retries;
90             }
91             logger.trace("VeluxBinding(): checking {}.", VeluxBridgeConfiguration.BRIDGE_REFRESH_MSECS);
92             if ((uncheckedConfiguration.refreshMSecs > 0) && (uncheckedConfiguration.refreshMSecs <= 10000)) {
93                 this.refreshMSecs = uncheckedConfiguration.refreshMSecs;
94             }
95             this.isBulkRetrievalEnabled = uncheckedConfiguration.isBulkRetrievalEnabled;
96             this.isSequentialEnforced = uncheckedConfiguration.isSequentialEnforced;
97             this.isProtocolTraceEnabled = uncheckedConfiguration.isProtocolTraceEnabled;
98
99         }
100         logger.trace("VeluxBinding(constructor) done.");
101     }
102
103     /**
104      * Access method returning a validated configuration.
105      *
106      * @return bridgeConfiguration of type {@link VeluxBridgeConfiguration
107      *         VeluxBridgeConfiguration}.
108      */
109     public VeluxBridgeConfiguration checked() {
110         logger.trace("checked() called.");
111         logger.debug("{}Config[{}={},{}={},{}={},{}={},{}={},{}={},{}={},{}={},{}={},{}={}]",
112                 VeluxBindingConstants.BINDING_ID, VeluxBridgeConfiguration.BRIDGE_PROTOCOL, protocol,
113                 VeluxBridgeConfiguration.BRIDGE_IPADDRESS, this.ipAddress, VeluxBridgeConfiguration.BRIDGE_TCPPORT,
114                 tcpPort, VeluxBridgeConfiguration.BRIDGE_PASSWORD, password.replaceAll(".", "*"),
115                 VeluxBridgeConfiguration.BRIDGE_TIMEOUT_MSECS, timeoutMsecs, VeluxBridgeConfiguration.BRIDGE_RETRIES,
116                 retries, VeluxBridgeConfiguration.BRIDGE_REFRESH_MSECS, refreshMSecs,
117                 VeluxBridgeConfiguration.BRIDGE_IS_BULK_RETRIEVAL_ENABLED, isBulkRetrievalEnabled,
118                 VeluxBridgeConfiguration.BRIDGE_IS_SEQUENTIAL_ENFORCED, isSequentialEnforced,
119                 VeluxBridgeConfiguration.BRIDGE_PROTOCOL_TRACE_ENABLED, isProtocolTraceEnabled);
120         logger.trace("checked() done.");
121         return this;
122     }
123 }