]> git.basschouten.com Git - openhab-addons.git/blob
e7c08e8157affd2237798aac414f76c9ce7289f5
[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;
14
15 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
16
17 import java.net.URI;
18 import java.time.ZonedDateTime;
19 import java.util.Comparator;
20 import java.util.List;
21
22 import javax.ws.rs.core.UriBuilder;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
27 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FloodLightMode;
28 import org.openhab.binding.netatmo.internal.api.dto.Home;
29 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
30 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent.NAEventsDataResponse;
31 import org.openhab.binding.netatmo.internal.api.dto.Ping;
32 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Base class for all Security related endpoints
38  *
39  * @author GaĆ«l L'hopital - Initial contribution
40  */
41 @NonNullByDefault
42 public class SecurityApi extends RestManager {
43     private final Logger logger = LoggerFactory.getLogger(SecurityApi.class);
44
45     public SecurityApi(ApiBridgeHandler apiClient) {
46         super(apiClient, FeatureArea.SECURITY);
47     }
48
49     /**
50      * Dissociates a webhook from a user.
51      *
52      * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
53      */
54     public void dropWebhook() throws NetatmoException {
55         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_DROP_WEBHOOK);
56         post(uriBuilder, ApiResponse.Ok.class, null);
57     }
58
59     /**
60      * Links a callback url to a user.
61      *
62      * @param uri Your webhook callback url (required)
63      * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
64      */
65     public boolean addwebhook(URI uri) throws NetatmoException {
66         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_ADD_WEBHOOK, PARAM_URL, uri.toString());
67         post(uriBuilder, ApiResponse.Ok.class, null);
68         return true;
69     }
70
71     private List<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoException {
72         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, params);
73         BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody();
74         if (body != null) {
75             Home home = body.getElement();
76             if (home != null) {
77                 return home.getEvents();
78             }
79         }
80         throw new NetatmoException("home should not be null");
81     }
82
83     public List<HomeEvent> getHomeEvents(String homeId, @Nullable ZonedDateTime freshestEventTime)
84             throws NetatmoException {
85         List<HomeEvent> events = getEvents(PARAM_HOME_ID, homeId);
86
87         // we have to rewind to the latest event just after oldestKnown
88         HomeEvent oldestRetrieved = events.get(events.size() - 1);
89         while (freshestEventTime != null && oldestRetrieved.getTime().isAfter(freshestEventTime)) {
90             events.addAll(getEvents(PARAM_HOME_ID, homeId, PARAM_EVENT_ID, oldestRetrieved.getId()));
91             oldestRetrieved = events.get(events.size() - 1);
92         }
93
94         // Remove unneeded events being before oldestKnown
95         return events.stream().filter(event -> freshestEventTime == null || event.getTime().isAfter(freshestEventTime))
96                 .sorted(Comparator.comparing(HomeEvent::getTime).reversed()).toList();
97     }
98
99     public List<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
100         return getEvents(PARAM_HOME_ID, homeId, PARAM_PERSON_ID, personId, PARAM_OFFSET, 1);
101     }
102
103     public List<HomeEvent> getDeviceEvents(String homeId, String deviceId, String deviceType) throws NetatmoException {
104         return getEvents(PARAM_HOME_ID, homeId, PARAM_DEVICE_ID, deviceId, PARAM_DEVICES_TYPE, deviceType);
105     }
106
107     public @Nullable String ping(String vpnUrl) {
108         UriBuilder uriBuilder = UriBuilder.fromUri(vpnUrl).path(PATH_COMMAND).path(SUB_PATH_PING);
109         try {
110             return get(uriBuilder, Ping.class).getStatus();
111         } catch (NetatmoException e) {
112             logger.debug("Pinging {} failed : {}", vpnUrl, e.getMessage());
113             return null;
114         }
115     }
116
117     public void changeStatus(String localCameraURL, boolean setOn) throws NetatmoException {
118         UriBuilder uriBuilder = UriBuilder.fromUri(localCameraURL).path(PATH_COMMAND).path(SUB_PATH_CHANGESTATUS);
119         uriBuilder.queryParam(PARAM_STATUS, setOn ? "on" : "off");
120         post(uriBuilder, ApiResponse.Ok.class, null);
121     }
122
123     public void changeFloodLightMode(String homeId, String cameraId, FloodLightMode mode) throws NetatmoException {
124         UriBuilder uriBuilder = getApiUriBuilder(PATH_STATE);
125         String payload = PAYLOAD_FLOODLIGHT.formatted(homeId, cameraId, mode.name().toLowerCase());
126         post(uriBuilder, ApiResponse.Ok.class, payload);
127     }
128
129     public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException {
130         UriBuilder uriBuilder = getApiUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
131         String payload = String.format(away ? PAYLOAD_PERSON_AWAY : PAYLOAD_PERSON_HOME, homeId, personId);
132         post(uriBuilder, ApiResponse.Ok.class, payload);
133     }
134 }