]> git.basschouten.com Git - openhab-addons.git/blob
31ad916b1a2993df2f38a8ef0f1aa02dfaba2a17
[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 import java.util.stream.Collectors;
20
21 import javax.ws.rs.core.UriBuilder;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
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
32 /**
33  * Base class for all Security related endpoints
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  */
37 @NonNullByDefault
38 public class SecurityApi extends RestManager {
39     public SecurityApi(ApiBridgeHandler apiClient) {
40         super(apiClient, FeatureArea.SECURITY);
41     }
42
43     /**
44      * Dissociates a webhook from a user.
45      *
46      * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
47      */
48     public void dropWebhook() throws NetatmoException {
49         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_DROP_WEBHOOK);
50         post(uriBuilder, ApiResponse.Ok.class, null, null);
51     }
52
53     /**
54      * Links a callback url to a user.
55      *
56      * @param uri Your webhook callback url (required)
57      * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
58      */
59     public boolean addwebhook(URI uri) throws NetatmoException {
60         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_ADD_WEBHOOK, PARAM_URL, uri.toString());
61         post(uriBuilder, ApiResponse.Ok.class, null, null);
62         return true;
63     }
64
65     public Collection<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
66         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, PARAM_HOME_ID, homeId, PARAM_PERSON_ID, personId,
67                 PARAM_OFFSET, 1);
68         NAEventsDataResponse response = get(uriBuilder, NAEventsDataResponse.class);
69         BodyResponse<Home> body = response.getBody();
70         if (body != null) {
71             Home home = body.getElement();
72             if (home != null) {
73                 return home.getEvents().stream().filter(event -> personId.equals(event.getPersonId()))
74                         .collect(Collectors.toList());
75             }
76         }
77         throw new NetatmoException("home should not be null");
78     }
79
80     public Collection<HomeEvent> getCameraEvents(String homeId, String deviceId, String deviceType)
81             throws NetatmoException {
82         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, PARAM_HOME_ID, homeId, PARAM_DEVICE_ID, deviceId,
83                 PARAM_DEVICES_TYPE, deviceType);
84         BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody();
85         if (body != null) {
86             Home home = body.getElement();
87             if (home != null) {
88                 return home.getEvents();
89             }
90         }
91         throw new NetatmoException("home should not be null");
92     }
93
94     public String ping(String vpnUrl) throws NetatmoException {
95         UriBuilder uriBuilder = UriBuilder.fromUri(vpnUrl).path(PATH_COMMAND).path(SUB_PATH_PING);
96         Ping response = get(uriBuilder, Ping.class);
97         return response.getStatus();
98     }
99
100     public void changeStatus(String localCameraURL, boolean setOn) throws NetatmoException {
101         UriBuilder uriBuilder = UriBuilder.fromUri(localCameraURL).path(PATH_COMMAND).path(SUB_PATH_CHANGESTATUS);
102         uriBuilder.queryParam(PARAM_STATUS, setOn ? "on" : "off");
103         post(uriBuilder, ApiResponse.Ok.class, null, null);
104     }
105
106     public void changeFloodLightMode(String homeId, String cameraId, FloodLightMode mode) throws NetatmoException {
107         UriBuilder uriBuilder = getAppUriBuilder(PATH_STATE);
108         String payload = String.format(
109                 "{\"home\": {\"id\":\"%s\",\"modules\": [ {\"id\":\"%s\",\"floodlight\":\"%s\"} ]}}", homeId, cameraId,
110                 mode.name().toLowerCase());
111         post(uriBuilder, ApiResponse.Ok.class, payload, "application/json;charset=utf-8");
112     }
113
114     public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException {
115         UriBuilder uriBuilder = getAppUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
116         String payload = String.format(
117                 away ? "{\"home_id\":\"%s\",\"person_id\":\"%s\"}" : "{\"home_id\":\"%s\",\"person_ids\":[\"%s\"]}",
118                 homeId, personId);
119         post(uriBuilder, ApiResponse.Ok.class, payload, "application/json;charset=utf-8");
120     }
121 }