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