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.common;
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;
21 * A value of an LCN variable.
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.
28 * @author Tobias Jüttner - Initial Contribution
29 * @author Fabian Wolter - Migration to OH2
32 public class VariableValue {
33 private static final String SENSOR_DEFECTIVE_STATE = "DEFECTIVE";
35 /** The absolute, native LCN value. */
36 private final long nativeValue;
39 * Constructor with native LCN value.
41 * @param nativeValue the native value
43 public VariableValue(long nativeValue) {
44 this.nativeValue = nativeValue;
48 * Converts to native value. Mask locked bit.
50 * @return the converted value
52 public long toNative(boolean useSpecialValues) {
53 if (useSpecialValues) {
54 return nativeValue & 0x7fff;
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.
64 * @return true if the regulator is locked
66 public boolean isRegulatorLocked() {
67 return (this.nativeValue & 0x8000) != 0;
71 * Returns the defective state of the originating sensor for this variable.
73 * @return true if the sensor is defective
75 public boolean isSensorDefective() {
76 return nativeValue == 0x7f00;
80 * Returns the configuration state of the variable.
82 * @return true if the variable is configured via LCN-PRO
84 public boolean isConfigured() {
85 return this.nativeValue != 0xFFFF;
88 public State getState(Variable variable) {
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");
95 stateValue = new DecimalType(toNative(variable.useLcnSpecialValues()));