2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.netatmo.internal.handler.channelhelper;
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils.*;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
23 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.VideoStatus;
24 import org.openhab.binding.netatmo.internal.api.dto.Event;
25 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
26 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
27 import org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils;
28 import org.openhab.core.library.types.DateTimeType;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
33 * The {@link EventChannelHelper} handles specific channels of cameras
35 * @author Gaƫl L'hopital - Initial contribution
39 public class EventChannelHelper extends ChannelHelper {
40 private @Nullable String vpnUrl;
41 private @Nullable String localUrl;
42 protected ModuleType moduleType = ModuleType.UNKNOWN;
44 public EventChannelHelper(Set<String> providedGroups) {
45 super(providedGroups);
48 public void setModuleType(ModuleType moduleType) {
49 this.moduleType = moduleType;
52 public void setUrls(@Nullable String vpnUrl, @Nullable String localUrl) {
53 this.localUrl = localUrl;
58 public void setNewData(@Nullable NAObject data) {
59 if (data instanceof Event event && !event.getEventType().validFor(moduleType)) {
62 super.setNewData(data);
66 protected @Nullable State internalGetEvent(String channelId, Event event) {
67 return switch (channelId) {
68 case CHANNEL_EVENT_TYPE -> toStringType(event.getEventType());
69 case CHANNEL_EVENT_MESSAGE -> toStringType(event.getName());
70 case CHANNEL_EVENT_TIME -> new DateTimeType(event.getTime());
71 case CHANNEL_EVENT_PERSON_ID -> toStringType(event.getPersonId());
72 case CHANNEL_EVENT_CAMERA_ID -> toStringType(event.getCameraId());
73 case CHANNEL_EVENT_SUBTYPE ->
74 event.getSubTypeDescription().map(ChannelTypeUtils::toStringType).orElse(UnDefType.NULL);
75 case CHANNEL_EVENT_SNAPSHOT -> toRawType(event.getSnapshotUrl());
76 case CHANNEL_EVENT_SNAPSHOT_URL -> toStringType(event.getSnapshotUrl());
82 protected @Nullable State internalGetHomeEvent(String channelId, @Nullable String groupId, HomeEvent event) {
83 return switch (channelId) {
84 case CHANNEL_EVENT_VIDEO_STATUS ->
85 event.getVideoId() != null ? toStringType(event.getVideoStatus()) : UnDefType.NULL;
86 case CHANNEL_EVENT_VIDEO_LOCAL_URL -> getStreamURL(true, event.getVideoId(), event.getVideoStatus());
87 case CHANNEL_EVENT_VIDEO_VPN_URL -> getStreamURL(false, event.getVideoId(), event.getVideoStatus());
92 private @Nullable String getUrl(boolean local) {
93 return local ? localUrl : vpnUrl;
96 private State getStreamURL(boolean local, @Nullable String videoId, VideoStatus videoStatus) {
97 String url = getUrl(local);
98 if (url == null || videoId == null || videoStatus != VideoStatus.AVAILABLE) {
99 return UnDefType.NULL;
101 return toStringType("%s/vod/%s/index.m3u8", url, videoId);