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