]> git.basschouten.com Git - openhab-addons.git/blob
cf2cde002b11c4a5f391fab915896f5702b5c946
[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.comfoair.internal.datatypes;
14
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.QuantityType;
19 import org.openhab.core.library.unit.Units;
20 import org.openhab.core.types.State;
21 import org.openhab.core.types.UnDefType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Class to handle time values
27  *
28  * @author Hans Böhm - Initial Contribution
29  */
30 @NonNullByDefault
31 public class DataTypeTime implements ComfoAirDataType {
32     private static final DataTypeTime SINGLETON_INSTANCE = new DataTypeTime();
33
34     private DataTypeTime() {
35     }
36
37     private final Logger logger = LoggerFactory.getLogger(DataTypeTime.class);
38
39     public static DataTypeTime getInstance() {
40         return SINGLETON_INSTANCE;
41     }
42
43     @Override
44     public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
45         if (data == null) {
46             logger.trace("\"DataTypeTime\" class \"convertToState\" method parameter: null");
47             return UnDefType.NULL;
48         } else {
49             int value = calculateNumberValue(data, commandType);
50
51             if (value < 0) {
52                 return UnDefType.NULL;
53             }
54
55             return new QuantityType<>(value, Units.HOUR);
56         }
57     }
58
59     @Override
60     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
61         int[] template = commandType.getChangeDataTemplate();
62         int[] possibleValues = commandType.getPossibleValues();
63         int position = commandType.getChangeDataPos();
64         QuantityType<?> hours = ((QuantityType<?>) value).toUnit(Units.HOUR);
65
66         if (hours != null) {
67             int intValue = hours.intValue();
68
69             if (possibleValues == null) {
70                 template[position] = intValue;
71             } else {
72                 for (int i = 0; i < possibleValues.length; i++) {
73                     if (possibleValues[i] == intValue) {
74                         template[position] = intValue;
75                         break;
76                     }
77                 }
78             }
79             return template;
80         } else {
81             logger.trace("\"DataTypeTime\" class \"convertFromState\" undefined state");
82             return null;
83         }
84     }
85 }