]> git.basschouten.com Git - openhab-addons.git/blob
58b3607a0c455026beb14c40a7dd4eb895629046
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.api.dto;
14
15 import java.time.ZonedDateTime;
16 import java.util.List;
17 import java.util.Optional;
18
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;
27
28 /**
29  * The {@link HomeEvent} holds information transferred by the webhook about a home event.
30  *
31  * @author GaĆ«l L'hopital - Initial contribution
32  *
33  */
34
35 @NonNullByDefault
36 public class HomeEvent extends Event {
37     public class NAEventsDataResponse extends ApiResponse<BodyResponse<Home>> {
38     }
39
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;
44         }
45     }
46
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();
56
57     @Override
58     public ZonedDateTime getTime() {
59         return time;
60     }
61
62     @Override
63     public @Nullable String getPersonId() {
64         return personId;
65     }
66
67     public @Nullable String getVideoId() {
68         return videoId;
69     }
70
71     public VideoStatus getVideoStatus() {
72         return videoStatus;
73     }
74
75     @Override
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;
88         } else {
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;
95             }
96         }
97         // ... and let ancestor do his work
98         return super.getSubTypeDescription();
99     }
100
101     @Override
102     public @Nullable String getSnapshotUrl() {
103         return internalGetUrl(snapshot);
104     }
105
106     public @Nullable String getVignetteUrl() {
107         return internalGetUrl(vignette);
108     }
109
110     public List<HomeEvent> getSubEvents() {
111         return subevents.stream().peek(subevent -> subevent.setCameraId(getCameraId())).toList();
112     }
113
114     private @Nullable String internalGetUrl(@Nullable Snapshot image) {
115         return image == null ? null : image.url();
116     }
117 }