2 * Copyright (c) 2010-2024 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.io.transport.serial.SerialPortManager;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ThingStatus;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@link IPBridgeThingHandler} is responsible for handling commands, which are
31 * sent to one of the channels. It implements a KNX Serial/USB Gateway, that either acts as a
32 * conduit for other {@link DeviceThingHandler}s, or for Channels that are
33 * directly defined on the bridge
35 * @author Karel Goderis - Initial contribution
36 * @author Simon Kaufmann - Refactoring and cleanup
39 public class SerialBridgeThingHandler extends KNXBridgeBaseThingHandler {
41 private @Nullable SerialClient client = null;
42 private @Nullable Future<?> initJob = null;
44 private final Logger logger = LoggerFactory.getLogger(SerialBridgeThingHandler.class);
46 private final SerialPortManager serialPortManager;
48 public SerialBridgeThingHandler(Bridge bridge, final SerialPortManager serialPortManager) {
50 this.serialPortManager = serialPortManager;
54 public void initialize() {
55 // create new instance using current configuration settings;
56 // when a parameter change is done from UI, dispose() and initialize() are called
57 SerialBridgeConfiguration config = getConfigAs(SerialBridgeConfiguration.class);
58 client = new SerialClient(config.getAutoReconnectPeriod(), thing.getUID(), config.getResponseTimeout(),
59 config.getReadingPause(), config.getReadRetriesLimit(), getScheduler(), config.getSerialPort(),
60 config.useCemi(), serialPortManager, getCommandExtensionData(), this);
62 updateStatus(ThingStatus.UNKNOWN);
63 // delay actual initialization, allow for longer runtime of actual initialization
64 initJob = scheduler.submit(this::initializeLater);
67 public void initializeLater() {
68 SerialClient tmpClient = client;
69 if (tmpClient != null) {
70 tmpClient.initialize();
75 public void dispose() {
76 Future<?> tmpInitJob = initJob;
77 if (tmpInitJob != null) {
78 if (!tmpInitJob.isDone()) {
79 logger.trace("Bridge {}, shutdown during init, trying to cancel", thing.getUID());
80 tmpInitJob.cancel(true);
83 } catch (InterruptedException e) {
84 logger.trace("Bridge {}, cancellation interrupted", thing.getUID());
90 SerialClient tmpClient = client;
91 if (tmpClient != null) {
99 protected KNXClient getClient() {
100 KNXClient ret = client;
102 return new NoOpClient();