Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
WU binding: localized thing status description (#4019)
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored and kaikreuzer committed Aug 11, 2017
1 parent 62e8c6f commit d322dc0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
@@ -0,0 +1,9 @@
# Thing status description
offline.comm-error-invalid-api-key = The API key is invalid.
offline.comm-error-running-request = An error occurred while running the Weather Underground request.
offline.comm-error-parsing-response = An error occurred while parsing the response to the Weather Underground request.
offline.comm-error-response = An error was detected in the response to the Weather Underground request; please check the logs for more details.
offline.conf-error-missing-apikey = The "API key" parameter must be configured.
offline.conf-error-missing-location = The "location" parameter must be configured.
offline.conf-error-syntax-language = The value of the "language" parameter must contain 2 letters.
offline.conf-error-min-refresh = The minimum value of the "refresh interval" parameter must be 5 minutes.
Expand Up @@ -187,3 +187,13 @@ channel-type.config.weatherunderground.snow.SourceUnit.label = Unit
channel-type.config.weatherunderground.snow.SourceUnit.description = Choix de l'unité de quantité de chutes de neige fournie par le service Weather Underground.
channel-type.config.weatherunderground.snow.SourceUnit.option.cm = Centimètres
channel-type.config.weatherunderground.snow.SourceUnit.option.in = Pouces

# Thing status description
offline.comm-error-invalid-api-key = La clé d''accès est invalide.
offline.comm-error-running-request = Une erreur est survenue lors de l''exécution de la requête Weather Underground.
offline.comm-error-parsing-response = Une erreur est survenue lors de l''analyse de la réponse à la requête Weather Underground.
offline.comm-error-response = Une erreur a été détectée dans la réponse à la requête Weather Underground; merci de consulter les logs pour plus de détails.
offline.conf-error-missing-apikey = Le paramètre "clé d''accès" doit être configuré.
offline.conf-error-missing-location = Le paramètre "emplacement des données météo" doit être configuré.
offline.conf-error-syntax-language = La valeur du paramètre "Langue" doit contenir 2 lettres.
offline.conf-error-min-refresh = La valeur minimale du paramètre "fréquence de rafraîchissement" doit être de 5 minutes.
Expand Up @@ -87,24 +87,29 @@ public void initialize() {

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();
Expand All @@ -113,7 +118,7 @@ public void initialize() {
startAutomaticRefresh();
} else {
logger.debug("Disabling thing '{}': {}", getThing().getUID(), errors);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, errors);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, statusDescr);
}
}

Expand Down Expand Up @@ -171,9 +176,10 @@ public void handleCommand(ChannelUID channelUID, Command command) {
* @param channelId the id identifying the channel to be updated
*/
private void updateChannel(String channelId) {
if (isLinked(channelId)) {
Channel channel = getThing().getChannel(channelId);
if (channel != null && isLinked(channelId)) {
// Get the source unit property from the channel when defined
String sourceUnit = (String) getThing().getChannel(channelId).getConfiguration()
String sourceUnit = (String) channel.getConfiguration()
.get(WeatherUndergroundBindingConstants.PROPERTY_SOURCE_UNIT);
if (sourceUnit == null) {
sourceUnit = "";
Expand Down Expand Up @@ -261,6 +267,7 @@ private WeatherUndergroundJsonData getWeatherData(Set<String> features, String l
boolean resultOk = false;
String error = null;
String errorDetail = null;
String statusDescr = null;

try {

Expand Down Expand Up @@ -300,6 +307,7 @@ private WeatherUndergroundJsonData getWeatherData(Set<String> features, String l
} 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 {
Expand All @@ -313,27 +321,30 @@ private WeatherUndergroundJsonData getWeatherData(Set<String> features, String l
errorDetail = "missing forecast sub-object";
} else if (feature.equals(FEATURE_GEOLOOKUP) && result.getLocation() == null) {
resultOk = false;
errorDetail = "missing forecast sub-object";
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, error);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, statusDescr);
}

return resultOk ? result : null;
Expand Down

0 comments on commit d322dc0

Please sign in to comment.