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.lcn.internal.converter;
15 import java.util.function.Function;
17 import javax.measure.Unit;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lcn.internal.common.LcnException;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.types.State;
27 * Base class for all LCN variable value converters.
29 * @author Fabian Wolter - Initial Contribution
32 public class ValueConverter extends Converter {
33 private @Nullable final Unit<?> unit;
34 private final Function<Long, Double> toHuman;
35 private final Function<Double, Long> toNative;
37 public ValueConverter(@Nullable Unit<?> unit, Function<Long, Double> toHuman, Function<Double, Long> toNative) {
39 this.toHuman = toHuman;
40 this.toNative = toNative;
44 * Converts the given human readable value into the native LCN value.
46 * @param humanReadableValue the value to convert
47 * @return the native value
49 protected long toNative(double humanReadableValue) {
50 return toNative.apply(humanReadableValue);
54 * Converts the given native LCN value into a human readable value.
56 * @param nativeValue the value to convert
57 * @return the human readable value
59 protected double toHumanReadable(long nativeValue) {
60 return toHuman.apply(nativeValue);
64 * Converts a human readable value into LCN native value.
66 * @param humanReadable value to convert
67 * @return the native LCN value
70 public DecimalType onCommandFromItem(double humanReadable) {
71 return new DecimalType(toNative(humanReadable));
75 * Converts a human readable value into LCN native value.
77 * @param humanReadable value to convert
78 * @return the native LCN value
79 * @throws LcnException when the value could not be converted to the base unit
82 public DecimalType onCommandFromItem(QuantityType<?> quantityType) throws LcnException {
83 Unit<?> localUnit = unit;
84 if (localUnit == null) {
85 return onCommandFromItem(quantityType.doubleValue());
88 QuantityType<?> quantityInBaseUnit = quantityType.toUnit(localUnit);
90 if (quantityInBaseUnit != null) {
91 return onCommandFromItem(quantityInBaseUnit.doubleValue());
93 throw new LcnException(quantityType + ": Incompatible Channel unit configured: " + localUnit);
98 * Converts a state update from the Thing into a human readable unit.
100 * @param state from the Thing
101 * @return human readable State
102 * @throws LcnException
105 public State onStateUpdateFromHandler(State state) throws LcnException {
106 if (state instanceof DecimalType decimalValue) {
107 Unit<?> localUnit = unit;
108 if (localUnit != null) {
109 return QuantityType.valueOf(toHumanReadable(decimalValue.longValue()), localUnit);
114 throw new LcnException("Unexpected state type: Was " + state.getClass().getSimpleName()
115 + " but expected DecimalType: " + state);