]> git.basschouten.com Git - openhab-addons.git/blob
fd4288476cc81a252a050f441cac6052e8ef7622
[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.URL;
17 import java.time.ZoneId;
18 import java.time.ZonedDateTime;
19
20 /**
21  * The {@link WeatherUndergroundJsonForecastDay} is the Java class used
22  * to map the list element of the entry "forecast.simpleforecast.forecastday"
23  * from the JSON response to a Weather Underground request.
24  *
25  * @author Laurent Garnier - Initial contribution
26  */
27 public class WeatherUndergroundJsonForecastDay {
28
29     // Commented members indicate properties returned by the API not used by the binding
30
31     private ForecastDate date;
32     private Integer period;
33
34     private ForecastTemperature high;
35     private ForecastTemperature low;
36
37     private String conditions;
38
39     private String icon;
40     private String icon_url;
41     // private String skyicon;
42
43     private Integer pop;
44
45     private ForecastPrecipitation qpf_allday;
46     // private ForecastPrecipitation qpf_day;
47     // private ForecastPrecipitation qpf_night;
48     private ForecastPrecipitation snow_allday;
49     // private ForecastPrecipitation snow_day;
50     // private ForecastPrecipitation snow_night;
51
52     private ForecastWind maxwind;
53     private ForecastWind avewind;
54
55     private Integer avehumidity;
56     // private Integer minhumidity;
57     // private Integer maxhumidity;
58
59     public WeatherUndergroundJsonForecastDay() {
60     }
61
62     /**
63      * Get the forecast date and time
64      *
65      * Used to update the channel forecastDayX#forecastTime
66      *
67      * @return the forecast date and time or null if not defined
68      */
69     public ZonedDateTime getForecastTime(ZoneId zoneId) {
70         return WeatherUndergroundJsonUtils.convertToZonedDateTime((date == null) ? null : date.getEpoch(), zoneId);
71     }
72
73     /**
74      * Get the period number
75      *
76      * @return the period number
77      */
78     public Integer getPeriod() {
79         return period;
80     }
81
82     /**
83      * Get the weather forecast conditions
84      *
85      * Used to update the channel forecastDayX#conditions
86      *
87      * @return the weather forecast conditions or null if not defined
88      */
89     public String getConditions() {
90         return conditions;
91     }
92
93     /**
94      * Get the icon URL representing the weather forecast conditions
95      *
96      * Used to update the channel forecastDayX#icon
97      *
98      * @return the icon URL representing the weather forecast conditions or null if not defined
99      */
100     public URL getIcon() {
101         return WeatherUndergroundJsonUtils.getValidUrl(icon_url);
102     }
103
104     /**
105      * Get the icon key used in the URL representing the weather forecast conditions
106      *
107      * Used to update the channel forecastDayX#iconKey
108      *
109      * @return the icon key used in the URL representing the weather forecast conditions
110      */
111     public String getIconKey() {
112         return icon;
113     }
114
115     /**
116      * Get the minimum temperature in degrees Celsius
117      *
118      * Used to update the channel forecastDayX#minTemperature
119      *
120      * @return the minimum temperature in degrees Celsius or null if not defined
121      */
122     public BigDecimal getMinTemperatureC() {
123         return (low == null) ? null : WeatherUndergroundJsonUtils.convertToBigDecimal(low.getCelsius());
124     }
125
126     /**
127      * Get the minimum temperature in degrees Fahrenheit
128      *
129      * Used to update the channel forecastDayX#minTemperature
130      *
131      * @return the minimum temperature in degrees Fahrenheit or null if not defined
132      */
133     public BigDecimal getMinTemperatureF() {
134         return (low == null) ? null : WeatherUndergroundJsonUtils.convertToBigDecimal(low.getFahrenheit());
135     }
136
137     /**
138      * Get the maximum temperature in degrees Celsius
139      *
140      * Used to update the channel forecastDayX#maxTemperature
141      *
142      * @return the maximum temperature in degrees Celsius or null if not defined
143      */
144     public BigDecimal getMaxTemperatureC() {
145         return (high == null) ? null : WeatherUndergroundJsonUtils.convertToBigDecimal(high.getCelsius());
146     }
147
148     /**
149      * Get the maximum temperature in degrees Fahrenheit
150      *
151      * Used to update the channel forecastDayX#maxTemperature
152      *
153      * @return the maximum temperature in degrees Fahrenheit or null if not defined
154      */
155     public BigDecimal getMaxTemperatureF() {
156         return (high == null) ? null : WeatherUndergroundJsonUtils.convertToBigDecimal(high.getFahrenheit());
157     }
158
159     /**
160      * Get the relative humidity
161      *
162      * Used to update the channel forecastDayX#relativeHumidity
163      *
164      * @return the relative humidity or null if not defined
165      */
166     public Integer getRelativeHumidity() {
167         return avehumidity;
168     }
169
170     /**
171      * Get the probability of precipitation
172      *
173      * Used to update the channel forecastDayX#probaPrecipitation
174      *
175      * @return the probability of precipitation or null if not defined
176      */
177     public Integer getProbaPrecipitation() {
178         return pop;
179     }
180
181     /**
182      * Get the precipitation for the full day in millimeters
183      *
184      * Used to update the channel forecastDayX#precipitationDay
185      *
186      * @return the precipitation for the full day in millimeters or null if not defined
187      */
188     public BigDecimal getPrecipitationDayMm() {
189         return (qpf_allday == null) ? null : qpf_allday.mm;
190     }
191
192     /**
193      * Get the precipitation for the full day in inches
194      *
195      * Used to update the channel forecastDayX#precipitation
196      *
197      * @return the precipitation for the full day in inches or null if not defined
198      */
199     public BigDecimal getPrecipitationDayIn() {
200         return (qpf_allday == null) ? null : qpf_allday.in;
201     }
202
203     /**
204      * Get the amount of snow for the full day in centimeters
205      *
206      * Used to update the channel forecastDayX#snow
207      *
208      * @return the amount of snow for the full day in centimeters or null if not defined
209      */
210     public BigDecimal getSnowCm() {
211         return (snow_allday == null) ? null : snow_allday.cm;
212     }
213
214     /**
215      * Get the amount of snow for the full day in inches
216      *
217      * Used to update the channel forecastDayX#snow
218      *
219      * @return the amount of snow for the full day in inches or null if not defined
220      */
221     public BigDecimal getSnowIn() {
222         return (snow_allday == null) ? null : snow_allday.in;
223     }
224
225     /**
226      * Get the maximum wind direction as a text
227      *
228      * Used to update the channel forecastDayX#maxWindDirection
229      *
230      * @return the maximum wind direction or null if not defined
231      */
232     public String getMaxWindDirection() {
233         return (maxwind == null) ? null : maxwind.getDir();
234     }
235
236     /**
237      * Get the maximum wind direction in degrees
238      *
239      * Used to update the channel forecastDayX#maxWindDirectionDegrees
240      *
241      * @return the maximum wind direction in degrees or null if not defined
242      */
243     public BigDecimal getMaxWindDirectionDegrees() {
244         return (maxwind == null) ? null : WeatherUndergroundJsonUtils.convertToBigDecimal(maxwind.getDegrees());
245     }
246
247     /**
248      * Get the maximum wind speed in km/h
249      *
250      * Used to update the channel forecastDayX#maxWindSpeed
251      *
252      * @return the maximum wind speed in km/h or null if not defined
253      */
254     public BigDecimal getMaxWindSpeedKmh() {
255         return (maxwind == null) ? null : maxwind.getKph();
256     }
257
258     /**
259      * Get the maximum wind speed in mph
260      *
261      * Used to update the channel forecastDayX#maxWindSpeed
262      *
263      * @return the maximum wind speed in mph or null if not defined
264      */
265     public BigDecimal getMaxWindSpeedMph() {
266         return (maxwind == null) ? null : maxwind.getMph();
267     }
268
269     /**
270      * Get the average wind direction as a text
271      *
272      * Used to update the channel forecastDayX#averageWindDirection
273      *
274      * @return the average wind direction or null if not defined
275      */
276     public String getAverageWindDirection() {
277         return (avewind == null) ? null : avewind.getDir();
278     }
279
280     /**
281      * Get the average wind direction in degrees
282      *
283      * Used to update the channel forecastDayX#averageWindDirectionDegrees
284      *
285      * @return the average wind direction in degrees or null if not defined
286      */
287     public BigDecimal getAverageWindDirectionDegrees() {
288         return (avewind == null) ? null : WeatherUndergroundJsonUtils.convertToBigDecimal(avewind.getDegrees());
289     }
290
291     /**
292      * Get the average wind speed in km/h
293      *
294      * Used to update the channel forecastDayX#averageWindSpeed
295      *
296      * @return the average wind speed in km/h or null if not defined
297      */
298     public BigDecimal getAverageWindSpeedKmh() {
299         return (avewind == null) ? null : avewind.getKph();
300     }
301
302     /**
303      * Get the average wind speed in mph
304      *
305      * Used to update the channel forecastDayX#averageWindSpeed
306      *
307      * @return the average wind speed in mph or null if not defined
308      */
309     public BigDecimal getAverageWindSpeedMph() {
310         return (avewind == null) ? null : avewind.getMph();
311     }
312
313     class ForecastDate {
314
315         // Commented members indicate properties returned by the API not used by the binding
316
317         private String epoch;
318         // private String pretty;
319         // private Integer day;
320         // private Integer month;
321         // private Integer year;
322         // private Integer yday;
323         // private Integer hour;
324         // private String min;
325         // private Integer sec;
326         // private String isdst;
327         // private String monthname;
328         // private String monthname_short;
329         // private String weekday_short;
330         // private String weekday;
331         // private String ampm;
332         // private String tz_short;
333         // private String tz_long;
334
335         ForecastDate() {
336         }
337
338         public String getEpoch() {
339             return epoch;
340         }
341     }
342
343     class ForecastTemperature {
344         private String fahrenheit;
345         private String celsius;
346
347         ForecastTemperature() {
348         }
349
350         public String getFahrenheit() {
351             return fahrenheit;
352         }
353
354         public String getCelsius() {
355             return celsius;
356         }
357     }
358
359     class ForecastPrecipitation {
360         private BigDecimal in;
361         private BigDecimal mm;
362         private BigDecimal cm;
363
364         ForecastPrecipitation() {
365         }
366
367         public BigDecimal getIn() {
368             return in;
369         }
370
371         public BigDecimal getMm() {
372             return mm;
373         }
374
375         public BigDecimal getCm() {
376             return cm;
377         }
378     }
379
380     class ForecastWind {
381         private BigDecimal mph;
382         private BigDecimal kph;
383         private String dir;
384         private String degrees;
385
386         ForecastWind() {
387         }
388
389         public BigDecimal getMph() {
390             return mph;
391         }
392
393         public BigDecimal getKph() {
394             return kph;
395         }
396
397         public String getDir() {
398             return dir;
399         }
400
401         public String getDegrees() {
402             return degrees;
403         }
404     }
405 }