]> git.basschouten.com Git - openhab-addons.git/blob
efd5ae4b7defb6b25a31d5494e3393948df35aed
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.weatherunderground.internal.json;
14
15 import java.math.BigDecimal;
16 import java.net.MalformedURLException;
17 import java.net.URL;
18 import java.time.DateTimeException;
19 import java.time.Instant;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
22
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link WeatherUndergroundJsonUtils} class contains utilities methods.
27  *
28  * @author Laurent Garnier - Initial contribution
29  */
30 public class WeatherUndergroundJsonUtils {
31
32     private static final String TREND_UP = "up";
33     private static final String TREND_DOWN = "down";
34     private static final String TREND_STABLE = "stable";
35
36     /**
37      * Convert a string representing an Epoch value into a Calendar object
38      *
39      * @param value the Epoch value as a string
40      *
41      * @return the ZonedDateTime object representing the date and time of the Epoch
42      *         or null in case of conversion error
43      */
44     public static ZonedDateTime convertToZonedDateTime(String value, ZoneId zoneId) {
45         if (isValid(value)) {
46             try {
47                 Instant epochSeconds = Instant.ofEpochSecond(Long.valueOf(value));
48                 return ZonedDateTime.ofInstant(epochSeconds, zoneId);
49             } catch (DateTimeException e) {
50                 LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to ZonedDateTime",
51                         value);
52             }
53         }
54
55         return null;
56     }
57
58     /**
59      * Convert a string representing an integer value into an Integer object
60      *
61      * @param value the integer value as a string
62      *
63      * @return the Integer object representing the value or null in case of conversion error
64      */
65     public static Integer convertToInteger(String value) {
66         Integer result = null;
67         if (isValid(value)) {
68             try {
69                 result = Integer.valueOf(value.trim());
70             } catch (NumberFormatException e) {
71                 LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to Integer", value);
72             }
73         }
74         return result;
75     }
76
77     /**
78      * Convert a string representing a decimal value into a BigDecimal object
79      *
80      * @param value the decimal value as a string
81      *
82      * @return the BigDecimal object representing the value or null in case of conversion error
83      */
84     public static BigDecimal convertToBigDecimal(String value) {
85         BigDecimal result = null;
86         if (isValid(value)) {
87             result = new BigDecimal(value.trim());
88         }
89         return result;
90     }
91
92     private static boolean isValid(String value) {
93         return (value != null) && !value.isEmpty() && !"N/A".equalsIgnoreCase(value) && !"NA".equalsIgnoreCase(value)
94                 && !"-".equals(value) && !"--".equals(value);
95     }
96
97     /**
98      * Convert a string representing a URL into a URL object
99      *
100      * @param url the URL as a string
101      *
102      * @return the URL object representing the URL or null in case of invalid URL
103      */
104     public static URL getValidUrl(String url) {
105         URL validUrl = null;
106         if (url != null && !url.trim().isEmpty()) {
107             try {
108                 validUrl = new URL(url.trim());
109             } catch (MalformedURLException e) {
110             }
111         }
112         return validUrl;
113     }
114
115     /**
116      * Convert a string representing a decimal value into a pressure trend constant
117      *
118      * @param value the decimal value as a string
119      *
120      * @return the pressure trend constant representing the value or null in case of conversion error
121      */
122     public static String convertToTrend(String value) {
123         String result = null;
124         if (isValid(value)) {
125             try {
126                 int val = Integer.valueOf(value.trim());
127                 if (val < 0) {
128                     result = TREND_DOWN;
129                 } else if (val > 0) {
130                     result = TREND_UP;
131                 } else {
132                     result = TREND_STABLE;
133                 }
134             } catch (NumberFormatException e) {
135                 LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to Integer", value);
136             }
137         }
138         return result;
139     }
140 }