]> git.basschouten.com Git - openhab-addons.git/blob
2a49d5d2dbaf38fbe3e0b0abd4423ea8c28887b3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.NonNullByDefault;
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 @NonNullByDefault
37 public class SerialClient extends AbstractKNXClient {
38
39     private final Logger logger = LoggerFactory.getLogger(SerialClient.class);
40
41     private final String serialPort;
42     private final boolean useCemi;
43
44     public SerialClient(int autoReconnectPeriod, ThingUID thingUID, int responseTimeout, int readingPause,
45             int readRetriesLimit, ScheduledExecutorService knxScheduler, String serialPort, boolean useCemi,
46             StatusUpdateCallback statusUpdateCallback) {
47         super(autoReconnectPeriod, thingUID, responseTimeout, readingPause, readRetriesLimit, knxScheduler,
48                 statusUpdateCallback);
49         this.serialPort = serialPort;
50         this.useCemi = useCemi;
51     }
52
53     @Override
54     protected KNXNetworkLink establishConnection() throws KNXException, InterruptedException {
55         try {
56             RXTXVersion.getVersion();
57             logger.debug("Establishing connection to KNX bus through FT1.2 on serial port {}{}.", serialPort,
58                     (useCemi ? " using CEMI" : ""));
59             // CEMI support by Calimero library, userful for newer serial devices like KNX RF sticks, kBerry,
60             // etc.; default is still old EMI frame format
61             if (useCemi) {
62                 return KNXNetworkLinkFT12.newCemiLink(serialPort, new TPSettings());
63             }
64             return new KNXNetworkLinkFT12(serialPort, new TPSettings());
65
66         } catch (NoClassDefFoundError e) {
67             throw new KNXException(
68                     "The serial FT1.2 KNX connection requires the RXTX libraries to be available, but they could not be found!",
69                     e);
70         } catch (KNXException e) {
71             final String msg = e.getMessage();
72             if ((msg != null) && (msg.startsWith("can not open serial port"))) {
73                 StringBuilder sb = new StringBuilder("Available ports are:\n");
74                 Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
75                 while (portList.hasMoreElements()) {
76                     CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
77                     if ((id != null) && (id.getPortType() == CommPortIdentifier.PORT_SERIAL)) {
78                         sb.append(id.getName());
79                         sb.append("\n");
80                     }
81                 }
82                 sb.deleteCharAt(sb.length() - 1);
83                 throw new KNXException("Serial port '" + serialPort + "' could not be opened. " + sb.toString());
84             } else {
85                 throw e;
86             }
87         }
88     }
89 }