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.bticinosmarther.internal.api.dto;
15 import java.util.Optional;
17 import javax.measure.quantity.Dimensionless;
18 import javax.measure.quantity.Temperature;
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;
31 import com.google.gson.annotations.SerializedName;
34 * The {@code Measure} class defines the dto for Smarther API measure object.
36 * @author Fabio Possieri - Initial contribution
38 public class Measure {
40 @SerializedName("timeStamp")
41 private String timestamp;
45 public String getTimestamp() {
50 * Returns the value of this measure.
52 * @return a string containing the measure value
54 public String getValue() {
59 * Returns the measure unit of this measure.
61 * @return a string containing the measure unit
63 public String getUnit() {
68 * Returns the measure unit of this measure.
70 * @return a {@link MeasureUnit} object representing the measure unit
72 * @throws {@link SmartherIllegalPropertyValueException}
73 * if the measure internal raw unit cannot be mapped to any valid measure unit
75 public MeasureUnit getMeasureUnit() throws SmartherIllegalPropertyValueException {
76 return MeasureUnit.fromValue(unit);
80 * Returns the value and measure unit of this measure as a combined {@link State} object.
82 * @return the value and measure unit
84 * @throws {@link SmartherIllegalPropertyValueException}
85 * if the measure internal raw unit cannot be mapped to any valid measure unit
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));
92 switch (MeasureUnit.fromValue(unit)) {
94 state = optValue.<State> map(t -> new QuantityType<Temperature>(new DecimalType(t), SIUnits.CELSIUS))
95 .orElse(UnDefType.UNDEF);
99 .<State> map(t -> new QuantityType<Temperature>(new DecimalType(t), ImperialUnits.FAHRENHEIT))
100 .orElse(UnDefType.UNDEF);
103 state = optValue.<State> map(t -> new QuantityType<Dimensionless>(new DecimalType(t), Units.PERCENT))
104 .orElse(UnDefType.UNDEF);
107 state = optValue.<State> map(t -> new DecimalType(t)).orElse(UnDefType.UNDEF);
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);