]> git.basschouten.com Git - openhab-addons.git/blob
c9fdfd40b01e7d6289b227e95b2e1b4caaafd915
[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.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.net.URLEncoder;
19 import java.nio.charset.StandardCharsets;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30 import java.util.stream.Collectors;
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.OpenWeatherMapOneCallAPIData;
44 import org.openhab.binding.openweathermap.internal.dto.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.i18n.CommunicationException;
49 import org.openhab.core.i18n.ConfigurationException;
50 import org.openhab.core.io.net.http.HttpUtil;
51 import org.openhab.core.library.types.PointType;
52 import org.openhab.core.library.types.RawType;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import com.google.gson.Gson;
57 import com.google.gson.JsonElement;
58 import com.google.gson.JsonObject;
59 import com.google.gson.JsonParser;
60 import com.google.gson.JsonSyntaxException;
61
62 /**
63  * The {@link OpenWeatherMapConnection} is responsible for handling the connections to OpenWeatherMap API.
64  *
65  * @author Christoph Weitkamp - Initial contribution
66  */
67 @NonNullByDefault
68 public class OpenWeatherMapConnection {
69
70     private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapConnection.class);
71
72     private static final String PROPERTY_MESSAGE = "message";
73
74     private static final String PNG_CONTENT_TYPE = "image/png";
75
76     private static final String PARAM_APPID = "appid";
77     private static final String PARAM_UNITS = "units";
78     private static final String PARAM_LAT = "lat";
79     private static final String PARAM_LON = "lon";
80     private static final String PARAM_LANG = "lang";
81     private static final String PARAM_FORECAST_CNT = "cnt";
82     private static final String PARAM_HISTORY_DATE = "dt";
83     private static final String PARAM_EXCLUDE = "exclude";
84
85     // Current weather data (see https://openweathermap.org/current)
86     private static final String WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather";
87     // 5 day / 3 hour forecast (see https://openweathermap.org/forecast5)
88     private static final String THREE_HOUR_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast";
89     // 16 day / daily forecast (see https://openweathermap.org/forecast16)
90     private static final String DAILY_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast/daily";
91     // UV Index (see https://openweathermap.org/api/uvi)
92     private static final String UVINDEX_URL = "https://api.openweathermap.org/data/2.5/uvi";
93     private static final String UVINDEX_FORECAST_URL = "https://api.openweathermap.org/data/2.5/uvi/forecast";
94     // Air Pollution (see https://openweathermap.org/api/air-pollution)
95     private static final String AIR_POLLUTION_URL = "https://api.openweathermap.org/data/2.5/air_pollution";
96     private static final String AIR_POLLUTION_FORECAST_URL = "https://api.openweathermap.org/data/2.5/air_pollution/forecast";
97     // Weather icons (see https://openweathermap.org/weather-conditions)
98     private static final String ICON_URL = "https://openweathermap.org/img/w/%s.png";
99     // One Call API (see https://openweathermap.org/api/one-call-api )
100     private static final String ONECALL_URL = "https://api.openweathermap.org/data";
101     private static final String ONECALL_DATA_SUFFIX_URL = "onecall";
102     private static final String ONECALL_HISTORY_SUFFIX_URL = "onecall/timemachine";
103
104     private final OpenWeatherMapAPIHandler handler;
105     private final HttpClient httpClient;
106
107     private static final ByteArrayFileCache IMAGE_CACHE = new ByteArrayFileCache("org.openhab.binding.openweathermap");
108     private final ExpiringCacheMap<String, String> cache;
109
110     private final Gson gson = new Gson();
111
112     public OpenWeatherMapConnection(OpenWeatherMapAPIHandler handler, HttpClient httpClient) {
113         this.handler = handler;
114         this.httpClient = httpClient;
115
116         OpenWeatherMapAPIConfiguration config = handler.getOpenWeatherMapAPIConfig();
117         cache = new ExpiringCacheMap<>(TimeUnit.MINUTES.toMillis(config.refreshInterval));
118     }
119
120     /**
121      * Requests the current weather data for the given location (see https://openweathermap.org/current).
122      *
123      * @param location location represented as {@link PointType}
124      * @return the current weather data
125      * @throws JsonSyntaxException
126      * @throws CommunicationException
127      * @throws ConfigurationException
128      */
129     public synchronized @Nullable OpenWeatherMapJsonWeatherData getWeatherData(@Nullable PointType location)
130             throws JsonSyntaxException, CommunicationException, ConfigurationException {
131         return gson.fromJson(
132                 getResponseFromCache(
133                         buildURL(WEATHER_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
134                 OpenWeatherMapJsonWeatherData.class);
135     }
136
137     /**
138      * Requests the hourly forecast data for the given location (see https://openweathermap.org/forecast5).
139      *
140      * @param location location represented as {@link PointType}
141      * @param count number of hours
142      * @return the hourly forecast data
143      * @throws JsonSyntaxException
144      * @throws CommunicationException
145      * @throws ConfigurationException
146      */
147     public synchronized @Nullable OpenWeatherMapJsonHourlyForecastData getHourlyForecastData(
148             @Nullable PointType location, int count)
149             throws JsonSyntaxException, CommunicationException, ConfigurationException {
150         if (count <= 0) {
151             throw new ConfigurationException("@text/offline.conf-error-not-supported-number-of-hours");
152         }
153
154         Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
155         params.put(PARAM_FORECAST_CNT, Integer.toString(count));
156
157         return gson.fromJson(getResponseFromCache(buildURL(THREE_HOUR_FORECAST_URL, params)),
158                 OpenWeatherMapJsonHourlyForecastData.class);
159     }
160
161     /**
162      * Requests the daily forecast data for the given location (see https://openweathermap.org/forecast16).
163      *
164      * @param location location represented as {@link PointType}
165      * @param count number of days
166      * @return the daily forecast data
167      * @throws JsonSyntaxException
168      * @throws CommunicationException
169      * @throws ConfigurationException
170      */
171     public synchronized @Nullable OpenWeatherMapJsonDailyForecastData getDailyForecastData(@Nullable PointType location,
172             int count) throws JsonSyntaxException, CommunicationException, ConfigurationException {
173         if (count <= 0) {
174             throw new ConfigurationException("@text/offline.conf-error-not-supported-number-of-days");
175         }
176
177         Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
178         params.put(PARAM_FORECAST_CNT, Integer.toString(count));
179
180         return gson.fromJson(getResponseFromCache(buildURL(DAILY_FORECAST_URL, params)),
181                 OpenWeatherMapJsonDailyForecastData.class);
182     }
183
184     /**
185      * Requests the UV Index data for the given location (see https://openweathermap.org/api/uvi).
186      *
187      * @param location location represented as {@link PointType}
188      * @return the UV Index data
189      * @throws JsonSyntaxException
190      * @throws CommunicationException
191      * @throws ConfigurationException
192      */
193     public synchronized @Nullable OpenWeatherMapJsonUVIndexData getUVIndexData(@Nullable PointType location)
194             throws JsonSyntaxException, CommunicationException, ConfigurationException {
195         return gson.fromJson(
196                 getResponseFromCache(
197                         buildURL(UVINDEX_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
198                 OpenWeatherMapJsonUVIndexData.class);
199     }
200
201     /**
202      * Requests the UV Index forecast data for the given location (see https://openweathermap.org/api/uvi).
203      *
204      * @param location location represented as {@link PointType}
205      * @return the UV Index forecast data
206      * @throws JsonSyntaxException
207      * @throws CommunicationException
208      * @throws ConfigurationException
209      */
210     public synchronized @Nullable List<OpenWeatherMapJsonUVIndexData> getUVIndexForecastData(
211             @Nullable PointType location, int count)
212             throws JsonSyntaxException, CommunicationException, ConfigurationException {
213         if (count <= 0) {
214             throw new ConfigurationException("@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 CommunicationException
231      * @throws ConfigurationException
232      */
233     public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionData(@Nullable PointType location)
234             throws JsonSyntaxException, CommunicationException, ConfigurationException {
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 CommunicationException
249      * @throws ConfigurationException
250      */
251     public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionForecastData(
252             @Nullable PointType location) throws JsonSyntaxException, CommunicationException, ConfigurationException {
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 One Call API for the given location. See https://openweathermap.org/api/one-call-api
297      * for 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 fetch 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 CommunicationException
306      * @throws ConfigurationException
307      */
308     public synchronized @Nullable OpenWeatherMapOneCallAPIData getOneCallAPIData(@Nullable PointType location,
309             boolean excludeMinutely, boolean excludeHourly, boolean excludeDaily, boolean excludeAlerts)
310             throws JsonSyntaxException, CommunicationException, ConfigurationException {
311         Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
312         List<String> exclude = new ArrayList<>();
313         if (excludeMinutely) {
314             exclude.add("minutely");
315         }
316         if (excludeHourly) {
317             exclude.add("hourly");
318         }
319         if (excludeDaily) {
320             exclude.add("daily");
321         }
322         if (excludeAlerts) {
323             exclude.add("alerts");
324         }
325         logger.debug("Exclude: '{}'", exclude);
326         if (!exclude.isEmpty()) {
327             params.put(PARAM_EXCLUDE, exclude.stream().collect(Collectors.joining(",")));
328         }
329         return gson.fromJson(getResponseFromCache(buildURL(buildOneCallURL(), params)),
330                 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 CommunicationException
343      * @throws ConfigurationException
344      */
345     public synchronized @Nullable OpenWeatherMapOneCallHistAPIData getOneCallHistAPIData(@Nullable PointType location,
346             int days) throws JsonSyntaxException, CommunicationException, ConfigurationException {
347         Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
348         // the API requests the history as timestamp in Unix time format.
349         params.put(PARAM_HISTORY_DATE,
350                 Long.toString(ZonedDateTime.now(ZoneId.of("UTC")).minusDays(days).toEpochSecond()));
351         return gson.fromJson(getResponseFromCache(buildURL(buildOneCallHistoryURL(), params)),
352                 OpenWeatherMapOneCallHistAPIData.class);
353     }
354
355     private Map<String, String> getRequestParams(OpenWeatherMapAPIConfiguration config, @Nullable PointType location) {
356         if (location == null) {
357             throw new ConfigurationException("@text/offline.conf-error-missing-location");
358         }
359
360         Map<String, String> params = new HashMap<>();
361         // API key (see https://openweathermap.org/appid)
362         String apikey = config.apikey;
363         if (apikey == null || (apikey = apikey.trim()).isEmpty()) {
364             throw new ConfigurationException("@text/offline.conf-error-missing-apikey");
365         }
366         params.put(PARAM_APPID, apikey);
367
368         // Units format (see https://openweathermap.org/current#data)
369         params.put(PARAM_UNITS, "metric");
370
371         // By geographic coordinates (see https://openweathermap.org/current#geo)
372         params.put(PARAM_LAT, location.getLatitude().toString());
373         params.put(PARAM_LON, location.getLongitude().toString());
374
375         // Multilingual support (see https://openweathermap.org/current#multi)
376         String language = config.language;
377         if (language != null && !(language = language.trim()).isEmpty()) {
378             params.put(PARAM_LANG, language.toLowerCase());
379         }
380         return params;
381     }
382
383     private String buildURL(String url, Map<String, String> requestParams) {
384         return requestParams.keySet().stream().map(key -> key + "=" + encodeParam(requestParams.get(key)))
385                 .collect(Collectors.joining("&", url + "?", ""));
386     }
387
388     private String buildOneCallURL() {
389         var config = handler.getOpenWeatherMapAPIConfig();
390         return ONECALL_URL + "/" + config.apiVersion + "/" + ONECALL_DATA_SUFFIX_URL;
391     }
392
393     private String buildOneCallHistoryURL() {
394         var config = handler.getOpenWeatherMapAPIConfig();
395         return ONECALL_URL + "/" + config.apiVersion + "/" + ONECALL_HISTORY_SUFFIX_URL;
396     }
397
398     private String encodeParam(@Nullable String value) {
399         return value == null ? "" : URLEncoder.encode(value, StandardCharsets.UTF_8);
400     }
401
402     private @Nullable String getResponseFromCache(String url) {
403         return cache.putIfAbsentAndGet(url, () -> getResponse(url));
404     }
405
406     private String getResponse(String url) {
407         try {
408             if (logger.isTraceEnabled()) {
409                 logger.trace("OpenWeatherMap request: URL = '{}'", uglifyApikey(url));
410             }
411             ContentResponse contentResponse = httpClient.newRequest(url).method(GET).timeout(10, TimeUnit.SECONDS)
412                     .send();
413             int httpStatus = contentResponse.getStatus();
414             String content = contentResponse.getContentAsString();
415             String errorMessage = "";
416             logger.trace("OpenWeatherMap response: status = {}, content = '{}'", httpStatus, content);
417             switch (httpStatus) {
418                 case OK_200:
419                     return content;
420                 case BAD_REQUEST_400:
421                 case UNAUTHORIZED_401:
422                 case NOT_FOUND_404:
423                     errorMessage = getErrorMessage(content);
424                     logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
425                     throw new ConfigurationException(errorMessage);
426                 case TOO_MANY_REQUESTS_429:
427                     // TODO disable refresh job temporarily (see https://openweathermap.org/appid#Accesslimitation)
428                 default:
429                     errorMessage = getErrorMessage(content);
430                     logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
431                     throw new CommunicationException(errorMessage);
432             }
433         } catch (ExecutionException e) {
434             String errorMessage = e.getMessage();
435             logger.debug("ExecutionException occurred during execution: {}", errorMessage, e);
436             if (e.getCause() instanceof HttpResponseException) {
437                 logger.debug("OpenWeatherMap server responded with status code {}: Invalid API key.", UNAUTHORIZED_401);
438                 throw new ConfigurationException("@text/offline.conf-error-invalid-apikey", e.getCause());
439             } else {
440                 throw new CommunicationException(
441                         errorMessage == null ? "@text/offline.communication-error" : errorMessage, e.getCause());
442             }
443         } catch (TimeoutException e) {
444             String errorMessage = e.getMessage();
445             logger.debug("TimeoutException occurred during execution: {}", errorMessage, e);
446             throw new CommunicationException(errorMessage == null ? "@text/offline.communication-error" : errorMessage,
447                     e.getCause());
448         } catch (InterruptedException e) {
449             String errorMessage = e.getMessage();
450             logger.debug("InterruptedException occurred during execution: {}", errorMessage, e);
451             Thread.currentThread().interrupt();
452             throw new CommunicationException(errorMessage == null ? "@text/offline.communication-error" : errorMessage,
453                     e.getCause());
454         }
455     }
456
457     private String uglifyApikey(String url) {
458         return url.replaceAll("(appid=)+\\w+", "appid=*****");
459     }
460
461     private String getErrorMessage(String response) {
462         JsonElement jsonResponse = JsonParser.parseString(response);
463         if (jsonResponse.isJsonObject()) {
464             JsonObject json = jsonResponse.getAsJsonObject();
465             if (json.has(PROPERTY_MESSAGE)) {
466                 return json.get(PROPERTY_MESSAGE).getAsString();
467             }
468         }
469         return response;
470     }
471 }