]> git.basschouten.com Git - openhab-addons.git/blob
c8e84368112f6b6e9505b6b864514729aeda2bff
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.LinkedHashSet;
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.netatmo.internal.api.data.EventType;
22 import org.openhab.binding.netatmo.internal.deserialization.NAObjectMap;
23 import org.openhab.binding.netatmo.internal.deserialization.NAPushType;
24
25 /**
26  * The {@link WebhookEvent} is responsible to hold
27  * data given back by the Netatmo API when calling the webhook
28  *
29  * @author GaĆ«l L'hopital - Initial contribution
30  *
31  */
32 @NonNullByDefault
33 public class WebhookEvent extends Event {
34     private NAPushType pushType = NAPushType.UNKNOWN;
35     private String homeId = "";
36     private String roomId = "";
37     private String deviceId = "";
38     private @Nullable String snapshotUrl;
39     private @Nullable String vignetteUrl;
40     private NAObjectMap<Person> persons = new NAObjectMap<>();
41     // Webhook does not provide the event generation time, so we'll use the event reception time
42     private ZonedDateTime time = ZonedDateTime.now();
43
44     public String getHomeId() {
45         return homeId;
46     }
47
48     public NAObjectMap<Person> getPersons() {
49         return persons;
50     }
51
52     @Override
53     public EventType getEventType() {
54         return pushType.event();
55     }
56
57     @Override
58     public ZonedDateTime getTime() {
59         return time;
60     }
61
62     @Override
63     public @Nullable String getPersonId() {
64         return persons.size() > 0 ? persons.keySet().iterator().next() : null;
65     }
66
67     @Override
68     public @Nullable String getSnapshotUrl() {
69         return snapshotUrl;
70     }
71
72     public @Nullable String getVignetteUrl() {
73         return vignetteUrl;
74     }
75
76     public Set<String> getNAObjectList() {
77         Set<String> result = new LinkedHashSet<>();
78         result.add(getCameraId());
79         addNotBlank(result, homeId);
80         addNotBlank(result, deviceId);
81         addNotBlank(result, roomId);
82         addNotBlank(result, getCameraId());
83         result.addAll(getPersons().keySet());
84         return result;
85     }
86
87     private void addNotBlank(Set<String> collection, String value) {
88         if (!value.isBlank()) {
89             collection.add(value);
90         }
91     }
92 }