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