]> git.basschouten.com Git - openhab-addons.git/blob
28825bf3ef66c4a3fa4b320467cd0103a2e34497
[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, ZonedDateTime freshestEventTime) throws NetatmoException {
84         List<HomeEvent> events = getEvents(PARAM_HOME_ID, homeId);
85
86         // we have to rewind to the latest event just after freshestEventTime
87         if (events.size() > 0) {
88             HomeEvent oldestRetrieved = events.get(events.size() - 1);
89             while (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
95         // Remove unneeded events being before freshestEventTime
96         return events.stream().filter(event -> event.getTime().isAfter(freshestEventTime))
97                 .sorted(Comparator.comparing(HomeEvent::getTime).reversed()).toList();
98     }
99
100     public List<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
101         return getEvents(PARAM_HOME_ID, homeId, PARAM_PERSON_ID, personId, PARAM_OFFSET, 1);
102     }
103
104     public List<HomeEvent> getDeviceEvents(String homeId, String deviceId, String deviceType) throws NetatmoException {
105         return getEvents(PARAM_HOME_ID, homeId, PARAM_DEVICE_ID, deviceId, PARAM_DEVICES_TYPE, deviceType);
106     }
107
108     public @Nullable String ping(String vpnUrl) {
109         UriBuilder uriBuilder = UriBuilder.fromUri(vpnUrl).path(PATH_COMMAND).path(SUB_PATH_PING);
110         try {
111             return get(uriBuilder, Ping.class).getStatus();
112         } catch (NetatmoException e) {
113             logger.debug("Pinging {} failed : {}", vpnUrl, e.getMessage());
114             return null;
115         }
116     }
117
118     public void changeStatus(String localCameraURL, boolean setOn) throws NetatmoException {
119         UriBuilder uriBuilder = UriBuilder.fromUri(localCameraURL).path(PATH_COMMAND).path(SUB_PATH_CHANGESTATUS);
120         uriBuilder.queryParam(PARAM_STATUS, setOn ? "on" : "off");
121         post(uriBuilder, ApiResponse.Ok.class, null);
122     }
123
124     public void changeFloodLightMode(String homeId, String cameraId, FloodLightMode mode) throws NetatmoException {
125         UriBuilder uriBuilder = getApiUriBuilder(PATH_STATE);
126         String payload = PAYLOAD_FLOODLIGHT.formatted(homeId, cameraId, mode.name().toLowerCase());
127         post(uriBuilder, ApiResponse.Ok.class, payload);
128     }
129
130     public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException {
131         UriBuilder uriBuilder = getApiUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
132         String payload = String.format(away ? PAYLOAD_PERSON_AWAY : PAYLOAD_PERSON_HOME, homeId, personId);
133         post(uriBuilder, ApiResponse.Ok.class, payload);
134     }
135 }