]> git.basschouten.com Git - openhab-addons.git/blob
f60b1bc416ad553b0716035a7b86db9785f77abc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.bticinosmarther.internal.api.dto;
14
15 import java.util.Optional;
16
17 import javax.measure.quantity.Dimensionless;
18 import javax.measure.quantity.Temperature;
19
20 import org.openhab.binding.bticinosmarther.internal.api.dto.Enums.MeasureUnit;
21 import org.openhab.binding.bticinosmarther.internal.api.exception.SmartherIllegalPropertyValueException;
22 import org.openhab.binding.bticinosmarther.internal.util.StringUtil;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.ImperialUnits;
26 import org.openhab.core.library.unit.SIUnits;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29
30 import com.google.gson.annotations.SerializedName;
31
32 import tec.uom.se.unit.Units;
33
34 /**
35  * The {@code Measure} class defines the dto for Smarther API measure object.
36  *
37  * @author Fabio Possieri - Initial contribution
38  */
39 public class Measure {
40
41     @SerializedName("timeStamp")
42     private String timestamp;
43     private String value;
44     private String unit;
45
46     public String getTimestamp() {
47         return timestamp;
48     }
49
50     /**
51      * Returns the value of this measure.
52      *
53      * @return a string containing the measure value
54      */
55     public String getValue() {
56         return value;
57     }
58
59     /**
60      * Returns the measure unit of this measure.
61      *
62      * @return a string containing the measure unit
63      */
64     public String getUnit() {
65         return unit;
66     }
67
68     /**
69      * Returns the measure unit of this measure.
70      *
71      * @return a {@link MeasureUnit} object representing the measure unit
72      *
73      * @throws {@link SmartherIllegalPropertyValueException}
74      *             if the measure internal raw unit cannot be mapped to any valid measure unit
75      */
76     public MeasureUnit getMeasureUnit() throws SmartherIllegalPropertyValueException {
77         return MeasureUnit.fromValue(unit);
78     }
79
80     /**
81      * Returns the value and measure unit of this measure as a combined {@link State} object.
82      *
83      * @return the value and measure unit
84      *
85      * @throws {@link SmartherIllegalPropertyValueException}
86      *             if the measure internal raw unit cannot be mapped to any valid measure unit
87      */
88     public State toState() throws SmartherIllegalPropertyValueException {
89         State state = UnDefType.UNDEF;
90         final Optional<Double> optValue = (StringUtil.isBlank(value)) ? Optional.empty()
91                 : Optional.of(Double.parseDouble(value));
92
93         switch (MeasureUnit.fromValue(unit)) {
94             case CELSIUS:
95                 state = optValue.<State> map(t -> new QuantityType<Temperature>(new DecimalType(t), SIUnits.CELSIUS))
96                         .orElse(UnDefType.UNDEF);
97                 break;
98             case FAHRENHEIT:
99                 state = optValue
100                         .<State> map(t -> new QuantityType<Temperature>(new DecimalType(t), ImperialUnits.FAHRENHEIT))
101                         .orElse(UnDefType.UNDEF);
102                 break;
103             case PERCENTAGE:
104                 state = optValue.<State> map(t -> new QuantityType<Dimensionless>(new DecimalType(t), Units.PERCENT))
105                         .orElse(UnDefType.UNDEF);
106                 break;
107             case DIMENSIONLESS:
108                 state = optValue.<State> map(t -> new DecimalType(t)).orElse(UnDefType.UNDEF);
109         }
110
111         return state;
112     }
113
114     @Override
115     public String toString() {
116         return (StringUtil.isBlank(timestamp)) ? String.format("value=%s, unit=%s", value, unit)
117                 : String.format("value=%s, unit=%s, timestamp=%s", value, unit, timestamp);
118     }
119 }