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.netatmo.internal.utils;
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.time.ZonedDateTime;
18 import java.util.Optional;
20 import javax.measure.Unit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.Measure;
25 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass;
26 import org.openhab.core.io.net.http.HttpUtil;
27 import org.openhab.core.library.types.DateTimeType;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.RawType;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
37 * This class holds various channel values conversion methods
39 * @author Gaƫl L'hopital - Initial contribution
43 public class ChannelTypeUtils {
45 public static @Nullable QuantityType<?> commandToQuantity(Command command, MeasureClass measureClass) {
46 Measure measureDef = measureClass.measureDefinition;
47 if (command instanceof QuantityType<?> quantityCommand) {
48 return quantityCommand.toUnit(measureDef.unit);
51 double value = Double.parseDouble(command.toString());
52 return QuantityType.valueOf(value, measureDef.unit);
53 } catch (NumberFormatException ignore) {
58 public static State toStringType(String pattern, Object... parms) {
59 return toStringType(String.format(pattern, parms));
62 public static State toStringType(@Nullable Enum<?> value) {
63 return (value == null) ? UnDefType.NULL : new StringType(value.name());
66 public static State toStringType(@Nullable String value) {
67 return (value == null) ? UnDefType.NULL : new StringType(value);
70 public static State toDateTimeType(@Nullable ZonedDateTime zonedDateTime) {
71 return (zonedDateTime == null) ? UnDefType.NULL : new DateTimeType(zonedDateTime);
74 public static State toDateTimeType(Optional<ZonedDateTime> zonedDateTime) {
75 return zonedDateTime.map(zdt -> (State) new DateTimeType(zdt)).orElse(UnDefType.NULL);
78 public static State toQuantityType(@Nullable Double value, @Nullable MeasureClass measureClass) {
79 if (value != null && !value.isNaN()) {
80 if (measureClass != null) {
81 Measure measureDef = measureClass.measureDefinition;
82 BigDecimal measure = new BigDecimal(Math.min(measureDef.maxValue, Math.max(measureDef.minValue, value)))
83 .setScale(measureDef.scale, RoundingMode.HALF_UP);
84 return new QuantityType<>(measure, measureDef.unit);
86 return new DecimalType(value);
89 return UnDefType.NULL;
92 public static State toQuantityType(@Nullable Number value, Unit<?> unit) {
93 return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
96 public static State toRawType(@Nullable String pictureUrl) {
97 if (pictureUrl != null) {
98 RawType picture = HttpUtil.downloadImage(pictureUrl);
99 if (picture != null) {
103 return UnDefType.NULL;