2 * Copyright (c) 2010-2024 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.api.dto;
15 import java.time.ZonedDateTime;
16 import java.util.List;
17 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.netatmo.internal.api.ApiResponse;
22 import org.openhab.binding.netatmo.internal.api.BodyResponse;
23 import org.openhab.binding.netatmo.internal.api.data.EventSubType;
24 import org.openhab.binding.netatmo.internal.api.data.EventType;
25 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.EventCategory;
26 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.VideoStatus;
29 * The {@link HomeEvent} holds information transferred by the webhook about a home event.
31 * @author Gaƫl L'hopital - Initial contribution
36 public class HomeEvent extends Event {
37 public class NAEventsDataResponse extends ApiResponse<BodyResponse<Home>> {
40 private record Snapshot(String url, ZonedDateTime expiresAt) {
41 // If the snapshot is expired we consider it as not available, so do not provide the url
42 public @Nullable String url() {
43 return expiresAt.isAfter(ZonedDateTime.now().withZoneSameInstant(expiresAt.getZone())) ? url : null;
47 private ZonedDateTime time = ZonedDateTime.now();
48 private @Nullable String personId;
49 private EventCategory category = EventCategory.UNKNOWN;
50 private @Nullable Snapshot snapshot;
51 private @Nullable Snapshot vignette;
52 private @Nullable String videoId;
53 private VideoStatus videoStatus = VideoStatus.UNKNOWN;
54 private boolean isArrival;
55 private List<HomeEvent> subevents = List.of();
58 public ZonedDateTime getTime() {
63 public @Nullable String getPersonId() {
67 public @Nullable String getVideoId() {
71 public VideoStatus getVideoStatus() {
76 public Optional<EventSubType> getSubTypeDescription() {
77 // Blend extra information provided by this kind of event in subcategories...
78 if (type == EventType.PERSON) {
79 subType = isArrival ? EventSubType.PERSON_ARRIVAL.subType : EventSubType.PERSON_SEEN.subType;
80 } else if (type == EventType.PERSON_HOME) {
81 subType = EventSubType.PERSON_ARRIVAL.subType;
82 } else if (type == EventType.PERSON_AWAY) {
83 subType = EventSubType.PERSON_DEPARTURE.subType;
84 } else if (type == EventType.HUMAN) {
85 subType = EventSubType.MOVEMENT_HUMAN.subType;
86 } else if (type == EventType.ANIMAL) {
87 subType = EventSubType.MOVEMENT_ANIMAL.subType;
89 if (category == EventCategory.ANIMAL) {
90 subType = EventSubType.MOVEMENT_ANIMAL.subType;
91 } else if (category == EventCategory.HUMAN) {
92 subType = EventSubType.MOVEMENT_HUMAN.subType;
93 } else if (category == EventCategory.VEHICLE) {
94 subType = EventSubType.MOVEMENT_VEHICLE.subType;
97 // ... and let ancestor do his work
98 return super.getSubTypeDescription();
102 public @Nullable String getSnapshotUrl() {
103 return internalGetUrl(snapshot);
106 public @Nullable String getVignetteUrl() {
107 return internalGetUrl(vignette);
110 public List<HomeEvent> getSubEvents() {
111 return subevents.stream().peek(subevent -> subevent.setCameraId(getCameraId())).toList();
114 private @Nullable String internalGetUrl(@Nullable Snapshot image) {
115 return image == null ? null : image.url();