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