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