]> git.basschouten.com Git - openhab-addons.git/blob
b3a69406aa22f5bde0cfd884eac6e707fab98e54
[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.knx.internal.client;
14
15 import java.util.Enumeration;
16 import java.util.concurrent.ScheduledExecutorService;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.openhab.core.thing.ThingUID;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import gnu.io.CommPortIdentifier;
24 import gnu.io.RXTXVersion;
25 import tuwien.auto.calimero.KNXException;
26 import tuwien.auto.calimero.link.KNXNetworkLink;
27 import tuwien.auto.calimero.link.KNXNetworkLinkFT12;
28 import tuwien.auto.calimero.link.medium.TPSettings;
29
30 /**
31  * Serial specific {@link AbstractKNXClient} implementation.
32  *
33  * @author Simon Kaufmann - initial contribution and API.
34  *
35  */
36 public class SerialClient extends AbstractKNXClient {
37
38     private final Logger logger = LoggerFactory.getLogger(SerialClient.class);
39
40     private final String serialPort;
41
42     public SerialClient(int autoReconnectPeriod, ThingUID thingUID, int responseTimeout, int readingPause,
43             int readRetriesLimit, ScheduledExecutorService knxScheduler, String serialPort,
44             StatusUpdateCallback statusUpdateCallback) {
45         super(autoReconnectPeriod, thingUID, responseTimeout, readingPause, readRetriesLimit, knxScheduler,
46                 statusUpdateCallback);
47         this.serialPort = serialPort;
48     }
49
50     @Override
51     protected @NonNull KNXNetworkLink establishConnection() throws KNXException, InterruptedException {
52         try {
53             RXTXVersion.getVersion();
54             logger.debug("Establishing connection to KNX bus through FT1.2 on serial port {}.", serialPort);
55             return new KNXNetworkLinkFT12(serialPort, new TPSettings());
56
57         } catch (NoClassDefFoundError e) {
58             throw new KNXException(
59                     "The serial FT1.2 KNX connection requires the RXTX libraries to be available, but they could not be found!",
60                     e);
61         } catch (KNXException e) {
62             if (e.getMessage().startsWith("can not open serial port")) {
63                 StringBuilder sb = new StringBuilder("Available ports are:\n");
64                 Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
65                 while (portList.hasMoreElements()) {
66                     CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
67                     if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
68                         sb.append(id.getName());
69                         sb.append("\n");
70                     }
71                 }
72                 sb.deleteCharAt(sb.length() - 1);
73                 throw new KNXException("Serial port '" + serialPort + "' could not be opened. " + sb.toString());
74             } else {
75                 throw e;
76             }
77         }
78     }
79 }