]> git.basschouten.com Git - openhab-addons.git/blob
5cb4692de96559195cd3cc153f0b1f7ccc8989e4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.handler;
14
15 import java.util.concurrent.Future;
16
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;
28
29 /**
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
34  *
35  * @author Karel Goderis - Initial contribution
36  * @author Simon Kaufmann - Refactoring and cleanup
37  */
38 @NonNullByDefault
39 public class SerialBridgeThingHandler extends KNXBridgeBaseThingHandler {
40
41     private @Nullable SerialClient client = null;
42     private @Nullable Future<?> initJob = null;
43
44     private final Logger logger = LoggerFactory.getLogger(SerialBridgeThingHandler.class);
45
46     private final SerialPortManager serialPortManager;
47
48     public SerialBridgeThingHandler(Bridge bridge, final SerialPortManager serialPortManager) {
49         super(bridge);
50         this.serialPortManager = serialPortManager;
51     }
52
53     @Override
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);
61
62         updateStatus(ThingStatus.UNKNOWN);
63         // delay actual initialization, allow for longer runtime of actual initialization
64         initJob = scheduler.submit(this::initializeLater);
65     }
66
67     public void initializeLater() {
68         SerialClient tmpClient = client;
69         if (tmpClient != null) {
70             tmpClient.initialize();
71         }
72     }
73
74     @Override
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);
81                 try {
82                     Thread.sleep(1000);
83                 } catch (InterruptedException e) {
84                     logger.trace("Bridge {}, cancellation interrupted", thing.getUID());
85                 }
86             }
87             initJob = null;
88         }
89
90         SerialClient tmpClient = client;
91         if (tmpClient != null) {
92             tmpClient.dispose();
93             client = null;
94         }
95         super.dispose();
96     }
97
98     @Override
99     protected KNXClient getClient() {
100         KNXClient ret = client;
101         if (ret == null) {
102             return new NoOpClient();
103         }
104         return ret;
105     }
106 }