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