2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.comfoair.internal.datatypes;
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;
27 * Class to handle volt values
29 * @author Grzegorz Miasko - Initial Contribution
30 * @author Hans Böhm - QuantityTypes
33 public class DataTypeVolt implements ComfoAirDataType {
34 private static final DataTypeVolt SINGLETON_INSTANCE = new DataTypeVolt();
36 private DataTypeVolt() {
39 private final Logger logger = LoggerFactory.getLogger(DataTypeVolt.class);
41 public static DataTypeVolt getInstance() {
42 return SINGLETON_INSTANCE;
46 public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
48 logger.trace("\"DataTypeVoltage\" class \"convertToState\" method parameter: null");
49 return UnDefType.NULL;
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);
55 return UnDefType.NULL;
61 public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
62 int[] template = commandType.getChangeDataTemplate();
65 if (value instanceof QuantityType<?> qt) {
66 QuantityType<?> qtVolt = qt.toUnit(Units.VOLT);
69 volt = qtVolt.floatValue();
73 } else if (value instanceof DecimalType dt) {
74 volt = dt.floatValue();
76 logger.trace("\"DataTypeVolt\" class \"convertFromState\" undefined state");
80 template[commandType.getChangeDataPos()] = (int) (volt * 255 / 10);