]> git.basschouten.com Git - openhab-addons.git/blob
518b1a8b4fce434f2065d3e327a1e6dd686aff6a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.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;
35
36 /**
37  * This class holds various channel values conversion methods
38  *
39  * @author GaĆ«l L'hopital - Initial contribution
40  *
41  */
42 @NonNullByDefault
43 public class ChannelTypeUtils {
44
45     public static @Nullable QuantityType<?> commandToQuantity(Command command, MeasureClass measureClass) {
46         Measure measureDef = measureClass.measureDefinition;
47         if (command instanceof QuantityType<?>) {
48             return ((QuantityType<?>) command).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, @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);
85             } else {
86                 return new DecimalType(value);
87             }
88         }
89         return UnDefType.NULL;
90     }
91
92     public static State toQuantityType(@Nullable Number value, Unit<?> unit) {
93         return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
94     }
95
96     public static State toRawType(@Nullable String pictureUrl) {
97         if (pictureUrl != null) {
98             RawType picture = HttpUtil.downloadImage(pictureUrl);
99             if (picture != null) {
100                 return picture;
101             }
102         }
103         return UnDefType.NULL;
104     }
105 }