2 * Copyright (c) 2010-2022 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.*;
18 import java.time.ZonedDateTime;
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;
31 * The {@link EventChannelHelper} handles specific channels of cameras
33 * @author Gaƫl L'hopital - Initial contribution
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;
43 public EventChannelHelper() {
44 this(GROUP_LAST_EVENT);
47 protected EventChannelHelper(String groupName) {
51 public void setModuleType(ModuleType moduleType) {
52 this.moduleType = moduleType;
55 public void setUrls(String vpnUrl, @Nullable String localUrl) {
56 this.localUrl = localUrl;
58 this.isLocal = localUrl != null;
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
70 lastEventTime = eventTime;
72 super.setNewData(data);
76 protected @Nullable State internalGetEvent(String channelId, Event event) {
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());
95 if (event instanceof HomeEvent) {
96 HomeEvent homeEvent = (HomeEvent) event;
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());
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;
114 return toStringType("%s/vod/%s/index.m3u8", url, videoId);