]> git.basschouten.com Git - openhab-addons.git/blob
0e1be2f895c35a3be73427c0c28c86f384b5f6f6
[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.qbus.internal.handler;
14
15 import java.io.IOException;
16
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;
26
27 /**
28  * The {@link QbusGlobalHandler} is used in other handlers, to share the functions.
29  *
30  * @author Koen Schockaert - Initial Contribution
31  */
32
33 @NonNullByDefault
34 public abstract class QbusGlobalHandler extends BaseThingHandler {
35
36     public QbusGlobalHandler(Thing thing) {
37         super(thing);
38     }
39
40     /**
41      * Get Bridge communication
42      *
43      * @param type
44      * @param globalId
45      * @return
46      */
47     public @Nullable QbusCommunication getCommunication(String type, @Nullable Integer globalId) {
48         QbusBridgeHandler qBridgeHandler = null;
49         if (globalId != null) {
50             qBridgeHandler = getBridgeHandler(type, globalId);
51         }
52
53         if (qBridgeHandler == null) {
54             updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.BRIDGE_UNINITIALIZED,
55                     "No bridge handler initialized for " + type + " with id " + globalId + ".");
56             return null;
57         }
58         QbusCommunication qComm = qBridgeHandler.getCommunication();
59         return qComm;
60     }
61
62     /**
63      * Get the Bridge handler
64      *
65      * @param type
66      * @param globalId
67      * @return
68      */
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);
74             return null;
75         }
76         QbusBridgeHandler qBridgeHandler = (QbusBridgeHandler) qBridge.getHandler();
77         return qBridgeHandler;
78     }
79
80     /**
81      *
82      * @param qComm
83      * @param type
84      * @param globalId
85      */
86     public void restartCommunication(QbusCommunication qComm, String type, @Nullable Integer globalId) {
87         try {
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);
95         }
96
97         QbusBridgeHandler qBridgeHandler = getBridgeHandler(type, globalId);
98
99         if (qBridgeHandler != null && qComm.communicationActive()) {
100             qBridgeHandler.bridgeOnline();
101         } else {
102             thingOffline(ThingStatusDetail.COMMUNICATION_ERROR, "Communication socket error");
103         }
104     }
105
106     /**
107      * Put thing offline
108      *
109      * @param message
110      */
111     public void thingOffline(ThingStatusDetail detail, String message) {
112         updateStatus(ThingStatus.OFFLINE, detail, message);
113     }
114 }