forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChannelTypeUtils.java
126 lines (101 loc) · 4.59 KB
/
ChannelTypeUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.netatmo.internal;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import javax.measure.Unit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.library.types.DateTimeType;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.QuantityType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.UnDefType;
/**
* This class holds various channel values conversion methods
*
* @author Gaël L'hopital - Initial contribution
* @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
*
*/
@NonNullByDefault
public class ChannelTypeUtils {
public static State toStringType(@Nullable String value) {
return (value == null) ? UnDefType.NULL : new StringType(value);
}
public static ZonedDateTime toZonedDateTime(Integer netatmoTS) {
Instant i = Instant.ofEpochSecond(netatmoTS);
return ZonedDateTime.ofInstant(i, ZoneId.systemDefault());
}
public static State toDateTimeType(@Nullable Float netatmoTS) {
return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS.intValue()));
}
public static State toDateTimeType(@Nullable Integer netatmoTS) {
return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS));
}
public static State toDateTimeType(@Nullable ZonedDateTime zonedDateTime) {
return (zonedDateTime == null) ? UnDefType.NULL : new DateTimeType(zonedDateTime);
}
public static State toDecimalType(@Nullable Float value) {
return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
}
public static State toDecimalType(@Nullable Integer value) {
return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
}
public static State toDecimalType(@Nullable Double value) {
return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
}
public static State toDecimalType(float value) {
return toDecimalType(new BigDecimal(value));
}
public static State toDecimalType(double value) {
return toDecimalType(new BigDecimal(value));
}
public static State toDecimalType(@Nullable BigDecimal decimal) {
return decimal == null ? UnDefType.NULL : new DecimalType(decimal.setScale(2, BigDecimal.ROUND_HALF_UP));
}
public static State toDecimalType(@Nullable String textualDecimal) {
return textualDecimal == null ? UnDefType.NULL : new DecimalType(textualDecimal);
}
public static State toOnOffType(@Nullable String yesno) {
return "on".equalsIgnoreCase(yesno) ? OnOffType.ON : OnOffType.OFF;
}
public static State toOnOffType(@Nullable Integer value) {
return value != null ? (value == 1 ? OnOffType.ON : OnOffType.OFF) : UnDefType.NULL;
}
public static State toQuantityType(@Nullable Float value, Unit<?> unit) {
return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
}
public static State toQuantityType(@Nullable Integer value, Unit<?> unit) {
return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
}
public static State toQuantityType(@Nullable Double value, Unit<?> unit) {
return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
}
public static State toQuantityType(float value, Unit<?> unit) {
return toQuantityType(new BigDecimal(value), unit);
}
public static State toQuantityType(int value, Unit<?> unit) {
return toQuantityType(new BigDecimal(value), unit);
}
public static State toQuantityType(double value, Unit<?> unit) {
return toQuantityType(new BigDecimal(value), unit);
}
public static State toQuantityType(@Nullable BigDecimal value, Unit<?> unit) {
return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
}
}