]> git.basschouten.com Git - openhab-addons.git/blob
f0a49be11f963891eb0ae894f21632454442788a
[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.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;
25
26 /**
27  * Class to handle temperature values
28  *
29  * @author Holger Hees - Initial Contribution
30  * @author Hans Böhm - QuantityTypes
31  */
32 @NonNullByDefault
33 public class DataTypeTemperature implements ComfoAirDataType {
34     private static final DataTypeTemperature SINGLETON_INSTANCE = new DataTypeTemperature();
35
36     private DataTypeTemperature() {
37     }
38
39     private final Logger logger = LoggerFactory.getLogger(DataTypeTemperature.class);
40
41     public static DataTypeTemperature getInstance() {
42         return SINGLETON_INSTANCE;
43     }
44
45     @Override
46     public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
47         if (data == null) {
48             logger.trace("\"DataTypeTemperature\" class \"convertToState\" method parameter: null");
49             return UnDefType.NULL;
50         } else {
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);
55                 } else {
56                     return new QuantityType<>((((double) data[readReplyDataPos[0]]) / 2) - 20, SIUnits.CELSIUS);
57                 }
58             } else {
59                 return UnDefType.NULL;
60             }
61         }
62     }
63
64     @Override
65     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
66         int[] template = commandType.getChangeDataTemplate();
67         float celsius;
68
69         if (value instanceof QuantityType<?> qt) {
70             QuantityType<?> qtCelsius = qt.toUnit(SIUnits.CELSIUS);
71
72             if (qtCelsius != null) {
73                 celsius = qtCelsius.floatValue();
74             } else {
75                 return null;
76             }
77         } else if (value instanceof DecimalType dt) {
78             celsius = dt.floatValue();
79         } else {
80             logger.trace("\"DataTypeTemperature\" class \"convertFromState\" undefined state");
81             return null;
82         }
83
84         if (commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_GHX) {
85             template[commandType.getChangeDataPos()] = (int) celsius;
86         } else {
87             template[commandType.getChangeDataPos()] = (int) (celsius + 20) * 2;
88         }
89         return template;
90     }
91 }