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.data.NetatmoConstants.SirenStatus;
29 import org.openhab.binding.netatmo.internal.api.dto.Home;
30 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
31 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent.NAEventsDataResponse;
32 import org.openhab.binding.netatmo.internal.api.dto.Ping;
33 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Base class for all Security related endpoints
40 * @author Gaƫl L'hopital - Initial contribution
43 public class SecurityApi extends RestManager {
44 private final Logger logger = LoggerFactory.getLogger(SecurityApi.class);
46 public SecurityApi(ApiBridgeHandler apiClient) {
47 super(apiClient, FeatureArea.SECURITY);
51 * Dissociates a webhook from a user.
53 * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
55 public void dropWebhook() throws NetatmoException {
56 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_DROP_WEBHOOK);
57 post(uriBuilder, ApiResponse.Ok.class, null);
61 * Links a callback url to a user.
63 * @param uri Your webhook callback url (required)
64 * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
66 public boolean addwebhook(URI uri) throws NetatmoException {
67 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_ADD_WEBHOOK, PARAM_URL, uri.toString());
68 post(uriBuilder, ApiResponse.Ok.class, null);
72 private List<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoException {
73 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, params);
74 BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody();
76 Home home = body.getElement();
78 return home.getEvents();
81 throw new NetatmoException("home should not be null");
84 public List<HomeEvent> getHomeEvents(String homeId, @Nullable ZonedDateTime freshestEventTime)
85 throws NetatmoException {
86 List<HomeEvent> events = getEvents(PARAM_HOME_ID, homeId);
88 // we have to rewind to the latest event just after oldestKnown
89 HomeEvent oldestRetrieved = events.get(events.size() - 1);
90 while (freshestEventTime != null && oldestRetrieved.getTime().isAfter(freshestEventTime)) {
91 events.addAll(getEvents(PARAM_HOME_ID, homeId, PARAM_EVENT_ID, oldestRetrieved.getId()));
92 oldestRetrieved = events.get(events.size() - 1);
95 // Remove unneeded events being before oldestKnown
96 return events.stream().filter(event -> freshestEventTime == null || 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 changeSirenStatus(String homeId, String moduleId, SirenStatus status) throws NetatmoException {
131 UriBuilder uriBuilder = getApiUriBuilder(PATH_STATE);
132 String payload = PAYLOAD_SIREN_PRESENCE.formatted(homeId, moduleId, status.name().toLowerCase());
133 post(uriBuilder, ApiResponse.Ok.class, payload);
136 public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException {
137 UriBuilder uriBuilder = getApiUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
138 String payload = String.format(away ? PAYLOAD_PERSON_AWAY : PAYLOAD_PERSON_HOME, homeId, personId);
139 post(uriBuilder, ApiResponse.Ok.class, payload);