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