]> git.basschouten.com Git - openhab-addons.git/blob
711e8d231bf7040cc46d4ac6776feba5e3bbf7cb
[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.plugwise.internal.protocol.field;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * A relative humidity class that is used for converting from and to Plugwise protocol values.
19  *
20  * @author Wouter Born - Initial contribution
21  */
22 @NonNullByDefault
23 public class Humidity {
24
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;
29
30     private final double value;
31
32     public Humidity(double value) {
33         this.value = value;
34     }
35
36     public Humidity(String hexValue) {
37         if (EMPTY_VALUE.equals(hexValue)) {
38             value = Double.MIN_VALUE;
39         } else {
40             value = MULTIPLIER * (Integer.parseInt(hexValue, 16) / MAX_HEX_VALUE) - OFFSET;
41         }
42     }
43
44     public double getValue() {
45         return value;
46     }
47
48     public String toHex() {
49         return String.format("%04X", Math.round((value + OFFSET) / MULTIPLIER * MAX_HEX_VALUE));
50     }
51
52     @Override
53     public String toString() {
54         return String.format("%.3f%%", value);
55     }
56 }