use of org.eclipse.smarthome.binding.weatherunderground.internal.config.WeatherUndergroundConfiguration in project smarthome by eclipse.
the class WeatherUndergroundHandler method startAutomaticRefresh.
/**
* Start the job refreshing the weather data
*/
private void startAutomaticRefresh() {
if (refreshJob == null || refreshJob.isCancelled()) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
// Request new weather data to the Weather Underground service
updateWeatherData();
// Update all channels from the updated weather data
for (Channel channel : getThing().getChannels()) {
updateChannel(channel.getUID().getId());
}
} catch (Exception e) {
logger.debug("Exception occurred during execution: {}", e.getMessage(), e);
}
}
};
WeatherUndergroundConfiguration config = getConfigAs(WeatherUndergroundConfiguration.class);
int period = (config.refresh != null) ? config.refresh.intValue() : DEFAULT_REFRESH_PERIOD;
refreshJob = scheduler.scheduleWithFixedDelay(runnable, 0, period, TimeUnit.MINUTES);
}
}
use of org.eclipse.smarthome.binding.weatherunderground.internal.config.WeatherUndergroundConfiguration in project smarthome by eclipse.
the class WeatherUndergroundHandler method updateWeatherData.
/**
* Request new current conditions and forecast 10 days to the Weather Underground service
* and store the data in weatherData
*
* @return true if success or false in case of error
*/
private boolean updateWeatherData() {
// Request new weather data to the Weather Underground service
WeatherUndergroundConfiguration config = getConfigAs(WeatherUndergroundConfiguration.class);
weatherData = getWeatherData(USUAL_FEATURES, StringUtils.trimToEmpty(config.location));
return weatherData != null;
}
use of org.eclipse.smarthome.binding.weatherunderground.internal.config.WeatherUndergroundConfiguration 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;
}
use of org.eclipse.smarthome.binding.weatherunderground.internal.config.WeatherUndergroundConfiguration in project smarthome by eclipse.
the class WeatherUndergroundHandler method initialize.
@Override
public void initialize() {
logger.debug("Initializing WeatherUnderground handler.");
WeatherUndergroundConfiguration config = getConfigAs(WeatherUndergroundConfiguration.class);
logger.debug("config apikey = {}", config.apikey);
logger.debug("config location = {}", config.location);
logger.debug("config language = {}", config.language);
logger.debug("config refresh = {}", config.refresh);
boolean validConfig = true;
String errors = "";
String statusDescr = null;
if (StringUtils.trimToNull(config.apikey) == null) {
errors += " Parameter 'apikey' must be configured.";
statusDescr = "@text/offline.conf-error-missing-apikey";
validConfig = false;
}
if (StringUtils.trimToNull(config.location) == null) {
errors += " Parameter 'location' must be configured.";
statusDescr = "@text/offline.conf-error-missing-location";
validConfig = false;
}
if (config.language != null) {
String lang = StringUtils.trimToEmpty(config.language);
if (lang.length() != 2) {
errors += " Parameter 'language' must be 2 letters.";
statusDescr = "@text/offline.conf-error-syntax-language";
validConfig = false;
}
}
if (config.refresh != null && config.refresh < 5) {
errors += " Parameter 'refresh' must be at least 5 minutes.";
statusDescr = "@text/offline.conf-error-min-refresh";
validConfig = false;
}
errors = errors.trim();
if (validConfig) {
startAutomaticRefresh();
} else {
logger.debug("Disabling thing '{}': {}", getThing().getUID(), errors);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, statusDescr);
}
}
Aggregations