]> git.basschouten.com Git - openhab-addons.git/blob
5cac555aca5a539b7a181a272ac2a59e7a7bf82b
[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.data.NetatmoConstants.SirenStatus;
29 import org.openhab.binding.netatmo.internal.api.dto.Home;
30 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
31 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent.NAEventsDataResponse;
32 import org.openhab.binding.netatmo.internal.api.dto.Ping;
33 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Base class for all Security related endpoints
39  *
40  * @author GaĆ«l L'hopital - Initial contribution
41  */
42 @NonNullByDefault
43 public class SecurityApi extends RestManager {
44     private final Logger logger = LoggerFactory.getLogger(SecurityApi.class);
45
46     public SecurityApi(ApiBridgeHandler apiClient) {
47         super(apiClient, FeatureArea.SECURITY);
48     }
49
50     /**
51      * Dissociates a webhook from a user.
52      *
53      * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
54      */
55     public void dropWebhook() throws NetatmoException {
56         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_DROP_WEBHOOK);
57         post(uriBuilder, ApiResponse.Ok.class, null);
58     }
59
60     /**
61      * Links a callback url to a user.
62      *
63      * @param uri Your webhook callback url (required)
64      * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
65      */
66     public boolean addwebhook(URI uri) throws NetatmoException {
67         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_ADD_WEBHOOK, PARAM_URL, uri.toString());
68         post(uriBuilder, ApiResponse.Ok.class, null);
69         return true;
70     }
71
72     private List<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoException {
73         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, params);
74         BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody();
75         if (body != null) {
76             Home home = body.getElement();
77             if (home != null) {
78                 return home.getEvents();
79             }
80         }
81         throw new NetatmoException("home should not be null");
82     }
83
84     public List<HomeEvent> getHomeEvents(String homeId, @Nullable ZonedDateTime freshestEventTime)
85             throws NetatmoException {
86         List<HomeEvent> events = getEvents(PARAM_HOME_ID, homeId);
87
88         // we have to rewind to the latest event just after oldestKnown
89         HomeEvent oldestRetrieved = events.get(events.size() - 1);
90         while (freshestEventTime != null && oldestRetrieved.getTime().isAfter(freshestEventTime)) {
91             events.addAll(getEvents(PARAM_HOME_ID, homeId, PARAM_EVENT_ID, oldestRetrieved.getId()));
92             oldestRetrieved = events.get(events.size() - 1);
93         }
94
95         // Remove unneeded events being before oldestKnown
96         return events.stream().filter(event -> freshestEventTime == null || 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 changeSirenStatus(String homeId, String moduleId, SirenStatus status) throws NetatmoException {
131         UriBuilder uriBuilder = getApiUriBuilder(PATH_STATE);
132         String payload = PAYLOAD_SIREN_PRESENCE.formatted(homeId, moduleId, status.name().toLowerCase());
133         post(uriBuilder, ApiResponse.Ok.class, payload);
134     }
135
136     public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException {
137         UriBuilder uriBuilder = getApiUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
138         String payload = String.format(away ? PAYLOAD_PERSON_AWAY : PAYLOAD_PERSON_HOME, homeId, personId);
139         post(uriBuilder, ApiResponse.Ok.class, payload);
140     }
141 }