]> git.basschouten.com Git - openhab-addons.git/blob
e0d6593491692628a771eb06f91ac58e269bd8ab
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.fronius.internal.api;
14
15 import javax.measure.Unit;
16
17 import org.openhab.core.library.types.QuantityType;
18 import org.openhab.core.types.util.UnitUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.gson.annotations.SerializedName;
23
24 /**
25  * The {@link InverterRealtimeResponse} is responsible for storing
26  * a value
27  *
28  * @author Thomas Rokohl - Initial contribution
29  * @author Jimmy Tanagra - Add conversion to QuantityType
30  */
31 public class ValueUnit {
32
33     @SerializedName("Value")
34     private double value;
35     @SerializedName("Unit")
36     private String unit = "";
37
38     public double getValue() {
39         return value;
40     }
41
42     public void setValue(double value) {
43         this.value = value;
44     }
45
46     public String getUnit() {
47         return this.unit == null ? "" : this.unit;
48     }
49
50     public void setUnit(String unit) {
51         this.unit = unit;
52     }
53
54     public QuantityType<?> asQuantityType() {
55         Unit<?> unit = UnitUtils.parseUnit(getUnit());
56         if (unit == null) {
57             final Logger logger = LoggerFactory.getLogger(ValueUnit.class);
58             logger.debug("The unit for ValueUnit ({})/({}) cannot be parsed", value, this.unit);
59             unit = QuantityType.ONE.getUnit();
60         }
61         return new QuantityType<>(value, unit);
62     }
63 }