Search in sources :

Example 1 with WeatherUndergroundJsonData

use of org.eclipse.smarthome.binding.weatherunderground.internal.json.WeatherUndergroundJsonData in project smarthome by eclipse.

the class WeatherUndergroundHandler method getWeatherData.

/**
 * Request new weather data to the Weather Underground service
 *
 * @param features the list of features to be requested
 * @return the weather data object mapping the JSON response or null in case of error
 */
private WeatherUndergroundJsonData getWeatherData(Set<String> features, String location) {
    WeatherUndergroundJsonData result = null;
    boolean resultOk = false;
    String error = null;
    String errorDetail = null;
    String statusDescr = null;
    try {
        // Build a valid URL for the Weather Underground service using
        // the requested features and the thing configuration settings
        WeatherUndergroundConfiguration config = getConfigAs(WeatherUndergroundConfiguration.class);
        String urlStr = URL.replace("%APIKEY%", StringUtils.trimToEmpty(config.apikey));
        urlStr = urlStr.replace("%FEATURES%", String.join("/", features));
        String lang = StringUtils.trimToEmpty(config.language);
        if (lang.isEmpty()) {
            // If language is not set in the configuration, you try deducing it from the system language
            lang = LANG_ISO_TO_WU_CODES.get(localeProvider.getLocale().getLanguage().toUpperCase());
            logger.debug("Use language deduced from system locale {}: {}", localeProvider.getLocale().getLanguage(), lang);
        }
        if (lang == null || lang.isEmpty()) {
            urlStr = urlStr.replace("%SETTINGS%", "");
        } else {
            urlStr = urlStr.replace("%SETTINGS%", "lang:" + lang.toUpperCase());
        }
        urlStr = urlStr.replace("%QUERY%", location);
        logger.debug("URL = {}", urlStr);
        // Run the HTTP request and get the JSON response from Weather Underground
        String response = null;
        try {
            response = HttpUtil.executeUrl("GET", urlStr, FETCH_TIMEOUT_MS);
            logger.debug("weatherData = {}", response);
        } catch (IllegalArgumentException e) {
            // catch Illegal character in path at index XX: http://api.wunderground.com/...
            error = "Error creating URI with location parameter: '" + location + "'";
            errorDetail = e.getMessage();
            statusDescr = "@text/offline.uri-error";
        }
        // Map the JSON response to an object
        result = gson.fromJson(response, WeatherUndergroundJsonData.class);
        if (result.getResponse() == null) {
            errorDetail = "missing response sub-object";
        } else if (result.getResponse().getErrorDescription() != null) {
            if ("keynotfound".equals(result.getResponse().getErrorType())) {
                error = "API key has to be fixed";
                statusDescr = "@text/offline.comm-error-invalid-api-key";
            }
            errorDetail = result.getResponse().getErrorDescription();
        } else {
            resultOk = true;
            for (String feature : features) {
                if (feature.equals(FEATURE_CONDITIONS) && result.getCurrent() == null) {
                    resultOk = false;
                    errorDetail = "missing current_observation sub-object";
                } else if (feature.equals(FEATURE_FORECAST10DAY) && result.getForecast() == null) {
                    resultOk = false;
                    errorDetail = "missing forecast sub-object";
                } else if (feature.equals(FEATURE_GEOLOOKUP) && result.getLocation() == null) {
                    resultOk = false;
                    errorDetail = "missing location sub-object";
                }
            }
        }
        if (!resultOk && error == null) {
            error = "Error in Weather Underground response";
            statusDescr = "@text/offline.comm-error-response";
        }
    } catch (IOException e) {
        error = "Error running Weather Underground request";
        errorDetail = e.getMessage();
        statusDescr = "@text/offline.comm-error-running-request";
    } catch (JsonSyntaxException e) {
        error = "Error parsing Weather Underground response";
        errorDetail = e.getMessage();
        statusDescr = "@text/offline.comm-error-parsing-response";
    }
    // Update the thing status
    if (resultOk) {
        updateStatus(ThingStatus.ONLINE);
    } else {
        logger.debug("{}: {}", error, errorDetail);
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, statusDescr);
    }
    return resultOk ? result : null;
}
Also used : WeatherUndergroundJsonData(org.eclipse.smarthome.binding.weatherunderground.internal.json.WeatherUndergroundJsonData) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) WeatherUndergroundConfiguration(org.eclipse.smarthome.binding.weatherunderground.internal.config.WeatherUndergroundConfiguration)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IOException (java.io.IOException)1 WeatherUndergroundConfiguration (org.eclipse.smarthome.binding.weatherunderground.internal.config.WeatherUndergroundConfiguration)1 WeatherUndergroundJsonData (org.eclipse.smarthome.binding.weatherunderground.internal.json.WeatherUndergroundJsonData)1