2 * Copyright (c) 2010-2020 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.time.Instant;
17 import java.time.ZoneId;
18 import java.time.ZonedDateTime;
20 import javax.measure.Unit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.library.types.DateTimeType;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.types.RawType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
32 import org.openhab.core.io.net.http.HttpUtil;
35 * This class holds various channel values conversion methods
37 * @author Gaƫl L'hopital - Initial contribution
38 * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
42 public class ChannelTypeUtils {
44 public static State toStringType(@Nullable String value) {
45 return (value == null) ? UnDefType.NULL : new StringType(value);
48 public static ZonedDateTime toZonedDateTime(Integer netatmoTS, ZoneId zoneId) {
49 Instant i = Instant.ofEpochSecond(netatmoTS);
50 return ZonedDateTime.ofInstant(i, zoneId);
53 public static State toDateTimeType(@Nullable Float netatmoTS, ZoneId zoneId) {
54 return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS.intValue(), zoneId));
57 public static State toDateTimeType(@Nullable Integer netatmoTS, ZoneId zoneId) {
58 return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS, zoneId));
61 public static State toDateTimeType(@Nullable ZonedDateTime zonedDateTime) {
62 return (zonedDateTime == null) ? UnDefType.NULL : new DateTimeType(zonedDateTime);
65 public static State toDecimalType(@Nullable Float value) {
66 return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
69 public static State toDecimalType(@Nullable Integer value) {
70 return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
73 public static State toDecimalType(@Nullable Double value) {
74 return (value == null) ? UnDefType.NULL : toDecimalType(new BigDecimal(value));
77 public static State toDecimalType(float value) {
78 return toDecimalType(new BigDecimal(value));
81 public static State toDecimalType(double value) {
82 return toDecimalType(new BigDecimal(value));
85 public static State toDecimalType(@Nullable BigDecimal decimal) {
86 return decimal == null ? UnDefType.NULL : new DecimalType(decimal.setScale(2, BigDecimal.ROUND_HALF_UP));
89 public static State toDecimalType(@Nullable String textualDecimal) {
90 return textualDecimal == null ? UnDefType.NULL : new DecimalType(textualDecimal);
93 public static State toOnOffType(@Nullable String yesno) {
94 return "on".equalsIgnoreCase(yesno) ? OnOffType.ON : OnOffType.OFF;
97 public static State toOnOffType(@Nullable Integer value) {
98 return value != null ? (value == 1 ? OnOffType.ON : OnOffType.OFF) : UnDefType.UNDEF;
101 public static State toOnOffType(@Nullable Boolean value) {
102 return value != null ? (value ? OnOffType.ON : OnOffType.OFF) : UnDefType.UNDEF;
105 public static State toQuantityType(@Nullable Float value, Unit<?> unit) {
106 return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
109 public static State toQuantityType(@Nullable Integer value, Unit<?> unit) {
110 return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
113 public static State toQuantityType(@Nullable Double value, Unit<?> unit) {
114 return value == null ? UnDefType.NULL : toQuantityType(new BigDecimal(value), unit);
117 public static State toQuantityType(float value, Unit<?> unit) {
118 return toQuantityType(new BigDecimal(value), unit);
121 public static State toQuantityType(int value, Unit<?> unit) {
122 return toQuantityType(new BigDecimal(value), unit);
125 public static State toQuantityType(double value, Unit<?> unit) {
126 return toQuantityType(new BigDecimal(value), unit);
129 public static State toQuantityType(@Nullable BigDecimal value, Unit<?> unit) {
130 return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
133 public static State toRawType(String pictureUrl) {
134 RawType picture = HttpUtil.downloadImage(pictureUrl);
135 return picture == null ? UnDefType.UNDEF : picture;