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