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 return qBridgeHandler.getCommunication();
62 * Get the Bridge handler
68 public @Nullable QbusBridgeHandler getBridgeHandler(String type, @Nullable Integer globalId) {
69 Bridge qBridge = getBridge();
70 if (qBridge == null) {
71 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.BRIDGE_UNINITIALIZED,
72 "No bridge initialized for " + type + " with ID " + globalId);
75 return (QbusBridgeHandler) qBridge.getHandler();
84 public void restartCommunication(QbusCommunication qComm, String type, @Nullable Integer globalId) {
86 qComm.restartCommunication();
87 } catch (InterruptedException e) {
88 String message = e.toString();
89 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, message);
90 } catch (IOException e) {
91 String message = e.toString();
92 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, message);
95 QbusBridgeHandler qBridgeHandler = getBridgeHandler(type, globalId);
97 if (qBridgeHandler != null && qComm.communicationActive()) {
98 qBridgeHandler.bridgeOnline();
100 thingOffline(ThingStatusDetail.COMMUNICATION_ERROR, "Communication socket error");
109 public void thingOffline(ThingStatusDetail detail, String message) {
110 updateStatus(ThingStatus.OFFLINE, detail, message);