]> git.basschouten.com Git - openhab-addons.git/blob
cac51625d7321d3f7b71e7613b83d95e2867f40d
[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     }
42
43     private ZonedDateTime time = ZonedDateTime.now();
44     private @Nullable String personId;
45     private EventCategory category = EventCategory.UNKNOWN;
46     private @Nullable Snapshot snapshot;
47     private @Nullable Snapshot vignette;
48     private @Nullable String videoId;
49     private VideoStatus videoStatus = VideoStatus.UNKNOWN;
50     private boolean isArrival;
51     private List<HomeEvent> subevents = List.of();
52
53     @Override
54     public ZonedDateTime getTime() {
55         return time;
56     }
57
58     @Override
59     public @Nullable String getPersonId() {
60         return personId;
61     }
62
63     public @Nullable String getVideoId() {
64         return videoId;
65     }
66
67     public VideoStatus getVideoStatus() {
68         return videoStatus;
69     }
70
71     @Override
72     public Optional<EventSubType> getSubTypeDescription() {
73         // Blend extra information provided by this kind of event in subcategories...
74         if (type == EventType.PERSON) {
75             subType = isArrival ? EventSubType.PERSON_ARRIVAL.subType : EventSubType.PERSON_SEEN.subType;
76         } else if (type == EventType.PERSON_HOME) {
77             subType = EventSubType.PERSON_ARRIVAL.subType;
78         } else if (type == EventType.PERSON_AWAY) {
79             subType = EventSubType.PERSON_DEPARTURE.subType;
80         } else if (type == EventType.HUMAN) {
81             subType = EventSubType.MOVEMENT_HUMAN.subType;
82         } else if (type == EventType.ANIMAL) {
83             subType = EventSubType.MOVEMENT_ANIMAL.subType;
84         } else {
85             if (category == EventCategory.ANIMAL) {
86                 subType = EventSubType.MOVEMENT_ANIMAL.subType;
87             } else if (category == EventCategory.HUMAN) {
88                 subType = EventSubType.MOVEMENT_HUMAN.subType;
89             } else if (category == EventCategory.VEHICLE) {
90                 subType = EventSubType.MOVEMENT_VEHICLE.subType;
91             }
92         }
93         // ... and let ancestor do his work
94         return super.getSubTypeDescription();
95     }
96
97     @Override
98     public @Nullable String getSnapshotUrl() {
99         return internalGetUrl(snapshot);
100     }
101
102     public @Nullable String getVignetteUrl() {
103         return internalGetUrl(vignette);
104     }
105
106     public List<HomeEvent> getSubEvents() {
107         return subevents.stream().peek(subevent -> subevent.setCameraId(getCameraId())).toList();
108     }
109
110     private @Nullable String internalGetUrl(@Nullable Snapshot image) {
111         return image == null ? null : image.url();
112     }
113 }