]> git.basschouten.com Git - openhab-addons.git/blob
f61af1d234a89a9a3cc8f3d5c4504dc2d406f4b4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.common;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.DecimalType;
17 import org.openhab.core.library.types.StringType;
18 import org.openhab.core.types.State;
19
20 /**
21  * A value of an LCN variable.
22  * <p>
23  * It internally stores the native LCN value and allows to convert from/into other units.
24  * Some conversions allow to specify whether the source value is absolute or relative.
25  * Relative values are used to create {@link VariableValue}s that can be added/subtracted from
26  * other (absolute) {@link VariableValue}s.
27  *
28  * @author Tobias Jüttner - Initial Contribution
29  * @author Fabian Wolter - Migration to OH2
30  */
31 @NonNullByDefault
32 public class VariableValue {
33     private static final String SENSOR_DEFECTIVE_STATE = "DEFECTIVE";
34
35     /** The absolute, native LCN value. */
36     private final long nativeValue;
37
38     /**
39      * Constructor with native LCN value.
40      *
41      * @param nativeValue the native value
42      */
43     public VariableValue(long nativeValue) {
44         this.nativeValue = nativeValue;
45     }
46
47     /**
48      * Converts to native value. Mask locked bit.
49      *
50      * @return the converted value
51      */
52     public long toNative(boolean useSpecialValues) {
53         if (useSpecialValues) {
54             return nativeValue & 0x7fff;
55         } else {
56             return nativeValue;
57         }
58     }
59
60     /**
61      * Returns the lock state if value comes from a regulator set-point.
62      * If the variable type is not a regulator, the result is undefined.
63      *
64      * @return true if the regulator is locked
65      */
66     public boolean isRegulatorLocked() {
67         return (this.nativeValue & 0x8000) != 0;
68     }
69
70     /**
71      * Returns the defective state of the originating sensor for this variable.
72      *
73      * @return true if the sensor is defective
74      */
75     public boolean isSensorDefective() {
76         return nativeValue == 0x7f00;
77     }
78
79     /**
80      * Returns the configuration state of the variable.
81      *
82      * @return true if the variable is configured via LCN-PRO
83      */
84     public boolean isConfigured() {
85         return this.nativeValue != 0xFFFF;
86     }
87
88     public State getState(Variable variable) {
89         State stateValue;
90         if (variable.useLcnSpecialValues() && isSensorDefective()) {
91             stateValue = new StringType(SENSOR_DEFECTIVE_STATE);
92         } else if (variable.useLcnSpecialValues() && !isConfigured()) {
93             stateValue = new StringType("Not configured in LCN-PRO");
94         } else {
95             stateValue = new DecimalType(toNative(variable.useLcnSpecialValues()));
96         }
97         return stateValue;
98     }
99 }