]> git.basschouten.com Git - openhab-addons.git/blob
30f425fa2a12a03890b2c8752fec262c2a186c90
[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
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);
48         }
49         try {
50             double value = Double.parseDouble(command.toString());
51             return QuantityType.valueOf(value, measureDef.unit);
52         } catch (NumberFormatException ignore) {
53         }
54         return null;
55     }
56
57     public static State toStringType(String pattern, Object... parms) {
58         return toStringType(String.format(pattern, parms));
59     }
60
61     public static State toStringType(@Nullable Enum<?> value) {
62         return (value == null) ? UnDefType.NULL : new StringType(value.name());
63     }
64
65     public static State toStringType(@Nullable String value) {
66         return (value == null) ? UnDefType.NULL : new StringType(value);
67     }
68
69     public static State toDateTimeType(@Nullable ZonedDateTime zonedDateTime) {
70         return (zonedDateTime == null) ? UnDefType.NULL : new DateTimeType(zonedDateTime);
71     }
72
73     public static State toDateTimeType(Optional<ZonedDateTime> zonedDateTime) {
74         return zonedDateTime.map(zdt -> (State) new DateTimeType(zdt)).orElse(UnDefType.NULL);
75     }
76
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);
83         }
84         return UnDefType.NULL;
85     }
86
87     public static State toQuantityType(@Nullable Number value, Unit<?> unit) {
88         return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
89     }
90
91     public static State toRawType(@Nullable String pictureUrl) {
92         if (pictureUrl != null) {
93             RawType picture = HttpUtil.downloadImage(pictureUrl);
94             if (picture != null) {
95                 return picture;
96             }
97         }
98         return UnDefType.NULL;
99     }
100 }