]> git.basschouten.com Git - openhab-addons.git/blob
717625842b39b85eaa1b97647208c9286a20a128
[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         return qBridgeHandler.getCommunication();
59     }
60
61     /**
62      * Get the Bridge handler
63      *
64      * @param type
65      * @param globalId
66      * @return
67      */
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);
73             return null;
74         }
75         return (QbusBridgeHandler) qBridge.getHandler();
76     }
77
78     /**
79      *
80      * @param qComm
81      * @param type
82      * @param globalId
83      */
84     public void restartCommunication(QbusCommunication qComm, String type, @Nullable Integer globalId) {
85         try {
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);
93         }
94
95         QbusBridgeHandler qBridgeHandler = getBridgeHandler(type, globalId);
96
97         if (qBridgeHandler != null && qComm.communicationActive()) {
98             qBridgeHandler.bridgeOnline();
99         } else {
100             thingOffline(ThingStatusDetail.COMMUNICATION_ERROR, "Communication socket error");
101         }
102     }
103
104     /**
105      * Put thing offline
106      *
107      * @param message
108      */
109     public void thingOffline(ThingStatusDetail detail, String message) {
110         updateStatus(ThingStatus.OFFLINE, detail, message);
111     }
112 }