2 * Copyright (c) 2010-2024 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.QuantityType;
29 import org.openhab.core.library.types.RawType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
36 * This class holds various channel values conversion methods
38 * @author Gaƫl L'hopital - Initial contribution
42 public class ChannelTypeUtils {
44 public static @Nullable QuantityType<?> commandToQuantity(Command command, MeasureClass measureClass) {
45 Measure measureDef = measureClass.measureDefinition;
46 if (command instanceof QuantityType<?> quantityCommand) {
47 return quantityCommand.toUnit(measureDef.unit);
50 double value = Double.parseDouble(command.toString());
51 return QuantityType.valueOf(value, measureDef.unit);
52 } catch (NumberFormatException ignore) {
57 public static State toStringType(String pattern, Object... parms) {
58 return toStringType(String.format(pattern, parms));
61 public static State toStringType(@Nullable Enum<?> value) {
62 return (value == null) ? UnDefType.NULL : new StringType(value.name());
65 public static State toStringType(@Nullable String value) {
66 return (value == null) ? UnDefType.NULL : new StringType(value);
69 public static State toDateTimeType(@Nullable ZonedDateTime zonedDateTime) {
70 return (zonedDateTime == null) ? UnDefType.NULL : new DateTimeType(zonedDateTime);
73 public static State toDateTimeType(Optional<ZonedDateTime> zonedDateTime) {
74 return zonedDateTime.map(zdt -> (State) new DateTimeType(zdt)).orElse(UnDefType.NULL);
77 public static State toQuantityType(@Nullable Double value, MeasureClass measureClass) {
78 if (value != null && !value.isNaN()) {
79 Measure measureDef = measureClass.measureDefinition;
80 BigDecimal measure = new BigDecimal(Math.min(measureDef.maxValue, Math.max(measureDef.minValue, value)))
81 .setScale(measureDef.scale, RoundingMode.HALF_UP);
82 return new QuantityType<>(measure, measureDef.unit);
84 return UnDefType.NULL;
87 public static State toQuantityType(@Nullable Number value, Unit<?> unit) {
88 return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
91 public static State toRawType(@Nullable String pictureUrl) {
92 if (pictureUrl != null) {
93 RawType picture = HttpUtil.downloadImage(pictureUrl);
94 if (picture != null) {
98 return UnDefType.NULL;