diff --git a/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/discovery/OpenUVDiscoveryService.java b/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/discovery/OpenUVDiscoveryService.java index db825f6c033..868ae3f3c39 100644 --- a/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/discovery/OpenUVDiscoveryService.java +++ b/bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/discovery/OpenUVDiscoveryService.java @@ -34,15 +34,12 @@ import org.slf4j.LoggerFactory; */ @NonNullByDefault public class OpenUVDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService { - private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class); - private static final int DISCOVER_TIMEOUT_SECONDS = 2; + private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class); + private @Nullable OpenUVBridgeHandler bridgeHandler; - /** - * Creates a OpenUVDiscoveryService with enabled autostart. - */ public OpenUVDiscoveryService() { super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS); } @@ -51,9 +48,9 @@ public class OpenUVDiscoveryService extends AbstractDiscoveryService implements public void setThingHandler(ThingHandler handler) { if (handler instanceof OpenUVBridgeHandler) { OpenUVBridgeHandler localHandler = (OpenUVBridgeHandler) handler; - this.bridgeHandler = localHandler; - this.i18nProvider = localHandler.getI18nProvider(); - this.localeProvider = localHandler.getLocaleProvider(); + bridgeHandler = localHandler; + i18nProvider = localHandler.getI18nProvider(); + localeProvider = localHandler.getLocaleProvider(); } } 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 541b53409b5..ceb8954857e 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,8 +17,8 @@ import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Collection; -import java.util.Collections; import java.util.Properties; +import java.util.Set; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -152,7 +152,7 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler { @Override public Collection> getServices() { - return Collections.singleton(OpenUVDiscoveryService.class); + return Set.of(OpenUVDiscoveryService.class); } public @Nullable PointType getLocation() { 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 30940a74d30..7749b50a779 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,7 +40,6 @@ 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; @@ -145,9 +144,8 @@ public class OpenUVReportHandler extends BaseThingHandler { location.getLongitude().toString(), location.getAltitude().toString()); if (openUVData != null) { scheduleUVMaxEvent(openUVData); - getThing().getChannels().forEach(channel -> { - updateChannel(channel.getUID(), openUVData); - }); + getThing().getChannels().stream().filter(channel -> isLinked(channel.getUID().getId())) + .forEach(channel -> updateState(channel.getUID(), getState(channel, openUVData))); updateStatus(ThingStatus.ONLINE); } else { updateStatus(ThingStatus.OFFLINE, bridgeStatusInfo.getStatusDetail(), @@ -191,52 +189,31 @@ public class OpenUVReportHandler extends BaseThingHandler { } } - /** - * Update the channel from the last OpenUV data retrieved - * - * @param channelUID the id identifying the channel to be updated - * @param openUVData - * - */ - private void updateChannel(ChannelUID channelUID, OpenUVResult openUVData) { - Channel channel = getThing().getChannel(channelUID.getId()); - if (channel != null && isLinked(channelUID)) { - ChannelTypeUID channelTypeUID = channel.getChannelTypeUID(); - if (channelTypeUID != null) { - switch (channelTypeUID.getId()) { - case UV_INDEX: - updateState(channelUID, new DecimalType(openUVData.getUv())); - break; - case ALERT_LEVEL: - updateState(channelUID, asAlertLevel(openUVData.getUv())); - break; - case UV_COLOR: - updateState(channelUID, - ALERT_COLORS.getOrDefault(asAlertLevel(openUVData.getUv()), ALERT_UNDEF)); - break; - case UV_MAX: - updateState(channelUID, new DecimalType(openUVData.getUvMax())); - break; - case OZONE: - updateState(channelUID, new QuantityType<>(openUVData.getOzone(), Units.DOBSON_UNIT)); - break; - case OZONE_TIME: - updateState(channelUID, openUVData.getOzoneTime()); - break; - case UV_MAX_TIME: - updateState(channelUID, openUVData.getUVMaxTime()); - break; - case UV_TIME: - updateState(channelUID, openUVData.getUVTime()); - break; - case SAFE_EXPOSURE: - SafeExposureConfiguration configuration = channel.getConfiguration() - .as(SafeExposureConfiguration.class); - updateState(channelUID, openUVData.getSafeExposureTime(configuration.index)); - break; - } - } + private State getState(Channel channel, OpenUVResult openUVData) { + ChannelUID uid = channel.getUID(); + switch (uid.getId()) { + case UV_INDEX: + return new DecimalType(openUVData.getUv()); + case ALERT_LEVEL: + return asAlertLevel(openUVData.getUv()); + case UV_COLOR: + return ALERT_COLORS.getOrDefault(asAlertLevel(openUVData.getUv()), ALERT_UNDEF); + case UV_MAX: + return new DecimalType(openUVData.getUvMax()); + case OZONE: + return new QuantityType<>(openUVData.getOzone(), Units.DOBSON_UNIT); + case OZONE_TIME: + return openUVData.getOzoneTime(); + case UV_MAX_TIME: + return openUVData.getUVMaxTime(); + case UV_TIME: + return openUVData.getUVTime(); + case SAFE_EXPOSURE: + SafeExposureConfiguration configuration = channel.getConfiguration() + .as(SafeExposureConfiguration.class); + return openUVData.getSafeExposureTime(configuration.index); } + return UnDefType.NULL; } private State asAlertLevel(double uv) {