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.QuantityType;
19 import org.openhab.core.library.unit.Units;
20 import org.openhab.core.types.State;
21 import org.openhab.core.types.UnDefType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Class to handle volt values
28 * @author Grzegorz Miasko - Initial Contribution
29 * @author Hans Böhm - QuantityTypes
32 public class DataTypeVolt implements ComfoAirDataType {
33 private static final DataTypeVolt SINGLETON_INSTANCE = new DataTypeVolt();
35 private DataTypeVolt() {
38 private final Logger logger = LoggerFactory.getLogger(DataTypeVolt.class);
40 public static DataTypeVolt getInstance() {
41 return SINGLETON_INSTANCE;
45 public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
47 logger.trace("\"DataTypeVolt\" class \"convertToState\" method parameter: null");
48 return UnDefType.NULL;
50 int[] readReplyDataPos = commandType.getReadReplyDataPos();
51 if (readReplyDataPos != null && readReplyDataPos[0] < data.length) {
52 return new QuantityType<>((double) data[readReplyDataPos[0]] * 10 / 255, Units.VOLT);
54 return UnDefType.NULL;
60 public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
61 int[] template = commandType.getChangeDataTemplate();
62 QuantityType<?> volts = ((QuantityType<?>) value).toUnit(Units.VOLT);
65 template[commandType.getChangeDataPos()] = (int) (volts.doubleValue() * 255 / 10);
68 logger.trace("\"DataTypeVolt\" class \"convertFromState\" undefined state");