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 static org.openhab.binding.qbus.internal.QbusBindingConstants.CHANNEL_CO2;
16 import static org.openhab.core.types.RefreshType.REFRESH;
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;
33 * The {@link QbusCO2Handler} is responsible for handling commands, which are
34 * sent to one of the channels.
36 * @author Koen Schockaert - Initial Contribution
40 public class QbusCO2Handler extends QbusGlobalHandler {
41 protected @Nullable QbusThingsConfig config;
43 protected @Nullable QbusThingsConfig co2Config = new QbusThingsConfig();
45 private @Nullable Integer co2Id;
47 private @Nullable String sn;
49 public QbusCO2Handler(Thing thing) {
57 public void initialize() {
64 scheduler.submit(() -> {
65 QbusCommunication controllerComm;
67 if (this.co2Id != null) {
68 controllerComm = getCommunication("CO2", this.co2Id);
70 thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "ID for CO2 no set! " + this.co2Id);
74 if (controllerComm == null) {
75 thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "ID for CO2 not known in controller " + this.co2Id);
79 Map<Integer, QbusCO2> co2CommLocal = controllerComm.getCo2();
81 QbusCO2 outputLocal = co2CommLocal.get(this.co2Id);
83 if (outputLocal == null) {
84 thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "Bridge could not initialize CO2 ID " + this.co2Id);
88 outputLocal.setThingHandler(this);
89 handleStateUpdate(outputLocal);
91 QbusBridgeHandler qBridgeHandler = getBridgeHandler("CO2", this.co2Id);
93 if (qBridgeHandler != null) {
94 if (qBridgeHandler.getStatus() == ThingStatus.ONLINE) {
95 updateStatus(ThingStatus.ONLINE);
97 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
98 "Bridge offline for CO2 ID " + this.co2Id);
105 * Handle the status update from the thing
108 public void handleCommand(ChannelUID channelUID, Command command) {
109 QbusCommunication qComm = getCommunication("CO2", this.co2Id);
112 thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "ID for CO2 not known in controller " + this.co2Id);
115 Map<Integer, QbusCO2> co2Comm = qComm.getCo2();
117 QbusCO2 qCo2 = co2Comm.get(this.co2Id);
120 thingOffline(ThingStatusDetail.CONFIGURATION_ERROR, "ID for CO2 not known in controller " + this.co2Id);
123 scheduler.submit(() -> {
124 if (!qComm.communicationActive()) {
125 restartCommunication(qComm, "CO2", this.co2Id);
128 if (qComm.communicationActive()) {
129 if (command == REFRESH) {
130 handleStateUpdate(qCo2);
134 switch (channelUID.getId()) {
136 thingOffline(ThingStatusDetail.COMMUNICATION_ERROR,
137 "Unknown Channel " + channelUID.getId());
146 * Method to update state of channel, called from Qbus CO2.
148 public void handleStateUpdate(QbusCO2 qCo2) {
149 Integer co2State = qCo2.getState();
150 if (co2State != null) {
151 updateState(CHANNEL_CO2, new DecimalType(co2State));
156 * Returns the serial number of the controller
158 * @return the serial nr
160 public @Nullable String getSN() {
165 * Sets the serial number of the controller
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);
174 sn = qBridgeHandler.getSn();
178 * Read the configuration
180 protected synchronized void readConfig() {
181 co2Config = getConfig().as(QbusThingsConfig.class);
185 * Returns the Id from the configuration
189 public @Nullable Integer getId() {
190 QbusThingsConfig localConfig = this.co2Config;
191 if (localConfig != null) {
192 return localConfig.co2Id;