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.comfoair.internal.datatypes;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.comfoair.internal.ComfoAirCommandType;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.library.types.QuantityType;
20 import org.openhab.core.library.unit.SIUnits;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.UnDefType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Class to handle temperature values
29 * @author Holger Hees - Initial Contribution
30 * @author Hans Böhm - QuantityTypes
33 public class DataTypeTemperature implements ComfoAirDataType {
34 private static final DataTypeTemperature SINGLETON_INSTANCE = new DataTypeTemperature();
36 private DataTypeTemperature() {
39 private final Logger logger = LoggerFactory.getLogger(DataTypeTemperature.class);
41 public static DataTypeTemperature getInstance() {
42 return SINGLETON_INSTANCE;
46 public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
48 logger.trace("\"DataTypeTemperature\" class \"convertToState\" method parameter: null");
49 return UnDefType.NULL;
51 int[] readReplyDataPos = commandType.getReadReplyDataPos();
52 if (readReplyDataPos != null && readReplyDataPos[0] < data.length) {
53 if (commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_GHX) {
54 return new QuantityType<>((double) data[readReplyDataPos[0]], SIUnits.CELSIUS);
56 return new QuantityType<>((((double) data[readReplyDataPos[0]]) / 2) - 20, SIUnits.CELSIUS);
59 return UnDefType.NULL;
65 public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
66 int[] template = commandType.getChangeDataTemplate();
69 if (value instanceof QuantityType<?> qt) {
70 QuantityType<?> qtCelsius = qt.toUnit(SIUnits.CELSIUS);
72 if (qtCelsius != null) {
73 celsius = qtCelsius.floatValue();
77 } else if (value instanceof DecimalType dt) {
78 celsius = dt.floatValue();
80 logger.trace("\"DataTypeTemperature\" class \"convertFromState\" undefined state");
84 if (commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_GHX) {
85 template[commandType.getChangeDataPos()] = (int) celsius;
87 template[commandType.getChangeDataPos()] = (int) (celsius + 20) * 2;