2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.netatmo.internal;
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;
21 import javax.measure.Unit;
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;
36 * This class holds various channel values conversion methods
38 * @author Gaƫl L'hopital - Initial contribution
39 * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
43 public class ChannelTypeUtils {
45 public static State toStringType(@Nullable String value) {
46 return (value == null) ? UnDefType.NULL : new StringType(value);
49 public static ZonedDateTime toZonedDateTime(Integer netatmoTS, ZoneId zoneId) {
50 Instant i = Instant.ofEpochSecond(netatmoTS);
51 return ZonedDateTime.ofInstant(i, zoneId);
54 public static State toDateTimeType(@Nullable Float netatmoTS, ZoneId zoneId) {
55 return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS.intValue(), zoneId));
58 public static State toDateTimeType(@Nullable Integer netatmoTS, ZoneId zoneId) {
59 return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS, zoneId));
62 public static State toDateTimeType(@Nullable ZonedDateTime zonedDateTime) {
63 return (zonedDateTime == null) ? UnDefType.NULL : new DateTimeType(zonedDateTime);
66 public static State toDecimalType(@Nullable Float value) {
67 return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
70 public static State toDecimalType(@Nullable Integer value) {
71 return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
74 public static State toDecimalType(@Nullable Double value) {
75 return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
78 public static State toDecimalType(float value) {
79 return toDecimalType(new BigDecimal(value));
82 public static State toDecimalType(double value) {
83 return toDecimalType(new BigDecimal(value));
86 public static State toDecimalType(@Nullable BigDecimal decimal) {
87 return decimal == null ? UnDefType.NULL : new DecimalType(decimal.setScale(2, RoundingMode.HALF_UP));
90 public static State toDecimalType(@Nullable String textualDecimal) {
91 return textualDecimal == null ? UnDefType.NULL : new DecimalType(textualDecimal);
94 public static State toOnOffType(@Nullable String yesno) {
95 return "on".equalsIgnoreCase(yesno) ? OnOffType.ON : OnOffType.OFF;
98 public static State toOnOffType(@Nullable Integer value) {
99 return value != null ? (value == 1 ? OnOffType.ON : OnOffType.OFF) : UnDefType.UNDEF;
102 public static State toOnOffType(@Nullable Boolean value) {
103 return value != null ? (value ? OnOffType.ON : OnOffType.OFF) : UnDefType.UNDEF;
106 public static State toQuantityType(@Nullable Float value, Unit<?> unit) {
107 return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
110 public static State toQuantityType(@Nullable Integer value, Unit<?> unit) {
111 return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
114 public static State toQuantityType(@Nullable Double value, Unit<?> unit) {
115 return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
118 public static State toQuantityType(float value, Unit<?> unit) {
119 return toQuantityType(new BigDecimal(value), unit);
122 public static State toQuantityType(int value, Unit<?> unit) {
123 return toQuantityType(new BigDecimal(value), unit);
126 public static State toQuantityType(double value, Unit<?> unit) {
127 return toQuantityType(new BigDecimal(value), unit);
130 public static State toQuantityType(@Nullable BigDecimal value, Unit<?> unit) {
131 return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
134 public static State toRawType(String pictureUrl) {
135 RawType picture = HttpUtil.downloadImage(pictureUrl);
136 return picture == null ? UnDefType.UNDEF : picture;