]> git.basschouten.com Git - openhab-addons.git/blob
53c5b634163fad76107431ea2fdde542ba4ffddb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Base class for all LCN variable value converters.
30  *
31  * @author Fabian Wolter - Initial Contribution
32  */
33 @NonNullByDefault
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;
39
40     public ValueConverter(@Nullable Unit<?> unit, Function<Long, Double> toHuman, Function<Double, Long> toNative) {
41         this.unit = unit;
42         this.toHuman = toHuman;
43         this.toNative = toNative;
44     }
45
46     /**
47      * Converts the given human readable value into the native LCN value.
48      *
49      * @param humanReadableValue the value to convert
50      * @return the native value
51      */
52     protected long toNative(double humanReadableValue) {
53         return toNative.apply(humanReadableValue);
54     }
55
56     /**
57      * Converts the given native LCN value into a human readable value.
58      *
59      * @param nativeValue the value to convert
60      * @return the human readable value
61      */
62     protected double toHumanReadable(long nativeValue) {
63         return toHuman.apply(nativeValue);
64     }
65
66     /**
67      * Converts a human readable value into LCN native value.
68      *
69      * @param humanReadable value to convert
70      * @return the native LCN value
71      */
72     @Override
73     public DecimalType onCommandFromItem(double humanReadable) {
74         return new DecimalType(toNative(humanReadable));
75     }
76
77     /**
78      * Converts a human readable value into LCN native value.
79      *
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
83      */
84     @Override
85     public DecimalType onCommandFromItem(QuantityType<?> quantityType) throws LcnException {
86         Unit<?> localUnit = unit;
87         if (localUnit == null) {
88             return onCommandFromItem(quantityType.doubleValue());
89         }
90
91         QuantityType<?> quantityInBaseUnit = quantityType.toUnit(localUnit);
92
93         if (quantityInBaseUnit != null) {
94             return onCommandFromItem(quantityInBaseUnit.doubleValue());
95         } else {
96             throw new LcnException(quantityType + ": Incompatible Channel unit configured: " + localUnit);
97         }
98     }
99
100     /**
101      * Converts a state update from the Thing into a human readable unit.
102      *
103      * @param state from the Thing
104      * @return human readable State
105      */
106     @Override
107     public State onStateUpdateFromHandler(State state) {
108         State result = state;
109
110         if (state instanceof DecimalType) {
111             Unit<?> localUnit = unit;
112             if (localUnit != null) {
113                 result = QuantityType.valueOf(toHumanReadable(((DecimalType) state).longValue()), localUnit);
114             }
115         } else {
116             logger.warn("Unexpected state type: {}", state.getClass().getSimpleName());
117         }
118
119         return result;
120     }
121 }