]> git.basschouten.com Git - openhab-addons.git/blob
f4310e60e3be5f2d05c9ff70077daf845519eeaa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.openweathermap.internal.connection;
14
15 import static org.eclipse.jetty.http.HttpMethod.GET;
16 import static org.eclipse.jetty.http.HttpStatus.*;
17
18 import java.io.UnsupportedEncodingException;
19 import java.net.URLEncoder;
20 import java.nio.charset.StandardCharsets;
21 import java.time.ZoneId;
22 import java.time.ZonedDateTime;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31 import java.util.stream.Collectors;
32
33 import org.eclipse.jdt.annotation.NonNullByDefault;
34 import org.eclipse.jdt.annotation.Nullable;
35 import org.eclipse.jetty.client.HttpClient;
36 import org.eclipse.jetty.client.HttpResponseException;
37 import org.eclipse.jetty.client.api.ContentResponse;
38 import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapAPIConfiguration;
39 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonAirPollutionData;
40 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonDailyForecastData;
41 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonHourlyForecastData;
42 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonUVIndexData;
43 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonWeatherData;
44 import org.openhab.binding.openweathermap.internal.dto.onecall.OpenWeatherMapOneCallAPIData;
45 import org.openhab.binding.openweathermap.internal.dto.onecallhist.OpenWeatherMapOneCallHistAPIData;
46 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAPIHandler;
47 import org.openhab.core.cache.ByteArrayFileCache;
48 import org.openhab.core.cache.ExpiringCacheMap;
49 import org.openhab.core.io.net.http.HttpUtil;
50 import org.openhab.core.library.types.PointType;
51 import org.openhab.core.library.types.RawType;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 import com.google.gson.Gson;
56 import com.google.gson.JsonElement;
57 import com.google.gson.JsonObject;
58 import com.google.gson.JsonParser;
59 import com.google.gson.JsonSyntaxException;
60
61 /**
62  * The {@link OpenWeatherMapConnection} is responsible for handling the connections to OpenWeatherMap API.
63  *
64  * @author Christoph Weitkamp - Initial contribution
65  */
66 @NonNullByDefault
67 public class OpenWeatherMapConnection {
68
69     private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapConnection.class);
70
71     private static final String PROPERTY_MESSAGE = "message";
72
73     private static final String PNG_CONTENT_TYPE = "image/png";
74
75     private static final String PARAM_APPID = "appid";
76     private static final String PARAM_UNITS = "units";
77     private static final String PARAM_LAT = "lat";
78     private static final String PARAM_LON = "lon";
79     private static final String PARAM_LANG = "lang";
80     private static final String PARAM_FORECAST_CNT = "cnt";
81     private static final String PARAM_HISTORY_DATE = "dt";
82     private static final String PARAM_EXCLUDE = "exclude";
83
84     // Current weather data (see https://openweathermap.org/current)
85     private static final String WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather";
86     // 5 day / 3 hour forecast (see https://openweathermap.org/forecast5)
87     private static final String THREE_HOUR_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast";
88     // 16 day / daily forecast (see https://openweathermap.org/forecast16)
89     private static final String DAILY_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast/daily";
90     // UV Index (see https://openweathermap.org/api/uvi)
91     private static final String UVINDEX_URL = "https://api.openweathermap.org/data/2.5/uvi";
92     private static final String UVINDEX_FORECAST_URL = "https://api.openweathermap.org/data/2.5/uvi/forecast";
93     // Air Pollution (see https://openweathermap.org/api/air-pollution)
94     private static final String AIR_POLLUTION_URL = "https://api.openweathermap.org/data/2.5/air_pollution";
95     private static final String AIR_POLLUTION_FORECAST_URL = "https://api.openweathermap.org/data/2.5/air_pollution/forecast";
96     // Weather icons (see https://openweathermap.org/weather-conditions)
97     private static final String ICON_URL = "https://openweathermap.org/img/w/%s.png";
98     // One Call API (see https://openweathermap.org/api/one-call-api )
99     private static final String ONECALL_URL = "https://api.openweathermap.org/data/2.5/onecall";
100     private static final String ONECALL_HISTORY_URL = "https://api.openweathermap.org/data/2.5/onecall/timemachine";
101
102     private final OpenWeatherMapAPIHandler handler;
103     private final HttpClient httpClient;
104
105     private static final ByteArrayFileCache IMAGE_CACHE = new ByteArrayFileCache("org.openhab.binding.openweathermap");
106     private final ExpiringCacheMap<String, String> cache;
107
108     private final Gson gson = new Gson();
109
110     public OpenWeatherMapConnection(OpenWeatherMapAPIHandler handler, HttpClient httpClient) {
111         this.handler = handler;
112         this.httpClient = httpClient;
113
114         OpenWeatherMapAPIConfiguration config = handler.getOpenWeatherMapAPIConfig();
115         cache = new ExpiringCacheMap<>(TimeUnit.MINUTES.toMillis(config.refreshInterval));
116     }
117
118     /**
119      * Requests the current weather data for the given location (see https://openweathermap.org/current).
120      *
121      * @param location location represented as {@link PointType}
122      * @return the current weather data
123      * @throws JsonSyntaxException
124      * @throws OpenWeatherMapCommunicationException
125      * @throws OpenWeatherMapConfigurationException
126      */
127     public synchronized @Nullable OpenWeatherMapJsonWeatherData getWeatherData(@Nullable PointType location)
128             throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
129         return gson.fromJson(
130                 getResponseFromCache(
131                         buildURL(WEATHER_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
132                 OpenWeatherMapJsonWeatherData.class);
133     }
134
135     /**
136      * Requests the hourly forecast data for the given location (see https://openweathermap.org/forecast5).
137      *
138      * @param location location represented as {@link PointType}
139      * @param count number of hours
140      * @return the hourly forecast data
141      * @throws JsonSyntaxException
142      * @throws OpenWeatherMapCommunicationException
143      * @throws OpenWeatherMapConfigurationException
144      */
145     public synchronized @Nullable OpenWeatherMapJsonHourlyForecastData getHourlyForecastData(
146             @Nullable PointType location, int count)
147             throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
148         if (count <= 0) {
149             throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-hours");
150         }
151
152         Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
153         params.put(PARAM_FORECAST_CNT, Integer.toString(count));
154
155         return gson.fromJson(getResponseFromCache(buildURL(THREE_HOUR_FORECAST_URL, params)),
156                 OpenWeatherMapJsonHourlyForecastData.class);
157     }
158
159     /**
160      * Requests the daily forecast data for the given location (see https://openweathermap.org/forecast16).
161      *
162      * @param location location represented as {@link PointType}
163      * @param count number of days
164      * @return the daily forecast data
165      * @throws JsonSyntaxException
166      * @throws OpenWeatherMapCommunicationException
167      * @throws OpenWeatherMapConfigurationException
168      */
169     public synchronized @Nullable OpenWeatherMapJsonDailyForecastData getDailyForecastData(@Nullable PointType location,
170             int count)
171             throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
172         if (count <= 0) {
173             throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-days");
174         }
175
176         Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
177         params.put(PARAM_FORECAST_CNT, Integer.toString(count));
178
179         return gson.fromJson(getResponseFromCache(buildURL(DAILY_FORECAST_URL, params)),
180                 OpenWeatherMapJsonDailyForecastData.class);
181     }
182
183     /**
184      * Requests the UV Index data for the given location (see https://openweathermap.org/api/uvi).
185      *
186      * @param location location represented as {@link PointType}
187      * @return the UV Index data
188      * @throws JsonSyntaxException
189      * @throws OpenWeatherMapCommunicationException
190      * @throws OpenWeatherMapConfigurationException
191      */
192     public synchronized @Nullable OpenWeatherMapJsonUVIndexData getUVIndexData(@Nullable PointType location)
193             throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
194         return gson.fromJson(
195                 getResponseFromCache(
196                         buildURL(UVINDEX_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
197                 OpenWeatherMapJsonUVIndexData.class);
198     }
199
200     /**
201      * Requests the UV Index forecast data for the given location (see https://openweathermap.org/api/uvi).
202      *
203      * @param location location represented as {@link PointType}
204      * @return the UV Index forecast data
205      * @throws JsonSyntaxException
206      * @throws OpenWeatherMapCommunicationException
207      * @throws OpenWeatherMapConfigurationException
208      */
209     public synchronized @Nullable List<OpenWeatherMapJsonUVIndexData> getUVIndexForecastData(
210             @Nullable PointType location, int count)
211             throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
212         if (count <= 0) {
213             throw new OpenWeatherMapConfigurationException(
214                     "@text/offline.conf-error-not-supported-uvindex-number-of-days");
215         }
216
217         Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
218         params.put(PARAM_FORECAST_CNT, Integer.toString(count));
219
220         return Arrays.asList(gson.fromJson(getResponseFromCache(buildURL(UVINDEX_FORECAST_URL, params)),
221                 OpenWeatherMapJsonUVIndexData[].class));
222     }
223
224     /**
225      * Requests the Air Pollution data for the given location (see https://openweathermap.org/api/air-pollution).
226      *
227      * @param location location represented as {@link PointType}
228      * @return the Air Pollution data
229      * @throws JsonSyntaxException
230      * @throws OpenWeatherMapCommunicationException
231      * @throws OpenWeatherMapConfigurationException
232      */
233     public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionData(@Nullable PointType location)
234             throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
235         return gson.fromJson(
236                 getResponseFromCache(
237                         buildURL(AIR_POLLUTION_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
238                 OpenWeatherMapJsonAirPollutionData.class);
239     }
240
241     /**
242      * Requests the Air Pollution forecast data for the given location (see
243      * https://openweathermap.org/api/air-pollution).
244      *
245      * @param location location represented as {@link PointType}
246      * @return the Air Pollution forecast data
247      * @throws JsonSyntaxException
248      * @throws OpenWeatherMapCommunicationException
249      * @throws OpenWeatherMapConfigurationException
250      */
251     public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionForecastData(
252             @Nullable PointType location)
253             throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
254         return gson.fromJson(
255                 getResponseFromCache(buildURL(AIR_POLLUTION_FORECAST_URL,
256                         getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
257                 OpenWeatherMapJsonAirPollutionData.class);
258     }
259
260     /**
261      * Downloads the icon for the given icon id (see https://openweathermap.org/weather-conditions).
262      *
263      * @param iconId the id of the icon
264      * @return the weather icon as {@link RawType}
265      */
266     public static @Nullable RawType getWeatherIcon(String iconId) {
267         if (iconId.isEmpty()) {
268             throw new IllegalArgumentException("Cannot download weather icon as icon id is null.");
269         }
270
271         return downloadWeatherIconFromCache(String.format(ICON_URL, iconId));
272     }
273
274     private static @Nullable RawType downloadWeatherIconFromCache(String url) {
275         if (IMAGE_CACHE.containsKey(url)) {
276             try {
277                 return new RawType(IMAGE_CACHE.get(url), PNG_CONTENT_TYPE);
278             } catch (Exception e) {
279                 LoggerFactory.getLogger(OpenWeatherMapConnection.class)
280                         .trace("Failed to download the content of URL '{}'", url, e);
281             }
282         } else {
283             RawType image = downloadWeatherIcon(url);
284             if (image != null) {
285                 IMAGE_CACHE.put(url, image.getBytes());
286                 return image;
287             }
288         }
289         return null;
290     }
291
292     private static @Nullable RawType downloadWeatherIcon(String url) {
293         return HttpUtil.downloadImage(url);
294     }
295
296     /**
297      * Get Weather data from the One Call API for the given location. See https://openweathermap.org/api/one-call-api
298      * for details.
299      *
300      * @param location location represented as {@link PointType}
301      * @param excludeMinutely if true, will not fetch minutely forecast data from the server
302      * @param excludeHourly if true, will not fetch hourly forecast data from the server
303      * @param excludeDaily if true, will not fetch hourly forecast data from the server
304      * @return
305      * @throws JsonSyntaxException
306      * @throws OpenWeatherMapCommunicationException
307      * @throws OpenWeatherMapConfigurationException
308      */
309     public synchronized @Nullable OpenWeatherMapOneCallAPIData getOneCallAPIData(@Nullable PointType location,
310             boolean excludeMinutely, boolean excludeHourly, boolean excludeDaily, boolean excludeAlerts)
311             throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
312         Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
313         List<String> exclude = new ArrayList<>();
314         if (excludeMinutely) {
315             exclude.add("minutely");
316         }
317         if (excludeHourly) {
318             exclude.add("hourly");
319         }
320         if (excludeDaily) {
321             exclude.add("daily");
322         }
323         if (excludeAlerts) {
324             exclude.add("alerts");
325         }
326         logger.debug("Exclude: '{}'", exclude);
327         if (!exclude.isEmpty()) {
328             params.put(PARAM_EXCLUDE, exclude.stream().collect(Collectors.joining(",")));
329         }
330         return gson.fromJson(getResponseFromCache(buildURL(ONECALL_URL, params)), OpenWeatherMapOneCallAPIData.class);
331     }
332
333     /**
334      * Get the historical weather data from the One Call API for the given location and the given number of days in the
335      * past. As of now, OpenWeatherMap supports this function for up to 5 days in the past. However, this may change in
336      * the future, so we don't enforce this limit here. See https://openweathermap.org/api/one-call-api for details.
337      *
338      * @param location location represented as {@link PointType}
339      * @param days number of days in the past, relative to the current time.
340      * @return
341      * @throws JsonSyntaxException
342      * @throws OpenWeatherMapCommunicationException
343      * @throws OpenWeatherMapConfigurationException
344      */
345     public synchronized @Nullable OpenWeatherMapOneCallHistAPIData getOneCallHistAPIData(@Nullable PointType location,
346             int days)
347             throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
348         Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
349         // the API requests the history as timestamp in Unix time format.
350         params.put(PARAM_HISTORY_DATE,
351                 Long.toString(ZonedDateTime.now(ZoneId.of("UTC")).minusDays(days).toEpochSecond()));
352         return gson.fromJson(getResponseFromCache(buildURL(ONECALL_HISTORY_URL, params)),
353                 OpenWeatherMapOneCallHistAPIData.class);
354     }
355
356     private Map<String, String> getRequestParams(OpenWeatherMapAPIConfiguration config, @Nullable PointType location) {
357         if (location == null) {
358             throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-location");
359         }
360
361         Map<String, String> params = new HashMap<>();
362         // API key (see https://openweathermap.org/appid)
363         String apikey = config.apikey;
364         if (apikey == null || (apikey = apikey.trim()).isEmpty()) {
365             throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-apikey");
366         }
367         params.put(PARAM_APPID, apikey);
368
369         // Units format (see https://openweathermap.org/current#data)
370         params.put(PARAM_UNITS, "metric");
371
372         // By geographic coordinates (see https://openweathermap.org/current#geo)
373         params.put(PARAM_LAT, location.getLatitude().toString());
374         params.put(PARAM_LON, location.getLongitude().toString());
375
376         // Multilingual support (see https://openweathermap.org/current#multi)
377         String language = config.language;
378         if (language != null && !(language = language.trim()).isEmpty()) {
379             params.put(PARAM_LANG, language.toLowerCase());
380         }
381         return params;
382     }
383
384     private String buildURL(String url, Map<String, String> requestParams) {
385         return requestParams.keySet().stream().map(key -> key + "=" + encodeParam(requestParams.get(key)))
386                 .collect(Collectors.joining("&", url + "?", ""));
387     }
388
389     private String encodeParam(@Nullable String value) {
390         if (value == null) {
391             return "";
392         }
393         try {
394             return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
395         } catch (UnsupportedEncodingException e) {
396             logger.debug("UnsupportedEncodingException occurred during execution: {}", e.getLocalizedMessage(), e);
397             return "";
398         }
399     }
400
401     private @Nullable String getResponseFromCache(String url) {
402         return cache.putIfAbsentAndGet(url, () -> getResponse(url));
403     }
404
405     private String getResponse(String url) {
406         try {
407             if (logger.isTraceEnabled()) {
408                 logger.trace("OpenWeatherMap request: URL = '{}'", uglifyApikey(url));
409             }
410             ContentResponse contentResponse = httpClient.newRequest(url).method(GET).timeout(10, TimeUnit.SECONDS)
411                     .send();
412             int httpStatus = contentResponse.getStatus();
413             String content = contentResponse.getContentAsString();
414             String errorMessage = "";
415             logger.trace("OpenWeatherMap response: status = {}, content = '{}'", httpStatus, content);
416             switch (httpStatus) {
417                 case OK_200:
418                     return content;
419                 case BAD_REQUEST_400:
420                 case UNAUTHORIZED_401:
421                 case NOT_FOUND_404:
422                     errorMessage = getErrorMessage(content);
423                     logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
424                     throw new OpenWeatherMapConfigurationException(errorMessage);
425                 case TOO_MANY_REQUESTS_429:
426                     // TODO disable refresh job temporarily (see https://openweathermap.org/appid#Accesslimitation)
427                 default:
428                     errorMessage = getErrorMessage(content);
429                     logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
430                     throw new OpenWeatherMapCommunicationException(errorMessage);
431             }
432         } catch (ExecutionException e) {
433             String errorMessage = e.getLocalizedMessage();
434             logger.trace("Exception occurred during execution: {}", errorMessage, e);
435             if (e.getCause() instanceof HttpResponseException) {
436                 logger.debug("OpenWeatherMap server responded with status code {}: Invalid API key.", UNAUTHORIZED_401);
437                 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-invalid-apikey", e.getCause());
438             } else {
439                 throw new OpenWeatherMapCommunicationException(errorMessage, e.getCause());
440             }
441         } catch (InterruptedException | TimeoutException e) {
442             logger.debug("Exception occurred during execution: {}", e.getLocalizedMessage(), e);
443             throw new OpenWeatherMapCommunicationException(e.getLocalizedMessage(), e.getCause());
444         }
445     }
446
447     private String uglifyApikey(String url) {
448         return url.replaceAll("(appid=)+\\w+", "appid=*****");
449     }
450
451     private String getErrorMessage(String response) {
452         JsonElement jsonResponse = JsonParser.parseString(response);
453         if (jsonResponse.isJsonObject()) {
454             JsonObject json = jsonResponse.getAsJsonObject();
455             if (json.has(PROPERTY_MESSAGE)) {
456                 return json.get(PROPERTY_MESSAGE).getAsString();
457             }
458         }
459         return response;
460     }
461 }