]> git.basschouten.com Git - openhab-addons.git/blob
925fff97f9ebdbc03d43ca2b7be117afa6fbbafd
[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.lcn.internal.converter;
14
15 import java.util.function.Function;
16
17 import javax.measure.Unit;
18
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
26 /**
27  * Base class for all LCN variable value converters.
28  *
29  * @author Fabian Wolter - Initial Contribution
30  */
31 @NonNullByDefault
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;
36
37     public ValueConverter(@Nullable Unit<?> unit, Function<Long, Double> toHuman, Function<Double, Long> toNative) {
38         this.unit = unit;
39         this.toHuman = toHuman;
40         this.toNative = toNative;
41     }
42
43     /**
44      * Converts the given human readable value into the native LCN value.
45      *
46      * @param humanReadableValue the value to convert
47      * @return the native value
48      */
49     protected long toNative(double humanReadableValue) {
50         return toNative.apply(humanReadableValue);
51     }
52
53     /**
54      * Converts the given native LCN value into a human readable value.
55      *
56      * @param nativeValue the value to convert
57      * @return the human readable value
58      */
59     protected double toHumanReadable(long nativeValue) {
60         return toHuman.apply(nativeValue);
61     }
62
63     /**
64      * Converts a human readable value into LCN native value.
65      *
66      * @param humanReadable value to convert
67      * @return the native LCN value
68      */
69     @Override
70     public DecimalType onCommandFromItem(double humanReadable) {
71         return new DecimalType(toNative(humanReadable));
72     }
73
74     /**
75      * Converts a human readable value into LCN native value.
76      *
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
80      */
81     @Override
82     public DecimalType onCommandFromItem(QuantityType<?> quantityType) throws LcnException {
83         Unit<?> localUnit = unit;
84         if (localUnit == null) {
85             return onCommandFromItem(quantityType.doubleValue());
86         }
87
88         QuantityType<?> quantityInBaseUnit = quantityType.toUnit(localUnit);
89
90         if (quantityInBaseUnit != null) {
91             return onCommandFromItem(quantityInBaseUnit.doubleValue());
92         } else {
93             throw new LcnException(quantityType + ": Incompatible Channel unit configured: " + localUnit);
94         }
95     }
96
97     /**
98      * Converts a state update from the Thing into a human readable unit.
99      *
100      * @param state from the Thing
101      * @return human readable State
102      * @throws LcnException
103      */
104     @Override
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);
110             }
111
112             return state;
113         } else {
114             throw new LcnException("Unexpected state type: Was " + state.getClass().getSimpleName()
115                     + " but expected DecimalType: " + state);
116         }
117     }
118 }