]> git.basschouten.com Git - openhab-addons.git/blob
24a5d6f5f25df1692c56f2d59cde16402a597749
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.concurrent.ScheduledExecutorService;
16 import java.util.stream.Collectors;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
20 import org.openhab.core.io.transport.serial.SerialPortManager;
21 import org.openhab.core.thing.ThingUID;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
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 static final String CALIMERO_ERROR_CANNOT_OPEN_PORT = "failed to open serial port";
40
41     private final Logger logger = LoggerFactory.getLogger(SerialClient.class);
42
43     private final SerialPortManager serialPortManager;
44     private final String serialPort;
45     private final boolean useCemi;
46
47     public SerialClient(int autoReconnectPeriod, ThingUID thingUID, int responseTimeout, int readingPause,
48             int readRetriesLimit, ScheduledExecutorService knxScheduler, String serialPort, boolean useCemi,
49             SerialPortManager serialPortManager, StatusUpdateCallback statusUpdateCallback) {
50         super(autoReconnectPeriod, thingUID, responseTimeout, readingPause, readRetriesLimit, knxScheduler,
51                 statusUpdateCallback);
52         this.serialPortManager = serialPortManager;
53         this.serialPort = serialPort;
54         this.useCemi = useCemi;
55     }
56
57     @Override
58     protected KNXNetworkLink establishConnection() throws KNXException, InterruptedException {
59         try {
60             logger.debug("Establishing connection to KNX bus through FT1.2 on serial port {}{}.", serialPort,
61                     (useCemi ? " using CEMI" : ""));
62             // CEMI support by Calimero library, userful for newer serial devices like KNX RF sticks, kBerry,
63             // etc.; default is still old EMI frame format
64             if (useCemi) {
65                 return KNXNetworkLinkFT12.newCemiLink(serialPort, new TPSettings());
66             }
67             return new KNXNetworkLinkFT12(serialPort, new TPSettings());
68
69         } catch (NoClassDefFoundError e) {
70             throw new KNXException(
71                     "The serial FT1.2 KNX connection requires the RXTX libraries to be available, but they could not be found!",
72                     e);
73         } catch (KNXException e) {
74             final String msg = e.getMessage();
75             // TODO add a test for this string match; error message might change in later version of Calimero library
76             if ((msg != null) && (msg.startsWith(CALIMERO_ERROR_CANNOT_OPEN_PORT))) {
77                 String availablePorts = serialPortManager.getIdentifiers().map(SerialPortIdentifier::getName)
78                         .collect(Collectors.joining("\n"));
79                 if (!availablePorts.isEmpty()) {
80                     availablePorts = " Available ports are:\n" + availablePorts;
81                 }
82                 throw new KNXException("Serial port '" + serialPort + "' could not be opened." + availablePorts);
83             } else {
84                 throw e;
85             }
86         }
87     }
88 }