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