[netatmo] Correction of Last-Event group (#16684)

* Correction of Last-Event group

Signed-off-by: root <gael@lhopital.org>
Signed-off-by: Gaël L'hopital <gael@lhopital.org>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Gaël L'hopital 2024-04-29 13:35:10 +02:00 committed by Ciprian Pascu
parent d84a4b1154
commit 7b4e383f72

View File

@ -37,8 +37,8 @@ import org.openhab.core.types.UnDefType;
*/ */
@NonNullByDefault @NonNullByDefault
public class EventChannelHelper extends ChannelHelper { public class EventChannelHelper extends ChannelHelper {
private boolean isLocal; private @Nullable String vpnUrl;
private @Nullable String vpnUrl, localUrl; private @Nullable String localUrl;
protected ModuleType moduleType = ModuleType.UNKNOWN; protected ModuleType moduleType = ModuleType.UNKNOWN;
public EventChannelHelper(Set<String> providedGroups) { public EventChannelHelper(Set<String> providedGroups) {
@ -49,61 +49,53 @@ public class EventChannelHelper extends ChannelHelper {
this.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.localUrl = localUrl;
this.vpnUrl = vpnUrl; this.vpnUrl = vpnUrl;
this.isLocal = localUrl != null;
} }
@Override @Override
public void setNewData(@Nullable NAObject data) { public void setNewData(@Nullable NAObject data) {
if (data instanceof Event event) { if (data instanceof Event event && !event.getEventType().validFor(moduleType)) {
if (!event.getEventType().validFor(moduleType)) { return;
return;
}
} }
super.setNewData(data); super.setNewData(data);
} }
@Override @Override
protected @Nullable State internalGetEvent(String channelId, Event event) { protected @Nullable State internalGetEvent(String channelId, Event event) {
switch (channelId) { return switch (channelId) {
case CHANNEL_EVENT_TYPE: case CHANNEL_EVENT_TYPE -> toStringType(event.getEventType());
return toStringType(event.getEventType()); case CHANNEL_EVENT_MESSAGE -> toStringType(event.getName());
case CHANNEL_EVENT_MESSAGE: case CHANNEL_EVENT_TIME -> new DateTimeType(event.getTime());
return toStringType(event.getName()); case CHANNEL_EVENT_PERSON_ID -> toStringType(event.getPersonId());
case CHANNEL_EVENT_TIME: case CHANNEL_EVENT_CAMERA_ID -> toStringType(event.getCameraId());
return new DateTimeType(event.getTime()); case CHANNEL_EVENT_SUBTYPE ->
case CHANNEL_EVENT_PERSON_ID: event.getSubTypeDescription().map(ChannelTypeUtils::toStringType).orElse(UnDefType.NULL);
return toStringType(event.getPersonId()); case CHANNEL_EVENT_SNAPSHOT -> toRawType(event.getSnapshotUrl());
case CHANNEL_EVENT_CAMERA_ID: case CHANNEL_EVENT_SNAPSHOT_URL -> toStringType(event.getSnapshotUrl());
return toStringType(event.getCameraId()); default -> null;
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;
} }
@Override @Override
protected @Nullable State internalGetHomeEvent(String channelId, @Nullable String groupId, HomeEvent event) { protected @Nullable State internalGetHomeEvent(String channelId, @Nullable String groupId, HomeEvent event) {
switch (channelId) { return switch (channelId) {
case CHANNEL_EVENT_VIDEO_STATUS: case CHANNEL_EVENT_VIDEO_STATUS ->
return event.getVideoId() != null ? toStringType(event.getVideoStatus()) : UnDefType.NULL; event.getVideoId() != null ? toStringType(event.getVideoStatus()) : UnDefType.NULL;
case CHANNEL_EVENT_VIDEO_LOCAL_URL: case CHANNEL_EVENT_VIDEO_LOCAL_URL -> getStreamURL(true, event.getVideoId(), event.getVideoStatus());
return getStreamURL(true, event.getVideoId(), event.getVideoStatus()); case CHANNEL_EVENT_VIDEO_VPN_URL -> getStreamURL(false, event.getVideoId(), event.getVideoStatus());
case CHANNEL_EVENT_VIDEO_VPN_URL: default -> null;
return getStreamURL(false, event.getVideoId(), event.getVideoStatus()); };
} }
return null;
private @Nullable String getUrl(boolean local) {
return local ? localUrl : vpnUrl;
} }
private State getStreamURL(boolean local, @Nullable String videoId, VideoStatus videoStatus) { private State getStreamURL(boolean local, @Nullable String videoId, VideoStatus videoStatus) {
String url = local ? localUrl : vpnUrl; String url = getUrl(local);
if ((local && !isLocal) || url == null || videoId == null || videoStatus != VideoStatus.AVAILABLE) { if (url == null || videoId == null || videoStatus != VideoStatus.AVAILABLE) {
return UnDefType.NULL; return UnDefType.NULL;
} }
return toStringType("%s/vod/%s/index.m3u8", url, videoId); return toStringType("%s/vod/%s/index.m3u8", url, videoId);