2 * Copyright (c) 2010-2020 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.types.State;
28 import org.openhab.core.types.UnDefType;
30 import com.google.gson.annotations.SerializedName;
32 import tec.uom.se.unit.Units;
35 * The {@code Measure} class defines the dto for Smarther API measure object.
37 * @author Fabio Possieri - Initial contribution
39 public class Measure {
41 @SerializedName("timeStamp")
42 private String timestamp;
46 public String getTimestamp() {
51 * Returns the value of this measure.
53 * @return a string containing the measure value
55 public String getValue() {
60 * Returns the measure unit of this measure.
62 * @return a string containing the measure unit
64 public String getUnit() {
69 * Returns the measure unit of this measure.
71 * @return a {@link MeasureUnit} object representing the measure unit
73 * @throws {@link SmartherIllegalPropertyValueException}
74 * if the measure internal raw unit cannot be mapped to any valid measure unit
76 public MeasureUnit getMeasureUnit() throws SmartherIllegalPropertyValueException {
77 return MeasureUnit.fromValue(unit);
81 * Returns the value and measure unit of this measure as a combined {@link State} object.
83 * @return the value and measure unit
85 * @throws {@link SmartherIllegalPropertyValueException}
86 * if the measure internal raw unit cannot be mapped to any valid measure unit
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));
93 switch (MeasureUnit.fromValue(unit)) {
95 state = optValue.<State> map(t -> new QuantityType<Temperature>(new DecimalType(t), SIUnits.CELSIUS))
96 .orElse(UnDefType.UNDEF);
100 .<State> map(t -> new QuantityType<Temperature>(new DecimalType(t), ImperialUnits.FAHRENHEIT))
101 .orElse(UnDefType.UNDEF);
104 state = optValue.<State> map(t -> new QuantityType<Dimensionless>(new DecimalType(t), Units.PERCENT))
105 .orElse(UnDefType.UNDEF);
108 state = optValue.<State> map(t -> new DecimalType(t)).orElse(UnDefType.UNDEF);
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);