]> git.basschouten.com Git - openhab-addons.git/blob
267f7448c46bba7bab4edae1340707bfce77b85d
[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 static org.openhab.binding.qbus.internal.QbusBindingConstants.CHANNEL_CO2;
16 import static org.openhab.core.types.RefreshType.REFRESH;
17
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.qbus.internal.QbusBridgeHandler;
23 import org.openhab.binding.qbus.internal.protocol.QbusCO2;
24 import org.openhab.binding.qbus.internal.protocol.QbusCommunication;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.types.Command;
31
32 /**
33  * The {@link QbusCO2Handler} is responsible for handling commands, which are
34  * sent to one of the channels.
35  *
36  * @author Koen Schockaert - Initial Contribution
37  */
38
39 @NonNullByDefault
40 public class QbusCO2Handler extends QbusGlobalHandler {
41     protected @Nullable QbusThingsConfig config;
42
43     protected @Nullable QbusThingsConfig co2Config = new QbusThingsConfig();
44
45     private @Nullable Integer co2Id;
46
47     private @Nullable String sn;
48
49     public QbusCO2Handler(Thing thing) {
50         super(thing);
51     }
52
53     /**
54      * Main initialization
55      */
56     @Override
57     public void initialize() {
58         readConfig();
59
60         this.co2Id = getId();
61
62         setSN();
63
64         scheduler.submit(() -> {
65             QbusCommunication controllerComm;
66
67             if (this.co2Id != null) {
68                 controllerComm = getCommunication("CO2", this.co2Id);
69             } else {
70                 thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "ID for CO2 no set! " + this.co2Id);
71                 return;
72             }
73
74             if (controllerComm == null) {
75                 thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "ID for CO2 not known in controller " + this.co2Id);
76                 return;
77             }
78
79             Map<Integer, QbusCO2> co2CommLocal = controllerComm.getCo2();
80
81             QbusCO2 outputLocal = co2CommLocal.get(this.co2Id);
82
83             if (outputLocal == null) {
84                 thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "Bridge could not initialize CO2 ID " + this.co2Id);
85                 return;
86             }
87
88             outputLocal.setThingHandler(this);
89             handleStateUpdate(outputLocal);
90
91             QbusBridgeHandler qBridgeHandler = getBridgeHandler("CO2", this.co2Id);
92
93             if (qBridgeHandler != null) {
94                 if (qBridgeHandler.getStatus() == ThingStatus.ONLINE) {
95                     updateStatus(ThingStatus.ONLINE);
96                 } else {
97                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
98                             "Bridge offline for CO2 ID " + this.co2Id);
99                 }
100             }
101         });
102     }
103
104     /**
105      * Handle the status update from the thing
106      */
107     @Override
108     public void handleCommand(ChannelUID channelUID, Command command) {
109         QbusCommunication qComm = getCommunication("CO2", this.co2Id);
110
111         if (qComm == null) {
112             thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "ID for CO2 not known in controller " + this.co2Id);
113             return;
114         } else {
115             Map<Integer, QbusCO2> co2Comm = qComm.getCo2();
116
117             QbusCO2 qCo2 = co2Comm.get(this.co2Id);
118
119             if (qCo2 == null) {
120                 thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "ID for CO2 not known in controller " + this.co2Id);
121                 return;
122             } else {
123                 scheduler.submit(() -> {
124                     if (!qComm.communicationActive()) {
125                         restartCommunication(qComm, "CO2", this.co2Id);
126                     }
127
128                     if (qComm.communicationActive()) {
129                         if (command == REFRESH) {
130                             handleStateUpdate(qCo2);
131                             return;
132                         }
133
134                         switch (channelUID.getId()) {
135                             default:
136                                 thingOffline(ThingStatusDetail.COMMUNICATION_ERROR,
137                                         "Unknown Channel " + channelUID.getId());
138                         }
139                     }
140                 });
141             }
142         }
143     }
144
145     /**
146      * Method to update state of channel, called from Qbus CO2.
147      */
148     public void handleStateUpdate(QbusCO2 qCo2) {
149         Integer co2State = qCo2.getState();
150         if (co2State != null) {
151             updateState(CHANNEL_CO2, new DecimalType(co2State));
152         }
153     }
154
155     /**
156      * Returns the serial number of the controller
157      *
158      * @return the serial nr
159      */
160     public @Nullable String getSN() {
161         return sn;
162     }
163
164     /**
165      * Sets the serial number of the controller
166      */
167     public void setSN() {
168         QbusBridgeHandler qBridgeHandler = getBridgeHandler("CO2", this.co2Id);
169         if (qBridgeHandler == null) {
170             thingOffline(ThingStatusDetail.COMMUNICATION_ERROR,
171                     "No communication with Qbus Bridge for CO2 " + this.co2Id);
172             return;
173         }
174         sn = qBridgeHandler.getSn();
175     }
176
177     /**
178      * Read the configuration
179      */
180     protected synchronized void readConfig() {
181         co2Config = getConfig().as(QbusThingsConfig.class);
182     }
183
184     /**
185      * Returns the Id from the configuration
186      *
187      * @return outputId
188      */
189     public @Nullable Integer getId() {
190         QbusThingsConfig localConfig = this.co2Config;
191         if (localConfig != null) {
192             return localConfig.co2Id;
193         } else {
194             return null;
195         }
196     }
197 }