]> git.basschouten.com Git - openhab-addons.git/blob
c213dffc2b6f0bdf9a85a2174028da61847f33e6
[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.apache.commons.lang.StringUtils;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.velux.internal.config.VeluxBridgeConfiguration;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /***
27  * <B>Class for Velux binding which validates the bridge configuration parameters.</B>
28  *
29  * <ul>
30  * <li>{@link #VeluxBinding constructor}</li>
31  * <li>{@link #checked }</li>
32  * </ul>
33  *
34  * @author Guenther Schreiner - Initial contribution
35  * @author Joachim Sauer (@Saua) - fix for isBulkRetrievalEnabled, isSequentialEnforced
36  */
37 @NonNullByDefault
38 public class VeluxBinding extends VeluxBridgeConfiguration {
39     private final Logger logger = LoggerFactory.getLogger(getClass());
40
41     /***
42      *** Startup methods
43      ***/
44
45     /**
46      * Constructor
47      *
48      * initializes the interface towards the Velux bridge. Furthermore, the checked configuration can be retrieved by
49      * the method {@link #checked checked}.
50      *
51      * @param uncheckedConfiguration
52      *            The configuration of type {@link VeluxBridgeConfiguration}
53      *            which shall be checked.
54      */
55     public VeluxBinding(@Nullable VeluxBridgeConfiguration uncheckedConfiguration) {
56         logger.trace("VeluxBinding(constructor) called.");
57         if (logger.isTraceEnabled()) {
58             for (Field field : VeluxBridgeConfiguration.class.getFields()) {
59                 if (!StringUtils.capitalize(field.getName()).contentEquals(field.getName())) {
60                     logger.trace("VeluxBinding(): FYI: a potential configuration string is '{}'.", field.getName());
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 }