]> git.basschouten.com Git - openhab-addons.git/blob
d880537d7008193a94862ecb305f91ae35f98829
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.dwdunwetter.internal.data;
14
15 import java.io.IOException;
16 import java.net.URLEncoder;
17 import java.nio.charset.StandardCharsets;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.openhab.core.io.net.http.HttpUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Provides the access to the API Endpoint
26  *
27  * @author Martin Koehler - Initial contribution
28  */
29 public class DwdWarningDataAccess {
30
31     private final Logger logger = LoggerFactory.getLogger(DwdWarningDataAccess.class);
32
33     // URL of the Service
34     private static final String DWD_URL = "https://maps.dwd.de/geoserver/dwd/ows?service=WFS&version=2.0.0&request=GetFeature&typeName=dwd:Warnungen_Gemeinden";
35
36     /**
37      * Returns the raw Data from the Endpoint.
38      * In case of errors or empty cellId value, returns an {@link StringUtils#EMPTY Empty String}.
39      *
40      * @param cellId The warnCell-Id for which the warnings should be returned
41      * @return The raw data received or an empty string.
42      */
43     public String getDataFromEndpoint(String cellId) {
44         try {
45             if (StringUtils.isBlank(cellId)) {
46                 logger.warn("No cellId provided");
47                 return StringUtils.EMPTY;
48             }
49
50             StringBuilder stringBuilder = new StringBuilder();
51             stringBuilder.append(DWD_URL);
52             stringBuilder.append("&CQL_FILTER=");
53             stringBuilder
54                     .append(URLEncoder.encode("WARNCELLID LIKE '" + cellId + "'", StandardCharsets.UTF_8.toString()));
55             logger.debug("Refreshing Data for cell {}", cellId);
56             String rawData = HttpUtil.executeUrl("GET", stringBuilder.toString(), 5000);
57             logger.trace("Raw request: {}", stringBuilder);
58             logger.trace("Raw response: {}", rawData);
59
60             return rawData;
61         } catch (IOException e) {
62             logger.warn("Communication error occurred while getting data: {}", e.getMessage());
63             logger.debug("Communication error trace", e);
64         }
65
66         return StringUtils.EMPTY;
67     }
68 }