[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
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<String> providedGroups) {
@ -49,61 +49,53 @@ public class EventChannelHelper extends ChannelHelper {
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);