]> git.basschouten.com Git - openhab-addons.git/blob
9624a855c76389eeeaa635cd6f24aed4872ac4e6
[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.homematic.internal.common;
14
15 import java.nio.charset.Charset;
16 import java.nio.charset.StandardCharsets;
17
18 import org.openhab.binding.homematic.internal.model.HmChannel;
19 import org.openhab.binding.homematic.internal.model.HmGatewayInfo;
20 import org.openhab.binding.homematic.internal.model.HmInterface;
21
22 /**
23  * The main gateway config class.
24  *
25  * @author Gerhard Riegler - Initial contribution
26  */
27 public class HomematicConfig {
28     private static final String GATEWAY_TYPE_AUTO = "AUTO";
29     private static final String GATEWAY_TYPE_CCU = "CCU";
30     private static final String GATEWAY_TYPE_NOCCU = "NOCCU";
31
32     private static final int DEFAULT_PORT_RF = 2001;
33     private static final int DEFAULT_PORT_WIRED = 2000;
34     private static final int DEFAULT_PORT_HMIP = 2010;
35     private static final int DEFAULT_PORT_CUXD = 8701;
36     private static final int DEFAULT_PORT_GROUP = 9292;
37     public static final int DEFAULT_INSTALL_MODE_DURATION = 60;
38
39     private String gatewayAddress;
40     private String gatewayType = GATEWAY_TYPE_AUTO;
41
42     private int rfPort;
43     private int wiredPort;
44     private int hmIpPort;
45     private int cuxdPort;
46     private int groupPort;
47
48     private String callbackHost;
49     private int xmlCallbackPort;
50     private int binCallbackPort;
51
52     private int socketMaxAlive = 900;
53     private int timeout = 15;
54     private int installModeDuration = DEFAULT_INSTALL_MODE_DURATION;
55     private long discoveryTimeToLive = -1;
56     private boolean unpairOnDeletion = false;
57     private boolean factoryResetOnDeletion = false;
58     private int bufferSize = 2048;
59
60     private HmGatewayInfo gatewayInfo;
61     private int callbackRegTimeout;
62
63     /**
64      * Returns the Homematic gateway address.
65      */
66     public String getGatewayAddress() {
67         return gatewayAddress;
68     }
69
70     /**
71      * Sets the Homematic gateway address.
72      */
73     public void setGatewayAddress(String gatewayAddress) {
74         this.gatewayAddress = gatewayAddress;
75     }
76
77     /**
78      * Returns the callback host address.
79      */
80     public String getCallbackHost() {
81         return callbackHost;
82     }
83
84     /**
85      * Sets the callback host address.
86      */
87     public void setCallbackHost(String callbackHost) {
88         this.callbackHost = callbackHost;
89     }
90
91     /**
92      * Returns the XML-RPC callback host port.
93      */
94     public int getXmlCallbackPort() {
95         return xmlCallbackPort;
96     }
97
98     /**
99      * Sets the XML-RPC callback host port.
100      */
101     public void setXmlCallbackPort(int xmlCallbackPort) {
102         this.xmlCallbackPort = xmlCallbackPort;
103     }
104
105     /**
106      * Returns the BIN-RPC callback host port.
107      */
108     public int getBinCallbackPort() {
109         return binCallbackPort;
110     }
111
112     /**
113      * Sets the BIN-RPC callback host port.
114      */
115     public void setBinCallbackPort(int binCallbackPort) {
116         this.binCallbackPort = binCallbackPort;
117     }
118
119     /**
120      * Sets timeout for callback registration.
121      */
122     public void setCallbackRegTimeout(int timeout) {
123         this.callbackRegTimeout = timeout;
124     }
125
126     /**
127      * Returns timeout for callback registrations.
128      */
129     public int getCallbackRegTimeout() {
130         return callbackRegTimeout;
131     }
132
133     /**
134      * Returns the HmGatewayInfo.
135      */
136     public HmGatewayInfo getGatewayInfo() {
137         return gatewayInfo;
138     }
139
140     /**
141      * Sets the HmGatewayInfo.
142      */
143     public void setGatewayInfo(HmGatewayInfo gatewayInfo) {
144         this.gatewayInfo = gatewayInfo;
145     }
146
147     /**
148      * Returns the max alive time of a socket connection to a Homematic gateway in seconds.
149      */
150     public int getSocketMaxAlive() {
151         return socketMaxAlive;
152     }
153
154     /**
155      * Sets the max alive time of a socket connection to a Homematic gateway in seconds.
156      */
157     public void setSocketMaxAlive(int socketMaxAlive) {
158         this.socketMaxAlive = socketMaxAlive;
159     }
160
161     /**
162      * Returns the timeout for the communication to a Homematic gateway in seconds.
163      */
164     public int getTimeout() {
165         return timeout;
166     }
167
168     /**
169      * Sets the timeout for the communication to a Homematic gateway in seconds.
170      */
171     public void setTimeout(int timeout) {
172         this.timeout = timeout;
173     }
174
175     /**
176      * Returns the time to live for discovery results of a Homematic gateway in seconds.
177      */
178     public long getDiscoveryTimeToLive() {
179         return discoveryTimeToLive;
180     }
181
182     /**
183      * Sets the time to live for discovery results of a Homematic gateway in seconds.
184      */
185     public void setDiscoveryTimeToLive(long discoveryTimeToLive) {
186         this.discoveryTimeToLive = discoveryTimeToLive;
187     }
188
189     /**
190      * Returns the HmGatewayType.
191      */
192     public String getGatewayType() {
193         return gatewayType;
194     }
195
196     /**
197      * Sets the HmGatewayType.
198      */
199     public void setGatewayType(String gatewayType) {
200         this.gatewayType = gatewayType;
201     }
202
203     /**
204      * Returns time in seconds that the controller will be in install mode when
205      * a device discovery is initiated
206      *
207      * @return time in seconds that the controller remains in install mode
208      */
209     public int getInstallModeDuration() {
210         return installModeDuration;
211     }
212
213     /**
214      * Sets installModeDuration
215      *
216      * @param installModeDuration time in seconds that the controller remains in
217      *            install mode
218      */
219     public void setInstallModeDuration(int installModeDuration) {
220         this.installModeDuration = installModeDuration;
221     }
222
223     /**
224      * Returns if devices are unpaired from the gateway when their corresponding things are removed
225      *
226      * @return <i>true</i> if devices are unpaired from the gateway when their corresponding things are removed
227      */
228     public boolean isUnpairOnDeletion() {
229         return unpairOnDeletion;
230     }
231
232     /**
233      * Sets unpairOnDeletion
234      *
235      * @param unpairOnDeletion if set to <i>true</i>, devices are unpaired from the gateway when their corresponding
236      *            things are removed
237      */
238     public void setUnpairOnDeletion(boolean unpairOnDeletion) {
239         this.unpairOnDeletion = unpairOnDeletion;
240     }
241
242     /**
243      * Returns if devices are factory reset when their corresponding things are removed
244      *
245      * @return <i>true</i> if devices are factory reset when their corresponding things are removed
246      */
247     public boolean isFactoryResetOnDeletion() {
248         return factoryResetOnDeletion;
249     }
250
251     /**
252      * Sets factoryResetOnDeletion
253      *
254      * @param factoryResetOnDeletion if set to <i>true</i>, devices are factory reset when their corresponding things
255      *            are removed
256      */
257     public void setFactoryResetOnDeletion(boolean factoryResetOnDeletion) {
258         this.factoryResetOnDeletion = factoryResetOnDeletion;
259     }
260
261     /**
262      * Returns the TclRegaScript url.
263      */
264     public String getTclRegaUrl() {
265         return "http://" + gatewayAddress + ":8181/tclrega.exe";
266     }
267
268     /**
269      * Returns the Homematic gateway port of the channel.
270      */
271     public int getRpcPort(HmChannel channel) {
272         return getRpcPort(channel.getDevice().getHmInterface());
273     }
274
275     /**
276      * Returns the Homematic gateway port of the interfaces.
277      */
278     public int getRpcPort(HmInterface hmInterface) {
279         if (HmInterface.WIRED.equals(hmInterface)) {
280             return getWiredPort();
281         } else if (HmInterface.HMIP.equals(hmInterface)) {
282             return getHmIpPort();
283         } else if (HmInterface.CUXD.equals(hmInterface)) {
284             return getCuxdPort();
285         } else if (HmInterface.GROUP.equals(hmInterface)) {
286             return getGroupPort();
287         } else {
288             return getRfPort();
289         }
290     }
291
292     /**
293      * Returns the port of the RF daemon.
294      */
295     private int getRfPort() {
296         return rfPort == 0 ? DEFAULT_PORT_RF : rfPort;
297     }
298
299     /**
300      * Returns the port of the wired daemon.
301      */
302     private int getWiredPort() {
303         return wiredPort == 0 ? DEFAULT_PORT_WIRED : wiredPort;
304     }
305
306     /**
307      * Returns the port of the HmIp daemon.
308      */
309     private int getHmIpPort() {
310         return hmIpPort == 0 ? DEFAULT_PORT_HMIP : hmIpPort;
311     }
312
313     /**
314      * Returns the port of the CUxD daemon.
315      */
316     private int getCuxdPort() {
317         return cuxdPort == 0 ? DEFAULT_PORT_CUXD : cuxdPort;
318     }
319
320     /**
321      * Returns the port of the group daemon.
322      */
323     public int getGroupPort() {
324         return groupPort == 0 ? DEFAULT_PORT_GROUP : groupPort;
325     }
326
327     /**
328      * Returns true, if a wired port is configured.
329      */
330     public boolean hasWiredPort() {
331         return wiredPort != 0;
332     }
333
334     /**
335      * Returns true, if a hmIp port is configured.
336      */
337     public boolean hasHmIpPort() {
338         return hmIpPort != 0;
339     }
340
341     /**
342      * Returns true, if a cuxd port is configured.
343      */
344     public boolean hasCuxdPort() {
345         return cuxdPort != 0;
346     }
347
348     /**
349      * Returns true, if a group port is configured.
350      */
351     public boolean hasGroupPort() {
352         return groupPort != 0;
353     }
354
355     /**
356      * Returns true, if a RF port is configured.
357      */
358     public boolean hasRfPort() {
359         return rfPort != 0;
360     }
361
362     /**
363      * Returns the encoding that is suitable on requests to and responds from the Homematic gateway.
364      */
365     public Charset getEncoding() {
366         if (gatewayInfo != null && gatewayInfo.isHomegear()) {
367             return StandardCharsets.UTF_8;
368         } else {
369             return StandardCharsets.ISO_8859_1;
370         }
371     }
372
373     /**
374      * Returns the buffers size used for the communication with the Homematic gateway.
375      */
376     public int getBufferSize() {
377         return bufferSize;
378     }
379
380     /**
381      * Returns true, if the configured gatewayType is CCU.
382      */
383     public boolean isCCUType() {
384         return gatewayType.equalsIgnoreCase(HomematicConfig.GATEWAY_TYPE_CCU);
385     }
386
387     /**
388      * Returns true, if the configured gatewayType is NoCCU.
389      */
390     public boolean isNoCCUType() {
391         return gatewayType.equalsIgnoreCase(HomematicConfig.GATEWAY_TYPE_NOCCU);
392     }
393
394     @Override
395     public String toString() {
396         return String.format("""
397                 %s[gatewayAddress=%s,callbackHost=%s,xmlCallbackPort=%d,binCallbackPort=%d,\
398                 gatewayType=%s,rfPort=%d,wiredPort=%d,hmIpPort=%d,cuxdPort=%d,groupPort=%d,timeout=%d,\
399                 discoveryTimeToLive=%d,installModeDuration=%d,socketMaxAlive=%d]\
400                 """, getClass().getSimpleName(), gatewayAddress, callbackHost, xmlCallbackPort, binCallbackPort,
401                 gatewayType, getRfPort(), getWiredPort(), getHmIpPort(), getCuxdPort(), getGroupPort(), timeout,
402                 discoveryTimeToLive, installModeDuration, socketMaxAlive);
403     }
404 }