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.Units;
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 time values
29 * @author Hans Böhm - Initial Contribution
32 public class DataTypeTime implements ComfoAirDataType {
33 private static final DataTypeTime SINGLETON_INSTANCE = new DataTypeTime();
35 private DataTypeTime() {
38 private final Logger logger = LoggerFactory.getLogger(DataTypeTime.class);
40 public static DataTypeTime getInstance() {
41 return SINGLETON_INSTANCE;
45 public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
47 logger.trace("\"DataTypeTime\" class \"convertToState\" method parameter: null");
48 return UnDefType.NULL;
50 int value = calculateNumberValue(data, commandType);
53 return UnDefType.NULL;
56 if (commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_DELAYS
57 || commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_PREHEATER) {
58 if (commandType == ComfoAirCommandType.FILTER_WEEKS) {
59 return new QuantityType<>(value, Units.WEEK);
61 return new QuantityType<>(value, Units.MINUTE);
63 } else if (commandType == ComfoAirCommandType.ENTHALPY_TIME) {
64 return new QuantityType<>(value * 12, Units.MINUTE);
66 return new QuantityType<>(value, Units.HOUR);
71 public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
72 int[] template = commandType.getChangeDataTemplate();
73 int[] possibleValues = commandType.getPossibleValues();
74 int position = commandType.getChangeDataPos();
77 if (value instanceof QuantityType<?> qt) {
78 QuantityType<?> internalQuantity = new QuantityType<>();
80 if (commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_DELAYS
81 || commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_PREHEATER) {
82 if (commandType == ComfoAirCommandType.FILTER_WEEKS) {
83 internalQuantity = qt.toUnit(Units.WEEK);
85 internalQuantity = qt.toUnit(Units.MINUTE);
88 internalQuantity = qt.toUnit(Units.HOUR);
91 if (internalQuantity != null) {
92 intValue = internalQuantity.intValue();
96 } else if (value instanceof DecimalType dt) {
97 intValue = dt.intValue();
99 logger.trace("\"DataTypeTime\" class \"convertFromState\" undefined state");
103 if (possibleValues == null) {
104 template[position] = intValue;
106 for (int i = 0; i < possibleValues.length; i++) {
107 if (possibleValues[i] == intValue) {
108 template[position] = intValue;