]> git.basschouten.com Git - openhab-addons.git/blob
e8b8fdc6ff8d9a4e2a820c850fdd464834907d5d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
21 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.VideoStatus;
22 import org.openhab.binding.netatmo.internal.api.dto.Event;
23 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
24 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
25 import org.openhab.core.library.types.DateTimeType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28
29 /**
30  * The {@link EventChannelHelper} handles specific channels of cameras
31  *
32  * @author GaĆ«l L'hopital - Initial contribution
33  *
34  */
35 @NonNullByDefault
36 public class EventChannelHelper extends ChannelHelper {
37     private boolean isLocal;
38     private @Nullable String vpnUrl, localUrl;
39     protected ModuleType moduleType = ModuleType.UNKNOWN;
40
41     public EventChannelHelper() {
42         this(GROUP_LAST_EVENT);
43     }
44
45     protected EventChannelHelper(String... providedGroups) {
46         super(providedGroups);
47     }
48
49     public void setModuleType(ModuleType moduleType) {
50         this.moduleType = moduleType;
51     }
52
53     public void setUrls(String vpnUrl, @Nullable String localUrl) {
54         this.localUrl = localUrl;
55         this.vpnUrl = vpnUrl;
56         this.isLocal = localUrl != null;
57     }
58
59     @Override
60     public void setNewData(@Nullable NAObject data) {
61         if (data instanceof Event) {
62             Event event = (Event) data;
63             if (!event.getEventType().validFor(moduleType)) {
64                 return;
65             }
66         }
67         super.setNewData(data);
68     }
69
70     @Override
71     protected @Nullable State internalGetEvent(String channelId, Event event) {
72         switch (channelId) {
73             case CHANNEL_EVENT_TYPE:
74                 return toStringType(event.getEventType());
75             case CHANNEL_EVENT_MESSAGE:
76                 return toStringType(event.getName());
77             case CHANNEL_EVENT_TIME:
78                 return new DateTimeType(event.getTime());
79             case CHANNEL_EVENT_PERSON_ID:
80                 return toStringType(event.getPersonId());
81             case CHANNEL_EVENT_CAMERA_ID:
82                 return toStringType(event.getCameraId());
83             case CHANNEL_EVENT_SUBTYPE:
84                 return event.getSubTypeDescription().map(d -> toStringType(d)).orElse(UnDefType.NULL);
85             case CHANNEL_EVENT_SNAPSHOT:
86                 return toRawType(event.getSnapshotUrl());
87             case CHANNEL_EVENT_SNAPSHOT_URL:
88                 return toStringType(event.getSnapshotUrl());
89         }
90         return null;
91     }
92
93     @Override
94     protected @Nullable State internalGetHomeEvent(String channelId, @Nullable String groupId, HomeEvent event) {
95         switch (channelId) {
96             case CHANNEL_EVENT_VIDEO_STATUS:
97                 return event.getVideoId() != null ? toStringType(event.getVideoStatus()) : UnDefType.NULL;
98             case CHANNEL_EVENT_VIDEO_LOCAL_URL:
99                 return getStreamURL(true, event.getVideoId(), event.getVideoStatus());
100             case CHANNEL_EVENT_VIDEO_VPN_URL:
101                 return getStreamURL(false, event.getVideoId(), event.getVideoStatus());
102         }
103         return null;
104     }
105
106     private State getStreamURL(boolean local, @Nullable String videoId, VideoStatus videoStatus) {
107         String url = local ? localUrl : vpnUrl;
108         if ((local && !isLocal) || url == null || videoId == null || videoStatus != VideoStatus.AVAILABLE) {
109             return UnDefType.NULL;
110         }
111         return toStringType("%s/vod/%s/index.m3u8", url, videoId);
112     }
113 }