]> git.basschouten.com Git - openhab-addons.git/blob
224c6551a177891b42e5da9657caa700a166286f
[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.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     /** The absolute, native LCN value. */
34     private final long nativeValue;
35
36     /**
37      * Constructor with native LCN value.
38      *
39      * @param nativeValue the native value
40      */
41     public VariableValue(long nativeValue) {
42         this.nativeValue = nativeValue;
43     }
44
45     /**
46      * Converts to native value. Mask locked bit.
47      *
48      * @return the converted value
49      */
50     public long toNative(boolean useSpecialValues) {
51         if (useSpecialValues) {
52             return nativeValue & 0x7fff;
53         } else {
54             return nativeValue;
55         }
56     }
57
58     /**
59      * Returns the lock state if value comes from a regulator set-point.
60      * If the variable type is not a regulator, the result is undefined.
61      *
62      * @return true if the regulator is locked
63      */
64     public boolean isRegulatorLocked() {
65         return (this.nativeValue & 0x8000) != 0;
66     }
67
68     /**
69      * Returns the defective state of the originating sensor for this variable.
70      *
71      * @return true if the sensor is defective
72      */
73     public boolean isSensorDefective() {
74         return nativeValue == 0x7f00;
75     }
76
77     /**
78      * Returns the configuration state of the variable.
79      *
80      * @return true if the variable is configured via LCN-PRO
81      */
82     public boolean isConfigured() {
83         return this.nativeValue != 0xFFFF;
84     }
85
86     public State getState(Variable variable) {
87         State stateValue;
88         if (variable.useLcnSpecialValues() && isSensorDefective()) {
89             stateValue = new StringType("Sensor defective: " + variable);
90         } else if (variable.useLcnSpecialValues() && !isConfigured()) {
91             stateValue = new StringType("Not configured in LCN-PRO: " + variable);
92         } else {
93             stateValue = new DecimalType(toNative(variable.useLcnSpecialValues()));
94         }
95         return stateValue;
96     }
97 }