]> git.basschouten.com Git - openhab-addons.git/blob
6af621995b2840cf4acda75d405f8f41fbb4f315
[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 volt values
28  *
29  * @author Grzegorz Miasko - Initial Contribution
30  * @author Hans Böhm - QuantityTypes
31  */
32 @NonNullByDefault
33 public class DataTypeVolt implements ComfoAirDataType {
34     private static final DataTypeVolt SINGLETON_INSTANCE = new DataTypeVolt();
35
36     private DataTypeVolt() {
37     }
38
39     private final Logger logger = LoggerFactory.getLogger(DataTypeVolt.class);
40
41     public static DataTypeVolt 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("\"DataTypeVoltage\" 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                 return new QuantityType<>((double) data[readReplyDataPos[0]] * 10 / 255, Units.VOLT);
54             } else {
55                 return UnDefType.NULL;
56             }
57         }
58     }
59
60     @Override
61     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
62         int[] template = commandType.getChangeDataTemplate();
63         float volt;
64
65         if (value instanceof QuantityType<?> qt) {
66             QuantityType<?> qtVolt = qt.toUnit(Units.VOLT);
67
68             if (qtVolt != null) {
69                 volt = qtVolt.floatValue();
70             } else {
71                 return null;
72             }
73         } else if (value instanceof DecimalType dt) {
74             volt = dt.floatValue();
75         } else {
76             logger.trace("\"DataTypeVolt\" class \"convertFromState\" undefined state");
77             return null;
78         }
79
80         template[commandType.getChangeDataPos()] = (int) (volt * 255 / 10);
81         return template;
82     }
83 }