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