]> git.basschouten.com Git - openhab-addons.git/blob
afe71153e9c8a4ca6b7053de4b1327483f71d4d9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal.handler.channelhelper;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils.*;
17
18 import java.util.Set;
19
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;
31
32 /**
33  * The {@link EventChannelHelper} handles specific channels of cameras
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 public class EventChannelHelper extends ChannelHelper {
40     private boolean isLocal;
41     private @Nullable String vpnUrl, localUrl;
42     protected ModuleType moduleType = ModuleType.UNKNOWN;
43
44     public EventChannelHelper(Set<String> providedGroups) {
45         super(providedGroups);
46     }
47
48     public void setModuleType(ModuleType moduleType) {
49         this.moduleType = moduleType;
50     }
51
52     public void setUrls(String vpnUrl, @Nullable String localUrl) {
53         this.localUrl = localUrl;
54         this.vpnUrl = vpnUrl;
55         this.isLocal = localUrl != null;
56     }
57
58     @Override
59     public void setNewData(@Nullable NAObject data) {
60         if (data instanceof Event event) {
61             if (!event.getEventType().validFor(moduleType)) {
62                 return;
63             }
64         }
65         super.setNewData(data);
66     }
67
68     @Override
69     protected @Nullable State internalGetEvent(String channelId, Event event) {
70         switch (channelId) {
71             case CHANNEL_EVENT_TYPE:
72                 return toStringType(event.getEventType());
73             case CHANNEL_EVENT_MESSAGE:
74                 return toStringType(event.getName());
75             case CHANNEL_EVENT_TIME:
76                 return new DateTimeType(event.getTime());
77             case CHANNEL_EVENT_PERSON_ID:
78                 return toStringType(event.getPersonId());
79             case CHANNEL_EVENT_CAMERA_ID:
80                 return toStringType(event.getCameraId());
81             case CHANNEL_EVENT_SUBTYPE:
82                 return event.getSubTypeDescription().map(ChannelTypeUtils::toStringType).orElse(UnDefType.NULL);
83             case CHANNEL_EVENT_SNAPSHOT:
84                 return toRawType(event.getSnapshotUrl());
85             case CHANNEL_EVENT_SNAPSHOT_URL:
86                 return toStringType(event.getSnapshotUrl());
87         }
88         return null;
89     }
90
91     @Override
92     protected @Nullable State internalGetHomeEvent(String channelId, @Nullable String groupId, HomeEvent event) {
93         switch (channelId) {
94             case CHANNEL_EVENT_VIDEO_STATUS:
95                 return event.getVideoId() != null ? toStringType(event.getVideoStatus()) : UnDefType.NULL;
96             case CHANNEL_EVENT_VIDEO_LOCAL_URL:
97                 return getStreamURL(true, event.getVideoId(), event.getVideoStatus());
98             case CHANNEL_EVENT_VIDEO_VPN_URL:
99                 return getStreamURL(false, event.getVideoId(), event.getVideoStatus());
100         }
101         return null;
102     }
103
104     private State getStreamURL(boolean local, @Nullable String videoId, VideoStatus videoStatus) {
105         String url = local ? localUrl : vpnUrl;
106         if ((local && !isLocal) || url == null || videoId == null || videoStatus != VideoStatus.AVAILABLE) {
107             return UnDefType.NULL;
108         }
109         return toStringType("%s/vod/%s/index.m3u8", url, videoId);
110     }
111 }