]> git.basschouten.com Git - openhab-addons.git/blob
8fe4c3bf09efe91f0149ab83fe02d01448d607e6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.netatmo.internal.utils;
14
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.time.ZonedDateTime;
18 import java.util.Optional;
19
20 import javax.measure.Unit;
21
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;
34
35 /**
36  * This class holds various channel values conversion methods
37  *
38  * @author GaĆ«l L'hopital - Initial contribution
39  *
40  */
41 @NonNullByDefault
42 public class ChannelTypeUtils {
43     private static final int DEFAULT_TIMEOUT_MS = 30000;
44
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);
49         }
50         try {
51             double value = Double.parseDouble(command.toString());
52             return QuantityType.valueOf(value, measureDef.unit);
53         } catch (NumberFormatException ignore) {
54         }
55         return null;
56     }
57
58     public static State toStringType(String pattern, Object... parms) {
59         return toStringType(String.format(pattern, parms));
60     }
61
62     public static State toStringType(@Nullable Enum<?> value) {
63         return (value == null) ? UnDefType.NULL : new StringType(value.name());
64     }
65
66     public static State toStringType(@Nullable String value) {
67         return (value == null) ? UnDefType.NULL : new StringType(value);
68     }
69
70     public static State toDateTimeType(@Nullable ZonedDateTime zonedDateTime) {
71         return (zonedDateTime == null) ? UnDefType.NULL : new DateTimeType(zonedDateTime);
72     }
73
74     public static State toDateTimeType(Optional<ZonedDateTime> zonedDateTime) {
75         return zonedDateTime.map(zdt -> (State) new DateTimeType(zdt)).orElse(UnDefType.NULL);
76     }
77
78     public static State toQuantityType(@Nullable Double value, MeasureClass measureClass) {
79         if (value != null && !value.isNaN()) {
80             Measure measureDef = measureClass.measureDefinition;
81             BigDecimal measure = new BigDecimal(Math.min(measureDef.maxValue, Math.max(measureDef.minValue, value)))
82                     .setScale(measureDef.scale, RoundingMode.HALF_UP);
83             return new QuantityType<>(measure, measureDef.unit);
84         }
85         return UnDefType.NULL;
86     }
87
88     public static State toQuantityType(@Nullable Number value, Unit<?> unit) {
89         return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
90     }
91
92     public static State toRawType(@Nullable String pictureUrl) {
93         if (pictureUrl != null) {
94             // Retrieving local picture can be quite long then extend the timeout.
95             RawType picture = HttpUtil.downloadImage(pictureUrl, DEFAULT_TIMEOUT_MS);
96             if (picture != null) {
97                 return picture;
98             }
99         }
100         return UnDefType.NULL;
101     }
102 }