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