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.*;
17 import java.time.ZonedDateTime;
18 import java.util.List;
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.dto.MeasureBodyElem;
26 import org.openhab.binding.netatmo.internal.api.dto.NAMain;
27 import org.openhab.binding.netatmo.internal.api.dto.NAMain.StationDataResponse;
28 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
31 * Base class for all Weather related endpoints
33 * @author Gaƫl L'hopital - Initial contribution
36 public class WeatherApi extends RestManager {
37 private class NAMeasuresResponse extends ApiResponse<List<MeasureBodyElem<Double>>> {
40 private class NADateMeasuresResponse extends ApiResponse<List<MeasureBodyElem<ZonedDateTime>>> {
43 public WeatherApi(ApiBridgeHandler apiClient) {
44 super(apiClient, FeatureArea.WEATHER);
49 * Returns data from a user's Weather Stations (measures and device specific data);
51 * @param deviceId Id of the device you want to retrieve information of (optional)
52 * @param getFavorites Whether to include the user's favorite Weather Stations in addition to the user's
53 * own Weather Stations (optional, default to false)
54 * @return StationDataResponse
55 * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
57 public StationDataResponse getStationsData(@Nullable String deviceId, boolean getFavorites)
58 throws NetatmoException {
59 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GETSTATION, PARAM_DEVICEID, deviceId, //
60 PARAM_FAVORITES, getFavorites);
61 StationDataResponse response = get(uriBuilder, StationDataResponse.class);
65 public NAMain getStationData(String deviceId) throws NetatmoException {
66 ListBodyResponse<NAMain> answer = getStationsData(deviceId, true).getBody();
68 NAMain station = answer.getElement(deviceId);
69 if (station != null) {
73 throw new NetatmoException("Unexpected answer searching device '%s' : not found.", deviceId);
76 public @Nullable Object getMeasures(String deviceId, @Nullable String moduleId, @Nullable String scale,
77 String apiDescriptor) throws NetatmoException {
78 MeasureBodyElem<?> result = getMeasure(deviceId, moduleId, scale, apiDescriptor);
79 return result.getSingleValue();
82 public @Nullable Object getMeasures(String deviceId, @Nullable String moduleId, @Nullable String scale,
83 String apiDescriptor, String limit) throws NetatmoException {
84 String queryLimit = limit;
85 if (!apiDescriptor.contains("_")) {
86 queryLimit += "_" + apiDescriptor;
89 MeasureBodyElem<?> result = getMeasure(deviceId, moduleId, scale, queryLimit.toLowerCase());
90 return result.getSingleValue();
93 private MeasureBodyElem<?> getMeasure(String deviceId, @Nullable String moduleId, @Nullable String scale,
94 String measureType) throws NetatmoException {
95 // NAMeasuresResponse is not designed for optimize=false
96 UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GETMEASURE, PARAM_DEVICEID, deviceId, "real_time", true,
97 "date_end", "last", "optimize", true, "type", measureType.toLowerCase(), PARAM_MODULEID, moduleId);
100 uriBuilder.queryParam("scale", scale.toLowerCase());
102 if (measureType.startsWith("date")) {
103 NADateMeasuresResponse response = get(uriBuilder, NADateMeasuresResponse.class);
104 List<MeasureBodyElem<ZonedDateTime>> body = response.getBody();
105 if (body != null && !body.isEmpty()) {
109 NAMeasuresResponse response = get(uriBuilder, NAMeasuresResponse.class);
110 List<MeasureBodyElem<Double>> body = response.getBody();
111 if (body != null && !body.isEmpty()) {
115 throw new NetatmoException("Empty response while getting measurements");