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.qbus.internal.handler;
15 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.qbus.internal.QbusBridgeHandler;
20 import org.openhab.binding.qbus.internal.protocol.QbusCommunication;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.thing.binding.BaseThingHandler;
28 * The {@link QbusGlobalHandler} is used in other handlers, to share the functions.
30 * @author Koen Schockaert - Initial Contribution
34 public abstract class QbusGlobalHandler extends BaseThingHandler {
36 public QbusGlobalHandler(Thing thing) {
41 * Get Bridge communication
47 public @Nullable QbusCommunication getCommunication(String type, @Nullable Integer globalId) {
48 QbusBridgeHandler qBridgeHandler = null;
49 if (globalId != null) {
50 qBridgeHandler = getBridgeHandler(type, globalId);
53 if (qBridgeHandler == null) {
54 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.BRIDGE_UNINITIALIZED,
55 "No bridge handler initialized for " + type + " with id " + globalId + ".");
58 QbusCommunication qComm = qBridgeHandler.getCommunication();
63 * Get the Bridge handler
69 public @Nullable QbusBridgeHandler getBridgeHandler(String type, @Nullable Integer globalId) {
70 Bridge qBridge = getBridge();
71 if (qBridge == null) {
72 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.BRIDGE_UNINITIALIZED,
73 "No bridge initialized for " + type + " with ID " + globalId);
76 QbusBridgeHandler qBridgeHandler = (QbusBridgeHandler) qBridge.getHandler();
77 return qBridgeHandler;
86 public void restartCommunication(QbusCommunication qComm, String type, @Nullable Integer globalId) {
88 qComm.restartCommunication();
89 } catch (InterruptedException e) {
90 String message = e.toString();
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, message);
92 } catch (IOException e) {
93 String message = e.toString();
94 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, message);
97 QbusBridgeHandler qBridgeHandler = getBridgeHandler(type, globalId);
99 if (qBridgeHandler != null && qComm.communicationActive()) {
100 qBridgeHandler.bridgeOnline();
102 thingOffline(ThingStatusDetail.COMMUNICATION_ERROR, "Communication socket error");
111 public void thingOffline(ThingStatusDetail detail, String message) {
112 updateStatus(ThingStatus.OFFLINE, detail, message);