]> git.basschouten.com Git - openhab-addons.git/blob
d8d1d7294fb71c4e574d3b91a131ebf42977af52
[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.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;
25
26 /**
27  * Class to handle time values
28  *
29  * @author Hans Böhm - Initial Contribution
30  */
31 @NonNullByDefault
32 public class DataTypeTime implements ComfoAirDataType {
33     private static final DataTypeTime SINGLETON_INSTANCE = new DataTypeTime();
34
35     private DataTypeTime() {
36     }
37
38     private final Logger logger = LoggerFactory.getLogger(DataTypeTime.class);
39
40     public static DataTypeTime getInstance() {
41         return SINGLETON_INSTANCE;
42     }
43
44     @Override
45     public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
46         if (data == null) {
47             logger.trace("\"DataTypeTime\" class \"convertToState\" method parameter: null");
48             return UnDefType.NULL;
49         } else {
50             int value = calculateNumberValue(data, commandType);
51
52             if (value < 0) {
53                 return UnDefType.NULL;
54             }
55
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);
60                 } else {
61                     return new QuantityType<>(value, Units.MINUTE);
62                 }
63             } else if (commandType == ComfoAirCommandType.ENTHALPY_TIME) {
64                 return new QuantityType<>(value * 12, Units.MINUTE);
65             }
66             return new QuantityType<>(value, Units.HOUR);
67         }
68     }
69
70     @Override
71     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
72         int[] template = commandType.getChangeDataTemplate();
73         int[] possibleValues = commandType.getPossibleValues();
74         int position = commandType.getChangeDataPos();
75         int intValue;
76
77         if (value instanceof QuantityType<?> qt) {
78             QuantityType<?> internalQuantity = new QuantityType<>();
79
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);
84                 } else {
85                     internalQuantity = qt.toUnit(Units.MINUTE);
86                 }
87             } else {
88                 internalQuantity = qt.toUnit(Units.HOUR);
89             }
90
91             if (internalQuantity != null) {
92                 intValue = internalQuantity.intValue();
93             } else {
94                 return null;
95             }
96         } else if (value instanceof DecimalType dt) {
97             intValue = dt.intValue();
98         } else {
99             logger.trace("\"DataTypeTime\" class \"convertFromState\" undefined state");
100             return null;
101         }
102
103         if (possibleValues == null) {
104             template[position] = intValue;
105         } else {
106             for (int i = 0; i < possibleValues.length; i++) {
107                 if (possibleValues[i] == intValue) {
108                     template[position] = intValue;
109                     break;
110                 }
111             }
112         }
113         return template;
114     }
115 }