2 * Copyright (c) 2010-2023 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 /** The absolute, native LCN value. */
34 private final long nativeValue;
37 * Constructor with native LCN value.
39 * @param nativeValue the native value
41 public VariableValue(long nativeValue) {
42 this.nativeValue = nativeValue;
46 * Converts to native value. Mask locked bit.
48 * @return the converted value
50 public long toNative(boolean useSpecialValues) {
51 if (useSpecialValues) {
52 return nativeValue & 0x7fff;
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.
62 * @return true if the regulator is locked
64 public boolean isRegulatorLocked() {
65 return (this.nativeValue & 0x8000) != 0;
69 * Returns the defective state of the originating sensor for this variable.
71 * @return true if the sensor is defective
73 public boolean isSensorDefective() {
74 return nativeValue == 0x7f00;
78 * Returns the configuration state of the variable.
80 * @return true if the variable is configured via LCN-PRO
82 public boolean isConfigured() {
83 return this.nativeValue != 0xFFFF;
86 public State getState(Variable variable) {
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);
93 stateValue = new DecimalType(toNative(variable.useLcnSpecialValues()));