From f6ab246a41de66012cd9699a0dda8f63533dbf5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Sat, 7 May 2022 20:37:03 +0200 Subject: [PATCH] [openuv] adressing issue #12688 (#12695) * openuv adressing issue #12688 Signed-off-by: clinique --- .../internal/handler/OpenUVBridgeHandler.java | 17 +++++++------- .../internal/handler/OpenUVReportHandler.java | 10 +++++---- .../openuv/internal/json/OpenUVResult.java | 22 +++++++++---------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVBridgeHandler.java b/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVBridgeHandler.java index ceb895485..17e267bae 100644 --- a/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVBridgeHandler.java +++ b/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVBridgeHandler.java @@ -17,6 +17,7 @@ import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Collection; +import java.util.Optional; import java.util.Properties; import java.util.Set; import java.util.concurrent.ScheduledFuture; @@ -67,7 +68,7 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler { private final TranslationProvider i18nProvider; private final LocaleProvider localeProvider; - private @Nullable ScheduledFuture reconnectJob; + private Optional> reconnectJob = Optional.empty(); public OpenUVBridgeHandler(Bridge bridge, LocationProvider locationProvider, TranslationProvider i18nProvider, LocaleProvider localeProvider, Gson gson) { @@ -93,11 +94,8 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler { @Override public void dispose() { - ScheduledFuture job = this.reconnectJob; - if (job != null && !job.isCancelled()) { - job.cancel(true); - } - reconnectJob = null; + reconnectJob.ifPresent(job -> job.cancel(true)); + reconnectJob = Optional.empty(); } @Override @@ -129,7 +127,8 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler { throw new OpenUVException(error); } } catch (JsonSyntaxException e) { - logger.debug("No valid json received when calling `{}` : {}", url, jsonData); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, + String.format("Invalid json received when calling `%s` : %s", url, jsonData)); } catch (IOException e) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); } catch (OpenUVException e) { @@ -139,8 +138,8 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, String .format("@text/offline.comm-error-quota-exceeded [ \"%s\" ]", tomorrowMidnight.toString())); - reconnectJob = scheduler.schedule(this::initiateConnexion, - Duration.between(LocalDateTime.now(), tomorrowMidnight).toMinutes(), TimeUnit.MINUTES); + reconnectJob = Optional.of(scheduler.schedule(this::initiateConnexion, + Duration.between(LocalDateTime.now(), tomorrowMidnight).toMinutes(), TimeUnit.MINUTES)); } else { updateStatus(ThingStatus.OFFLINE, e.isApiKeyError() ? ThingStatusDetail.CONFIGURATION_ERROR : ThingStatusDetail.NONE, diff --git a/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVReportHandler.java b/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVReportHandler.java index 7749b50a7..13fc637f6 100644 --- a/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVReportHandler.java +++ b/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVReportHandler.java @@ -40,6 +40,7 @@ import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingStatusInfo; import org.openhab.core.thing.binding.BaseThingHandler; +import org.openhab.core.thing.type.ChannelTypeUID; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; import org.openhab.core.types.State; @@ -208,10 +209,11 @@ public class OpenUVReportHandler extends BaseThingHandler { return openUVData.getUVMaxTime(); case UV_TIME: return openUVData.getUVTime(); - case SAFE_EXPOSURE: - SafeExposureConfiguration configuration = channel.getConfiguration() - .as(SafeExposureConfiguration.class); - return openUVData.getSafeExposureTime(configuration.index); + } + ChannelTypeUID channelType = channel.getChannelTypeUID(); + if (channelType != null && SAFE_EXPOSURE.equals(channelType.getId())) { + SafeExposureConfiguration configuration = channel.getConfiguration().as(SafeExposureConfiguration.class); + return openUVData.getSafeExposureTime(configuration.index); } return UnDefType.NULL; } diff --git a/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/json/OpenUVResult.java b/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/json/OpenUVResult.java index 54bbc07f3..9da634b66 100644 --- a/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/json/OpenUVResult.java +++ b/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/json/OpenUVResult.java @@ -29,8 +29,7 @@ import org.slf4j.LoggerFactory; import com.google.gson.annotations.SerializedName; /** - * The {@link OpenUVResult} is responsible for storing - * the "result" node from the OpenUV JSON response + * The {@link OpenUVResult} is responsible for storing the result node from the OpenUV JSON response * * @author Gaƫl L'hopital - Initial contribution */ @@ -54,10 +53,10 @@ public class OpenUVResult { } private double uv; - private @Nullable ZonedDateTime uvTime; private double uvMax; - private @Nullable ZonedDateTime uvMaxTime; private double ozone; + private @Nullable ZonedDateTime uvTime; + private @Nullable ZonedDateTime uvMaxTime; private @Nullable ZonedDateTime ozoneTime; private Map safeExposureTime = new HashMap<>(); @@ -73,19 +72,20 @@ public class OpenUVResult { return ozone; } + private State getValueOrNull(@Nullable ZonedDateTime value) { + return value == null ? UnDefType.NULL : new DateTimeType(value); + } + public State getUVTime() { - ZonedDateTime value = uvTime; - return value != null ? new DateTimeType(value) : UnDefType.NULL; + return getValueOrNull(uvTime); } public State getUVMaxTime() { - ZonedDateTime value = uvMaxTime; - return value != null ? new DateTimeType(value) : UnDefType.NULL; + return getValueOrNull(uvMaxTime); } public State getOzoneTime() { - ZonedDateTime value = ozoneTime; - return value != null ? new DateTimeType(value) : UnDefType.NULL; + return getValueOrNull(ozoneTime); } public State getSafeExposureTime(String index) { @@ -93,7 +93,7 @@ public class OpenUVResult { FitzpatrickType value = FitzpatrickType.valueOf(index); Integer duration = safeExposureTime.get(value); if (duration != null) { - return new QuantityType<>(duration, Units.MINUTE); + return QuantityType.valueOf(duration, Units.MINUTE); } } catch (IllegalArgumentException e) { logger.warn("Unexpected Fitzpatrick index value '{}' : {}", index, e.getMessage());