2 * Copyright (c) 2010-2021 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;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Base class for all LCN variable value converters.
31 * @author Fabian Wolter - Initial Contribution
34 public class ValueConverter extends Converter {
35 private final Logger logger = LoggerFactory.getLogger(ValueConverter.class);
36 private @Nullable final Unit<?> unit;
37 private final Function<Long, Double> toHuman;
38 private final Function<Double, Long> toNative;
40 public ValueConverter(@Nullable Unit<?> unit, Function<Long, Double> toHuman, Function<Double, Long> toNative) {
42 this.toHuman = toHuman;
43 this.toNative = toNative;
47 * Converts the given human readable value into the native LCN value.
49 * @param humanReadableValue the value to convert
50 * @return the native value
52 protected long toNative(double humanReadableValue) {
53 return toNative.apply(humanReadableValue);
57 * Converts the given native LCN value into a human readable value.
59 * @param nativeValue the value to convert
60 * @return the human readable value
62 protected double toHumanReadable(long nativeValue) {
63 return toHuman.apply(nativeValue);
67 * Converts a human readable value into LCN native value.
69 * @param humanReadable value to convert
70 * @return the native LCN value
73 public DecimalType onCommandFromItem(double humanReadable) {
74 return new DecimalType(toNative(humanReadable));
78 * Converts a human readable value into LCN native value.
80 * @param humanReadable value to convert
81 * @return the native LCN value
82 * @throws LcnException when the value could not be converted to the base unit
85 public DecimalType onCommandFromItem(QuantityType<?> quantityType) throws LcnException {
86 Unit<?> localUnit = unit;
87 if (localUnit == null) {
88 return onCommandFromItem(quantityType.doubleValue());
91 QuantityType<?> quantityInBaseUnit = quantityType.toUnit(localUnit);
93 if (quantityInBaseUnit != null) {
94 return onCommandFromItem(quantityInBaseUnit.doubleValue());
96 throw new LcnException(quantityType + ": Incompatible Channel unit configured: " + localUnit);
101 * Converts a state update from the Thing into a human readable unit.
103 * @param state from the Thing
104 * @return human readable State
107 public State onStateUpdateFromHandler(State state) {
108 State result = state;
110 if (state instanceof DecimalType) {
111 Unit<?> localUnit = unit;
112 if (localUnit != null) {
113 result = QuantityType.valueOf(toHumanReadable(((DecimalType) state).longValue()), localUnit);
116 logger.warn("Unexpected state type: {}", state.getClass().getSimpleName());