/**
 * Copyright (c) 2010-2020 Contributors to the openHAB project
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.openhab.binding.comfoair.internal.datatypes;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.comfoair.internal.ComfoAirCommandType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class to handle temperature values
 *
 * @author Holger Hees - Initial Contribution
 * @author Hans Böhm - QuantityTypes
 */
@NonNullByDefault
public class DataTypeTemperature implements ComfoAirDataType {
    private static final DataTypeTemperature SINGLETON_INSTANCE = new DataTypeTemperature();

    private DataTypeTemperature() {
    }

    private final Logger logger = LoggerFactory.getLogger(DataTypeTemperature.class);

    public static DataTypeTemperature getInstance() {
        return SINGLETON_INSTANCE;
    }

    @Override
    public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
        if (data == null) {
            logger.trace("\"DataTypeTemperature\" class \"convertToState\" method parameter: null");
            return UnDefType.NULL;
        } else {
            int[] readReplyDataPos = commandType.getReadReplyDataPos();
            if (readReplyDataPos != null && readReplyDataPos[0] < data.length) {
                return new QuantityType<>((((double) data[readReplyDataPos[0]]) / 2) - 20, SIUnits.CELSIUS);
            } else {
                return UnDefType.NULL;
            }
        }
    }

    @Override
    public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
        int[] template = commandType.getChangeDataTemplate();
        QuantityType<?> celsius = ((QuantityType<?>) value).toUnit(SIUnits.CELSIUS);

        if (celsius != null) {
            template[commandType.getChangeDataPos()] = (int) (celsius.doubleValue() + 20) * 2;
            return template;
        } else {
            logger.trace("\"DataTypeTemperature\" class \"convertFromState\" undefined state");
            return null;
        }
    }
}

@@ -1795,24 +1795,254 @@
 a.length) %7B%0A
+                if (commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_EWT) %7B%0A                    return new QuantityType%3C%3E((double) data%5BreadReplyDataPos%5B0%5D%5D, SIUnits.CELSIUS);%0A                %7D else %7B%0A    
             
@@ -2130,32 +2130,50 @@
 Units.CELSIUS);%0A
+                %7D%0A
             %7D el
@@ -2525,32 +2525,233 @@
 sius != null) %7B%0A
+            if (commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_EWT) %7B%0A                template%5BcommandType.getChangeDataPos()%5D = celsius.intValue();%0A            %7D else %7B%0A    
             temp
@@ -2829,16 +2829,30 @@
 0) * 2;%0A
+            %7D%0A
         

fix ewt temperatures (#8608)