2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.netatmo.internal.api;
15 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
18 import java.util.Collection;
19 import java.util.stream.Collectors;
21 import javax.ws.rs.core.UriBuilder;
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;
33 * Base class for all Security related endpoints
35 * @author Gaƫl L'hopital - Initial contribution
38 public class SecurityApi extends RestManager {
39 public SecurityApi(ApiBridgeHandler apiClient) {
40 super(apiClient, FeatureArea.SECURITY);
44 * Dissociates a webhook from a user.
46 * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
48 public void dropWebhook() throws NetatmoException {
49 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_DROPWEBHOOK);
50 post(uriBuilder, ApiResponse.Ok.class, null, null);
54 * Links a callback url to a user.
56 * @param uri Your webhook callback url (required)
57 * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
59 public void addwebhook(URI uri) throws NetatmoException {
60 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_ADDWEBHOOK, PARAM_URL, uri.toString());
61 post(uriBuilder, ApiResponse.Ok.class, null, null);
64 public Collection<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
65 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GETEVENTS, PARAM_HOMEID, homeId, PARAM_PERSONID, personId,
67 NAEventsDataResponse response = get(uriBuilder, NAEventsDataResponse.class);
68 BodyResponse<Home> body = response.getBody();
70 Home home = body.getElement();
72 return home.getEvents().stream().filter(event -> personId.equals(event.getPersonId()))
73 .collect(Collectors.toList());
76 throw new NetatmoException("home should not be null");
79 public Collection<HomeEvent> getCameraEvents(String homeId, String cameraId) throws NetatmoException {
80 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GETEVENTS, PARAM_HOMEID, homeId, PARAM_DEVICEID, cameraId);
81 NAEventsDataResponse response = get(uriBuilder, NAEventsDataResponse.class);
82 BodyResponse<Home> body = response.getBody();
84 Home home = body.getElement();
86 return home.getEvents();
89 throw new NetatmoException("home should not be null");
92 public String ping(String vpnUrl) throws NetatmoException {
93 UriBuilder uriBuilder = UriBuilder.fromUri(vpnUrl).path(PATH_COMMAND).path(SUB_PATH_PING);
94 Ping response = get(uriBuilder, Ping.class);
95 return response.getStatus();
98 public void changeStatus(String localCameraURL, boolean setOn) throws NetatmoException {
99 UriBuilder uriBuilder = UriBuilder.fromUri(localCameraURL).path(PATH_COMMAND).path(SUB_PATH_CHANGESTATUS);
100 uriBuilder.queryParam(PARAM_STATUS, setOn ? "on" : "off");
101 post(uriBuilder, ApiResponse.Ok.class, null, null);
104 public void changeFloodLightMode(String localCameraURL, FloodLightMode mode) throws NetatmoException {
105 UriBuilder uriBuilder = UriBuilder.fromUri(localCameraURL).path(PATH_COMMAND).path(SUB_PATH_FLOODLIGHTSET);
106 uriBuilder.queryParam("config", "%7B%22mode%22:%22" + mode.toString() + "%22%7D");
107 get(uriBuilder, ApiResponse.Ok.class);
110 public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException {
111 UriBuilder uriBuilder = getAppUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
112 String payload = String.format(
113 away ? "{\"home_id\":\"%s\",\"person_id\":\"%s\"}" : "{\"home_id\":\"%s\",\"person_ids\":[\"%s\"]}",
115 post(uriBuilder, ApiResponse.Ok.class, payload, "application/json;charset=utf-8");