]> git.basschouten.com Git - openhab-addons.git/blob
cd4cd3c407937aa0a58f63a36493b6a783df55eb
[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.SIUnits;
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 temperature values
27  *
28  * @author Holger Hees - Initial Contribution
29  * @author Hans Böhm - QuantityTypes
30  */
31 @NonNullByDefault
32 public class DataTypeTemperature implements ComfoAirDataType {
33     private static final DataTypeTemperature SINGLETON_INSTANCE = new DataTypeTemperature();
34
35     private DataTypeTemperature() {
36     }
37
38     private final Logger logger = LoggerFactory.getLogger(DataTypeTemperature.class);
39
40     public static DataTypeTemperature 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("\"DataTypeTemperature\" class \"convertToState\" method parameter: null");
48             return UnDefType.NULL;
49         } else {
50             int[] readReplyDataPos = commandType.getReadReplyDataPos();
51             if (readReplyDataPos != null && readReplyDataPos[0] < data.length) {
52                 if (commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_GHX) {
53                     return new QuantityType<>((double) data[readReplyDataPos[0]], SIUnits.CELSIUS);
54                 } else {
55                     return new QuantityType<>((((double) data[readReplyDataPos[0]]) / 2) - 20, SIUnits.CELSIUS);
56                 }
57             } else {
58                 return UnDefType.NULL;
59             }
60         }
61     }
62
63     @Override
64     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
65         int[] template = commandType.getChangeDataTemplate();
66         QuantityType<?> celsius = ((QuantityType<?>) value).toUnit(SIUnits.CELSIUS);
67
68         if (celsius != null) {
69             if (commandType.getReadCommand() == ComfoAirCommandType.Constants.REQUEST_GET_GHX) {
70                 template[commandType.getChangeDataPos()] = celsius.intValue();
71             } else {
72                 template[commandType.getChangeDataPos()] = (int) (celsius.doubleValue() + 20) * 2;
73             }
74             return template;
75         } else {
76             logger.trace("\"DataTypeTemperature\" class \"convertFromState\" undefined state");
77             return null;
78         }
79     }
80 }