]> git.basschouten.com Git - openhab-addons.git/blob
909187a1895c2f10f8b435680e23737ce29a080c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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;
14
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.time.Instant;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20
21 import javax.measure.Unit;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.io.net.http.HttpUtil;
26 import org.openhab.core.library.types.DateTimeType;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
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.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  * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
40  *
41  */
42 @NonNullByDefault
43 public class ChannelTypeUtils {
44
45     public static State toStringType(@Nullable String value) {
46         return (value == null) ? UnDefType.NULL : new StringType(value);
47     }
48
49     public static ZonedDateTime toZonedDateTime(Integer netatmoTS, ZoneId zoneId) {
50         Instant i = Instant.ofEpochSecond(netatmoTS);
51         return ZonedDateTime.ofInstant(i, zoneId);
52     }
53
54     public static State toDateTimeType(@Nullable Float netatmoTS, ZoneId zoneId) {
55         return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS.intValue(), zoneId));
56     }
57
58     public static State toDateTimeType(@Nullable Integer netatmoTS, ZoneId zoneId) {
59         return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS, zoneId));
60     }
61
62     public static State toDateTimeType(@Nullable ZonedDateTime zonedDateTime) {
63         return (zonedDateTime == null) ? UnDefType.NULL : new DateTimeType(zonedDateTime);
64     }
65
66     public static State toDecimalType(@Nullable Float value) {
67         return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
68     }
69
70     public static State toDecimalType(@Nullable Integer value) {
71         return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
72     }
73
74     public static State toDecimalType(@Nullable Double value) {
75         return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
76     }
77
78     public static State toDecimalType(float value) {
79         return toDecimalType(new BigDecimal(value));
80     }
81
82     public static State toDecimalType(double value) {
83         return toDecimalType(new BigDecimal(value));
84     }
85
86     public static State toDecimalType(@Nullable BigDecimal decimal) {
87         return decimal == null ? UnDefType.NULL : new DecimalType(decimal.setScale(2, RoundingMode.HALF_UP));
88     }
89
90     public static State toDecimalType(@Nullable String textualDecimal) {
91         return textualDecimal == null ? UnDefType.NULL : new DecimalType(textualDecimal);
92     }
93
94     public static State toOnOffType(@Nullable String yesno) {
95         return "on".equalsIgnoreCase(yesno) ? OnOffType.ON : OnOffType.OFF;
96     }
97
98     public static State toOnOffType(@Nullable Integer value) {
99         return value != null ? (value == 1 ? OnOffType.ON : OnOffType.OFF) : UnDefType.UNDEF;
100     }
101
102     public static State toOnOffType(@Nullable Boolean value) {
103         return value != null ? (value ? OnOffType.ON : OnOffType.OFF) : UnDefType.UNDEF;
104     }
105
106     public static State toQuantityType(@Nullable Float value, Unit<?> unit) {
107         return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
108     }
109
110     public static State toQuantityType(@Nullable Integer value, Unit<?> unit) {
111         return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
112     }
113
114     public static State toQuantityType(@Nullable Double value, Unit<?> unit) {
115         return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
116     }
117
118     public static State toQuantityType(float value, Unit<?> unit) {
119         return toQuantityType(new BigDecimal(value), unit);
120     }
121
122     public static State toQuantityType(int value, Unit<?> unit) {
123         return toQuantityType(new BigDecimal(value), unit);
124     }
125
126     public static State toQuantityType(double value, Unit<?> unit) {
127         return toQuantityType(new BigDecimal(value), unit);
128     }
129
130     public static State toQuantityType(@Nullable BigDecimal value, Unit<?> unit) {
131         return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
132     }
133
134     public static State toRawType(String pictureUrl) {
135         RawType picture = HttpUtil.downloadImage(pictureUrl);
136         return picture == null ? UnDefType.UNDEF : picture;
137     }
138 }