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