]> git.basschouten.com Git - openhab-addons.git/commitdiff
[openuv] adressing issue #12688 (#12695)
authorGaël L'hopital <gael@lhopital.org>
Sat, 7 May 2022 18:37:03 +0000 (20:37 +0200)
committerGitHub <noreply@github.com>
Sat, 7 May 2022 18:37:03 +0000 (20:37 +0200)
* openuv adressing issue #12688

Signed-off-by: clinique <gael@lhopital.org>
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVBridgeHandler.java
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVReportHandler.java
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/json/OpenUVResult.java

index ceb8954857e2cb8dbed0077816c42497956253ce..17e267baeedb339b3f22a9ce64fc398d66457b81 100644 (file)
@@ -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<ScheduledFuture<?>> 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,
index 7749b50a779dad940b441eb7b0d5eb2331a98fbb..13fc637f6494a5d8adce3f368c30a76ab9533696 100644 (file)
@@ -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;
     }
index 54bbc07f31a7ae73d05089ad7090d1f258940c40..9da634b66bcb5da51d10caf00564c340169923f3 100644 (file)
@@ -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<FitzpatrickType, @Nullable Integer> 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());