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;
20 import javax.ws.rs.core.UriBuilder;
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;
35 * Base class for all Security related endpoints
37 * @author Gaƫl L'hopital - Initial contribution
40 public class SecurityApi extends RestManager {
41 private final Logger logger = LoggerFactory.getLogger(SecurityApi.class);
43 public SecurityApi(ApiBridgeHandler apiClient) {
44 super(apiClient, FeatureArea.SECURITY);
48 * Dissociates a webhook from a user.
50 * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
52 public void dropWebhook() throws NetatmoException {
53 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_DROP_WEBHOOK);
54 post(uriBuilder, ApiResponse.Ok.class, null);
58 * Links a callback url to a user.
60 * @param uri Your webhook callback url (required)
61 * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
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);
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();
73 Home home = body.getElement();
75 return home.getEvents();
78 throw new NetatmoException("home should not be null");
81 public Collection<HomeEvent> getHomeEvents(String homeId) throws NetatmoException {
82 return getEvents(PARAM_HOME_ID, homeId);
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);
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);
94 public @Nullable String ping(String vpnUrl) {
95 UriBuilder uriBuilder = UriBuilder.fromUri(vpnUrl).path(PATH_COMMAND).path(SUB_PATH_PING);
97 return get(uriBuilder, Ping.class).getStatus();
98 } catch (NetatmoException e) {
99 logger.debug("Pinging {} failed : {}", vpnUrl, e.getMessage());
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);
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);
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);