]> git.basschouten.com Git - openhab-addons.git/blob
95397d30871c32650527a55df69b63acfecddad3
[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 WeatherUndergroundJsonCurrent} is the Java class used
22  * to map the entry "current_observation" from the JSON response to a Weather
23  * Underground request.
24  *
25  * @author Laurent Garnier - Initial contribution
26  */
27 public class WeatherUndergroundJsonCurrent {
28
29     // Commented members indicate properties returned by the API not used by the binding
30
31     // private Object image;
32
33     // private Location display_location;
34     private Location observation_location;
35     // private Object estimated;
36
37     private String station_id;
38
39     // private String observation_time;
40     // private String observation_time_rfc822;
41     private String observation_epoch;
42     // private String local_time_rfc822;
43     // private String local_epoch;
44     // private String local_tz_short;
45     // private String local_tz_long;
46     // private String local_tz_offset;
47
48     private String weather;
49
50     // private String ;temperature_string;
51     private BigDecimal temp_f;
52     private BigDecimal temp_c;
53
54     private String relative_humidity;
55
56     // private String wind_string;
57     private String wind_dir;
58     private BigDecimal wind_degrees;
59     private BigDecimal wind_mph;
60     private String wind_gust_mph;
61     private BigDecimal wind_kph;
62     private String wind_gust_kph;
63
64     private String pressure_mb;
65     private String pressure_in;
66     private String pressure_trend;
67
68     // private String dewpoint_string;
69     private BigDecimal dewpoint_f;
70     private BigDecimal dewpoint_c;
71
72     // private String heat_index_string;
73     private String heat_index_f;
74     private String heat_index_c;
75
76     // private String windchill_string;
77     private String windchill_f;
78     private String windchill_c;
79
80     // private String feelslike_string;
81     private String feelslike_f;
82     private String feelslike_c;
83
84     private String visibility_mi;
85     private String visibility_km;
86
87     private String solarradiation;
88     private String UV;
89
90     // private String precip_1hr_string;
91     private String precip_1hr_in;
92     private String precip_1hr_metric;
93     // private String precip_today_string;
94     private String precip_today_in;
95     private String precip_today_metric;
96
97     private String icon;
98     private String icon_url;
99     // private String forecast_url;
100     // private String history_url;
101     // private String ob_url;
102
103     // private String nowcast;
104
105     public WeatherUndergroundJsonCurrent() {
106     }
107
108     /**
109      * Get the observation location (full name)
110      *
111      * Used to update the channel current#location
112      *
113      * @return the observation location or null if not defined
114      */
115     public String getLocation() {
116         return (observation_location == null) ? null : observation_location.getFull();
117     }
118
119     /**
120      * Get the station ID
121      *
122      * Used to update the channel current#stationId
123      *
124      * @return the station ID or null if not defined
125      */
126     public String getStationId() {
127         return station_id;
128     }
129
130     /**
131      * Get the observation date and time
132      *
133      * Used to update the channel current#observationTime
134      *
135      * @return the observation date and time or null if not defined
136      */
137     public ZonedDateTime getObservationTime(ZoneId zoneId) {
138         return WeatherUndergroundJsonUtils.convertToZonedDateTime(observation_epoch, zoneId);
139     }
140
141     /**
142      * Get the current weather conditions
143      *
144      * Used to update the channel current#conditions
145      *
146      * @return the current weather conditions or null if not defined
147      */
148     public String getConditions() {
149         return weather;
150     }
151
152     /**
153      * Get the current temperature in degrees Celsius
154      *
155      * Used to update the channel current#temperature
156      *
157      * @return the current temperature in degrees Celsius or null if not defined
158      */
159     public BigDecimal getTemperatureC() {
160         return temp_c;
161     }
162
163     /**
164      * Get the current temperature in degrees Fahrenheit
165      *
166      * Used to update the channel current#temperature
167      *
168      * @return the current temperature in degrees Fahrenheit or null if not defined
169      */
170     public BigDecimal getTemperatureF() {
171         return temp_f;
172     }
173
174     /**
175      * Get the current relative humidity
176      *
177      * Used to update the channel current#relativeHumidity
178      *
179      * @return the current relative humidity or null if not defined
180      */
181     public Integer getRelativeHumidity() {
182         if (relative_humidity != null && !relative_humidity.isEmpty() && !"N/A".equalsIgnoreCase(relative_humidity)) {
183             return WeatherUndergroundJsonUtils.convertToInteger(relative_humidity.replace("%", ""));
184         }
185         return null;
186     }
187
188     /**
189      * Get the wind direction as a text
190      *
191      * Used to update the channel current#windDirection
192      *
193      * @return the wind direction or null if not defined
194      */
195     public String getWindDirection() {
196         return wind_dir;
197     }
198
199     /**
200      * Get the wind direction in degrees
201      *
202      * Used to update the channel current#windDirectionDegrees
203      *
204      * @return the wind direction in degrees or null if not defined
205      */
206     public BigDecimal getWindDirectionDegrees() {
207         return wind_degrees;
208     }
209
210     /**
211      * Get the wind speed in km/h
212      *
213      * Used to update the channel current#windSpeed
214      *
215      * @return the wind speed in km/h or null if not defined
216      */
217     public BigDecimal getWindSpeedKmh() {
218         return wind_kph;
219     }
220
221     /**
222      * Get the wind speed in mph
223      *
224      * Used to update the channel current#windSpeed
225      *
226      * @return the wind speed in mph or null if not defined
227      */
228     public BigDecimal getWindSpeedMph() {
229         return wind_mph;
230     }
231
232     /**
233      * Get the wind gust in km/h
234      *
235      * Used to update the channel current#windGust
236      *
237      * @return the wind gust in km/h or null if not defined
238      */
239     public BigDecimal getWindGustKmh() {
240         return WeatherUndergroundJsonUtils.convertToBigDecimal(wind_gust_kph);
241     }
242
243     /**
244      * Get the wind gust in mph
245      *
246      * Used to update the channel current#windGust
247      *
248      * @return the wind gust in mph or null if not defined
249      */
250     public BigDecimal getWindGustMph() {
251         return WeatherUndergroundJsonUtils.convertToBigDecimal(wind_gust_mph);
252     }
253
254     /**
255      * Get the pressure in hPa
256      *
257      * Used to update the channel current#pressure
258      *
259      * @return the pressure in hPa or null if not defined
260      */
261     public BigDecimal getPressureHPa() {
262         return WeatherUndergroundJsonUtils.convertToBigDecimal(pressure_mb);
263     }
264
265     /**
266      * Get the pressure in inHg
267      *
268      * Used to update the channel current#pressure
269      *
270      * @return the pressure in inHg or null if not defined
271      */
272     public BigDecimal getPressureInHg() {
273         return WeatherUndergroundJsonUtils.convertToBigDecimal(pressure_in);
274     }
275
276     /**
277      * Get the pressure trend
278      *
279      * Used to update the channel current#pressureTrend
280      *
281      * @return the pressure trend or null if not defined
282      */
283     public String getPressureTrend() {
284         return WeatherUndergroundJsonUtils.convertToTrend(pressure_trend);
285     }
286
287     /**
288      * Get the dew point temperature in degrees Celsius
289      *
290      * Used to update the channel current#dewPoint
291      *
292      * @return the dew point temperature in degrees Celsius or null if not defined
293      */
294     public BigDecimal getDewPointC() {
295         return dewpoint_c;
296     }
297
298     /**
299      * Get the dew point temperature in degrees Fahrenheit
300      *
301      * Used to update the channel current#dewPoint
302      *
303      * @return the dew point temperature in degrees Fahrenheit or null if not defined
304      */
305     public BigDecimal getDewPointF() {
306         return dewpoint_f;
307     }
308
309     /**
310      * Get the heat index in degrees Celsius
311      *
312      * Used to update the channel current#heatIndex
313      *
314      * @return the heat index in degrees Celsius or null if not defined
315      */
316     public BigDecimal getHeatIndexC() {
317         return WeatherUndergroundJsonUtils.convertToBigDecimal(heat_index_c);
318     }
319
320     /**
321      * Get the heat index in degrees Fahrenheit
322      *
323      * Used to update the channel current#heatIndex
324      *
325      * @return the heat index in degrees Fahrenheit or null if not defined
326      */
327     public BigDecimal getHeatIndexF() {
328         return WeatherUndergroundJsonUtils.convertToBigDecimal(heat_index_f);
329     }
330
331     /**
332      * Get the wind chill temperature in degrees Celsius
333      *
334      * Used to update the channel current#windChill
335      *
336      * @return the wind chill temperature in degrees Celsius or null if not defined
337      */
338     public BigDecimal getWindChillC() {
339         return WeatherUndergroundJsonUtils.convertToBigDecimal(windchill_c);
340     }
341
342     /**
343      * Get the wind chill temperature in degrees Fahrenheit
344      *
345      * Used to update the channel current#windChill
346      *
347      * @return the wind chill temperature in degrees Fahrenheit or null if not defined
348      */
349     public BigDecimal getWindChillF() {
350         return WeatherUndergroundJsonUtils.convertToBigDecimal(windchill_f);
351     }
352
353     /**
354      * Get the feeling temperature in degrees Celsius
355      *
356      * Used to update the channel current#feelingTemperature
357      *
358      * @return the feeling temperature in degrees Celsius or null if not defined
359      */
360     public BigDecimal getFeelingTemperatureC() {
361         return WeatherUndergroundJsonUtils.convertToBigDecimal(feelslike_c);
362     }
363
364     /**
365      * Get the feeling temperature in degrees Fahrenheit
366      *
367      * Used to update the channel current#feelingTemperature
368      *
369      * @return the feeling temperature in degrees Fahrenheit or null if not defined
370      */
371     public BigDecimal getFeelingTemperatureF() {
372         return WeatherUndergroundJsonUtils.convertToBigDecimal(feelslike_f);
373     }
374
375     /**
376      * Get the visibility in kilometers
377      *
378      * Used to update the channel current#visibility
379      *
380      * @return the visibility in kilometers or null if not defined
381      */
382     public BigDecimal getVisibilityKm() {
383         return WeatherUndergroundJsonUtils.convertToBigDecimal(visibility_km);
384     }
385
386     /**
387      * Get the visibility in miles
388      *
389      * Used to update the channel current#visibility
390      *
391      * @return the visibility in miles or null if not defined
392      */
393     public BigDecimal getVisibilityMi() {
394         return WeatherUndergroundJsonUtils.convertToBigDecimal(visibility_mi);
395     }
396
397     /**
398      * Get the precipitation in the last hour in millimeters
399      *
400      * Used to update the channel current#precipitationHour
401      *
402      * @return the precipitation in the last hour in millimeters or null if not defined
403      */
404     public BigDecimal getPrecipitationHourMm() {
405         BigDecimal result = WeatherUndergroundJsonUtils.convertToBigDecimal(precip_1hr_metric);
406         if ((result != null) && (result.doubleValue() < 0.0)) {
407             result = BigDecimal.ZERO;
408         }
409         return result;
410     }
411
412     /**
413      * Get the precipitation in the last hour in inches
414      *
415      * Used to update the channel current#precipitationHour
416      *
417      * @return the precipitation in the last hour in inches or null if not defined
418      */
419     public BigDecimal getPrecipitationHourIn() {
420         BigDecimal result = WeatherUndergroundJsonUtils.convertToBigDecimal(precip_1hr_in);
421         if ((result != null) && (result.doubleValue() < 0.0)) {
422             result = BigDecimal.ZERO;
423         }
424         return result;
425     }
426
427     /**
428      * Get the precipitation for the full day in millimeters
429      *
430      * Used to update the channel current#precipitationDay
431      *
432      * @return the precipitation for the full day in millimeters or null if not defined
433      */
434     public BigDecimal getPrecipitationDayMm() {
435         return WeatherUndergroundJsonUtils.convertToBigDecimal(precip_today_metric);
436     }
437
438     /**
439      * Get the precipitation for the full day in inches
440      *
441      * Used to update the channel current#precipitationDay
442      *
443      * @return the precipitation for the full day in inches or null if not defined
444      */
445     public BigDecimal getPrecipitationDayIn() {
446         return WeatherUndergroundJsonUtils.convertToBigDecimal(precip_today_in);
447     }
448
449     /**
450      * Get the solar radiation in Watts/sq. m
451      *
452      * Used to update the channel current#solarRadiation
453      *
454      * @return the solar radiation or null if not defined or negative
455      */
456     public BigDecimal getSolarRadiation() {
457         BigDecimal value = WeatherUndergroundJsonUtils.convertToBigDecimal(solarradiation);
458         // We check that the index is not negative
459         if (value != null && value.signum() == -1) {
460             value = null;
461         }
462         return value;
463     }
464
465     /**
466      * Get the UV Index
467      *
468      * Used to update the channel current#UVIndex
469      *
470      * @return the UV Index or null if not defined or negative
471      */
472     public BigDecimal getUVIndex() {
473         BigDecimal value = WeatherUndergroundJsonUtils.convertToBigDecimal(UV);
474         // We check that the index is not negative
475         if (value != null && value.signum() == -1) {
476             value = null;
477         }
478         return value;
479     }
480
481     /**
482      * Get the icon URL representing the current weather conditions
483      *
484      * Used to update the channel current#icon
485      *
486      * @return the icon URL representing the current weather conditions or null if not defined
487      */
488     public URL getIcon() {
489         return WeatherUndergroundJsonUtils.getValidUrl(icon_url);
490     }
491
492     /**
493      * Get the icon key used in the URL representing the current weather conditions
494      *
495      * Used to update the channel current#iconKey
496      *
497      * @return the icon key used in the URL representing the current weather conditions
498      */
499     public String getIconKey() {
500         return icon;
501     }
502
503     class Location {
504         private String full;
505         private String city;
506         private String state;
507         private String state_name;
508         private String country;
509         private String country_iso3166;
510         private String zip;
511         private String magic;
512         private String wmo;
513         private String latitude;
514         private String longitude;
515         private String elevation;
516
517         Location() {
518         }
519
520         public String getFull() {
521             return full;
522         }
523
524         public String getCity() {
525             return city;
526         }
527
528         public String getState() {
529             return state;
530         }
531
532         public String getStateName() {
533             return state_name;
534         }
535
536         public String getCountry() {
537             return country;
538         }
539
540         public String getCountryIso3166() {
541             return country_iso3166;
542         }
543
544         public String getZip() {
545             return zip;
546         }
547
548         public String getMagic() {
549             return magic;
550         }
551
552         public String getWmo() {
553             return wmo;
554         }
555
556         public String getLatitude() {
557             return latitude;
558         }
559
560         public String getLongitude() {
561             return longitude;
562         }
563
564         public String getElevation() {
565             return elevation;
566         }
567     }
568 }