2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.knx.internal.handler;
15 import java.util.concurrent.Future;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.knx.internal.client.KNXClient;
20 import org.openhab.binding.knx.internal.client.NoOpClient;
21 import org.openhab.binding.knx.internal.client.SerialClient;
22 import org.openhab.binding.knx.internal.config.SerialBridgeConfiguration;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.ThingStatus;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * The {@link IPBridgeThingHandler} is responsible for handling commands, which are
30 * sent to one of the channels. It implements a KNX Serial/USB Gateway, that either acts as a
31 * conduit for other {@link DeviceThingHandler}s, or for Channels that are
32 * directly defined on the bridge
34 * @author Karel Goderis - Initial contribution
35 * @author Simon Kaufmann - Refactoring and cleanup
38 public class SerialBridgeThingHandler extends KNXBridgeBaseThingHandler {
40 private @Nullable SerialClient client = null;
41 private @Nullable Future<?> initJob = null;
43 private final Logger logger = LoggerFactory.getLogger(SerialBridgeThingHandler.class);
45 public SerialBridgeThingHandler(Bridge bridge) {
50 public void initialize() {
51 // create new instance using current configuration settings;
52 // when a parameter change is done from UI, dispose() and initialize() are called
53 SerialBridgeConfiguration config = getConfigAs(SerialBridgeConfiguration.class);
54 client = new SerialClient(config.getAutoReconnectPeriod(), thing.getUID(), config.getResponseTimeout(),
55 config.getReadingPause(), config.getReadRetriesLimit(), getScheduler(), config.getSerialPort(),
56 config.useCemi(), this);
58 updateStatus(ThingStatus.UNKNOWN);
59 // delay actual initialization, allow for longer runtime of actual initialization
60 initJob = scheduler.submit(this::initializeLater);
63 public void initializeLater() {
64 final var tmpClient = client;
65 if (tmpClient != null) {
66 tmpClient.initialize();
71 public void dispose() {
72 final var tmpInitJob = initJob;
73 if (tmpInitJob != null) {
74 if (!tmpInitJob.isDone()) {
75 logger.trace("Bridge {}, shutdown during init, trying to cancel", thing.getUID());
76 tmpInitJob.cancel(true);
79 } catch (InterruptedException e) {
80 logger.trace("Bridge {}, cancellation interrupted", thing.getUID());
86 final var tmpClient = client;
87 if (tmpClient != null) {
95 protected KNXClient getClient() {
96 KNXClient ret = client;
98 return new NoOpClient();