From 57025ce961913e7bbd7b129bd8aac18dae735bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Mon, 29 Apr 2024 13:35:10 +0200 Subject: [PATCH] [netatmo] Correction of Last-Event group (#16684) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Correction of Last-Event group Signed-off-by: root Signed-off-by: Gaƫl L'hopital --- .../channelhelper/EventChannelHelper.java | 68 ++++++++----------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/EventChannelHelper.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/EventChannelHelper.java index afe71153e9c8a..947e47bf5a448 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/EventChannelHelper.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/EventChannelHelper.java @@ -37,8 +37,8 @@ */ @NonNullByDefault public class EventChannelHelper extends ChannelHelper { - private boolean isLocal; - private @Nullable String vpnUrl, localUrl; + private @Nullable String vpnUrl; + private @Nullable String localUrl; protected ModuleType moduleType = ModuleType.UNKNOWN; public EventChannelHelper(Set providedGroups) { @@ -49,61 +49,53 @@ public void setModuleType(ModuleType moduleType) { this.moduleType = moduleType; } - public void setUrls(String vpnUrl, @Nullable String localUrl) { + public void setUrls(@Nullable String vpnUrl, @Nullable String localUrl) { this.localUrl = localUrl; this.vpnUrl = vpnUrl; - this.isLocal = localUrl != null; } @Override public void setNewData(@Nullable NAObject data) { - if (data instanceof Event event) { - if (!event.getEventType().validFor(moduleType)) { - return; - } + if (data instanceof Event event && !event.getEventType().validFor(moduleType)) { + return; } super.setNewData(data); } @Override protected @Nullable State internalGetEvent(String channelId, Event event) { - switch (channelId) { - case CHANNEL_EVENT_TYPE: - return toStringType(event.getEventType()); - case CHANNEL_EVENT_MESSAGE: - return toStringType(event.getName()); - case CHANNEL_EVENT_TIME: - return new DateTimeType(event.getTime()); - case CHANNEL_EVENT_PERSON_ID: - return toStringType(event.getPersonId()); - case CHANNEL_EVENT_CAMERA_ID: - return toStringType(event.getCameraId()); - case CHANNEL_EVENT_SUBTYPE: - return event.getSubTypeDescription().map(ChannelTypeUtils::toStringType).orElse(UnDefType.NULL); - case CHANNEL_EVENT_SNAPSHOT: - return toRawType(event.getSnapshotUrl()); - case CHANNEL_EVENT_SNAPSHOT_URL: - return toStringType(event.getSnapshotUrl()); - } - return null; + return switch (channelId) { + case CHANNEL_EVENT_TYPE -> toStringType(event.getEventType()); + case CHANNEL_EVENT_MESSAGE -> toStringType(event.getName()); + case CHANNEL_EVENT_TIME -> new DateTimeType(event.getTime()); + case CHANNEL_EVENT_PERSON_ID -> toStringType(event.getPersonId()); + case CHANNEL_EVENT_CAMERA_ID -> toStringType(event.getCameraId()); + case CHANNEL_EVENT_SUBTYPE -> + event.getSubTypeDescription().map(ChannelTypeUtils::toStringType).orElse(UnDefType.NULL); + case CHANNEL_EVENT_SNAPSHOT -> toRawType(event.getSnapshotUrl()); + case CHANNEL_EVENT_SNAPSHOT_URL -> toStringType(event.getSnapshotUrl()); + default -> null; + }; } @Override protected @Nullable State internalGetHomeEvent(String channelId, @Nullable String groupId, HomeEvent event) { - switch (channelId) { - case CHANNEL_EVENT_VIDEO_STATUS: - return event.getVideoId() != null ? toStringType(event.getVideoStatus()) : UnDefType.NULL; - case CHANNEL_EVENT_VIDEO_LOCAL_URL: - return getStreamURL(true, event.getVideoId(), event.getVideoStatus()); - case CHANNEL_EVENT_VIDEO_VPN_URL: - return getStreamURL(false, event.getVideoId(), event.getVideoStatus()); - } - return null; + return switch (channelId) { + case CHANNEL_EVENT_VIDEO_STATUS -> + event.getVideoId() != null ? toStringType(event.getVideoStatus()) : UnDefType.NULL; + case CHANNEL_EVENT_VIDEO_LOCAL_URL -> getStreamURL(true, event.getVideoId(), event.getVideoStatus()); + case CHANNEL_EVENT_VIDEO_VPN_URL -> getStreamURL(false, event.getVideoId(), event.getVideoStatus()); + default -> null; + }; + } + + private @Nullable String getUrl(boolean local) { + return local ? localUrl : vpnUrl; } private State getStreamURL(boolean local, @Nullable String videoId, VideoStatus videoStatus) { - String url = local ? localUrl : vpnUrl; - if ((local && !isLocal) || url == null || videoId == null || videoStatus != VideoStatus.AVAILABLE) { + String url = getUrl(local); + if (url == null || videoId == null || videoStatus != VideoStatus.AVAILABLE) { return UnDefType.NULL; } return toStringType("%s/vod/%s/index.m3u8", url, videoId);