]> git.basschouten.com Git - openhab-addons.git/blob
4c598f1fe7d36a2ee60525f14a5879286df620a5
[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.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.thing.Bridge;
24 import org.openhab.core.thing.ThingStatus;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
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
33  *
34  * @author Karel Goderis - Initial contribution
35  * @author Simon Kaufmann - Refactoring and cleanup
36  */
37 @NonNullByDefault
38 public class SerialBridgeThingHandler extends KNXBridgeBaseThingHandler {
39
40     private @Nullable SerialClient client = null;
41     private @Nullable Future<?> initJob = null;
42
43     private final Logger logger = LoggerFactory.getLogger(SerialBridgeThingHandler.class);
44
45     public SerialBridgeThingHandler(Bridge bridge) {
46         super(bridge);
47     }
48
49     @Override
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);
57
58         updateStatus(ThingStatus.UNKNOWN);
59         // delay actual initialization, allow for longer runtime of actual initialization
60         initJob = scheduler.submit(this::initializeLater);
61     }
62
63     public void initializeLater() {
64         final var tmpClient = client;
65         if (tmpClient != null) {
66             tmpClient.initialize();
67         }
68     }
69
70     @Override
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);
77                 try {
78                     Thread.sleep(1000);
79                 } catch (InterruptedException e) {
80                     logger.trace("Bridge {}, cancellation interrupted", thing.getUID());
81                 }
82             }
83             initJob = null;
84         }
85
86         final var tmpClient = client;
87         if (tmpClient != null) {
88             tmpClient.dispose();
89             client = null;
90         }
91         super.dispose();
92     }
93
94     @Override
95     protected KNXClient getClient() {
96         KNXClient ret = client;
97         if (ret == null) {
98             return new NoOpClient();
99         }
100         return ret;
101     }
102 }