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