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.plugwise.internal.protocol.field;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
18 * A relative humidity class that is used for converting from and to Plugwise protocol values.
20 * @author Wouter Born - Initial contribution
23 public class Humidity {
25 private static final String EMPTY_VALUE = "FFFF";
26 private static final double MAX_HEX_VALUE = 65536;
27 private static final double MULTIPLIER = 125;
28 private static final double OFFSET = 6;
30 private final double value;
32 public Humidity(double value) {
36 public Humidity(String hexValue) {
37 if (EMPTY_VALUE.equals(hexValue)) {
38 value = Double.MIN_VALUE;
40 value = MULTIPLIER * (Integer.parseInt(hexValue, 16) / MAX_HEX_VALUE) - OFFSET;
44 public double getValue() {
48 public String toHex() {
49 return String.format("%04X", Math.round((value + OFFSET) / MULTIPLIER * MAX_HEX_VALUE));
53 public String toString() {
54 return String.format("%.3f%%", value);