2 * Copyright (c) 2010-2024 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.time.ZonedDateTime;
19 import java.util.Comparator;
20 import java.util.List;
22 import javax.ws.rs.core.UriBuilder;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
27 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FloodLightMode;
28 import org.openhab.binding.netatmo.internal.api.dto.Home;
29 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
30 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent.NAEventsDataResponse;
31 import org.openhab.binding.netatmo.internal.api.dto.Ping;
32 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Base class for all Security related endpoints
39 * @author Gaƫl L'hopital - Initial contribution
42 public class SecurityApi extends RestManager {
43 private final Logger logger = LoggerFactory.getLogger(SecurityApi.class);
45 public SecurityApi(ApiBridgeHandler apiClient) {
46 super(apiClient, FeatureArea.SECURITY);
50 * Dissociates a webhook from a user.
52 * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
54 public void dropWebhook() throws NetatmoException {
55 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_DROP_WEBHOOK);
56 post(uriBuilder, ApiResponse.Ok.class, null);
60 * Links a callback url to a user.
62 * @param uri Your webhook callback url (required)
63 * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
65 public boolean addwebhook(URI uri) throws NetatmoException {
66 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_ADD_WEBHOOK, PARAM_URL, uri.toString());
67 post(uriBuilder, ApiResponse.Ok.class, null);
71 private List<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoException {
72 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, params);
73 BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody();
75 Home home = body.getElement();
77 return home.getEvents();
80 throw new NetatmoException("home should not be null");
83 public List<HomeEvent> getHomeEvents(String homeId, ZonedDateTime freshestEventTime) throws NetatmoException {
84 List<HomeEvent> events = getEvents(PARAM_HOME_ID, homeId);
86 // we have to rewind to the latest event just after freshestEventTime
87 if (events.size() > 0) {
88 HomeEvent oldestRetrieved = events.get(events.size() - 1);
89 while (oldestRetrieved.getTime().isAfter(freshestEventTime)) {
90 events.addAll(getEvents(PARAM_HOME_ID, homeId, PARAM_EVENT_ID, oldestRetrieved.getId()));
91 oldestRetrieved = events.get(events.size() - 1);
95 // Remove unneeded events being before freshestEventTime
96 return events.stream().filter(event -> event.getTime().isAfter(freshestEventTime))
97 .sorted(Comparator.comparing(HomeEvent::getTime).reversed()).toList();
100 public List<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
101 return getEvents(PARAM_HOME_ID, homeId, PARAM_PERSON_ID, personId, PARAM_OFFSET, 1);
104 public List<HomeEvent> getDeviceEvents(String homeId, String deviceId, String deviceType) throws NetatmoException {
105 return getEvents(PARAM_HOME_ID, homeId, PARAM_DEVICE_ID, deviceId, PARAM_DEVICES_TYPE, deviceType);
108 public @Nullable String ping(String vpnUrl) {
109 UriBuilder uriBuilder = UriBuilder.fromUri(vpnUrl).path(PATH_COMMAND).path(SUB_PATH_PING);
111 return get(uriBuilder, Ping.class).getStatus();
112 } catch (NetatmoException e) {
113 logger.debug("Pinging {} failed : {}", vpnUrl, e.getMessage());
118 public void changeStatus(String localCameraURL, boolean setOn) throws NetatmoException {
119 UriBuilder uriBuilder = UriBuilder.fromUri(localCameraURL).path(PATH_COMMAND).path(SUB_PATH_CHANGESTATUS);
120 uriBuilder.queryParam(PARAM_STATUS, setOn ? "on" : "off");
121 post(uriBuilder, ApiResponse.Ok.class, null);
124 public void changeFloodLightMode(String homeId, String cameraId, FloodLightMode mode) throws NetatmoException {
125 UriBuilder uriBuilder = getApiUriBuilder(PATH_STATE);
126 String payload = PAYLOAD_FLOODLIGHT.formatted(homeId, cameraId, mode.name().toLowerCase());
127 post(uriBuilder, ApiResponse.Ok.class, payload);
130 public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException {
131 UriBuilder uriBuilder = getApiUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
132 String payload = String.format(away ? PAYLOAD_PERSON_AWAY : PAYLOAD_PERSON_HOME, homeId, personId);
133 post(uriBuilder, ApiResponse.Ok.class, payload);