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