]> git.basschouten.com Git - openhab-addons.git/blob
336b432b63c7f1a197f30c7f0a9c528aba936c91
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal.api;
14
15 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
16
17 import java.time.ZonedDateTime;
18 import java.util.List;
19
20 import javax.ws.rs.core.UriBuilder;
21
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;
29
30 /**
31  * Base class for all Weather related endpoints
32  *
33  * @author GaĆ«l L'hopital - Initial contribution
34  */
35 @NonNullByDefault
36 public class WeatherApi extends RestManager {
37     private class NAMeasuresResponse extends ApiResponse<List<MeasureBodyElem<Double>>> {
38     }
39
40     private class NADateMeasuresResponse extends ApiResponse<List<MeasureBodyElem<ZonedDateTime>>> {
41     }
42
43     public WeatherApi(ApiBridgeHandler apiClient) {
44         super(apiClient, FeatureArea.WEATHER);
45     }
46
47     /**
48      *
49      * Returns data from a user's Weather Stations (measures and device specific data);
50      *
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
56      */
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);
62         return response;
63     }
64
65     public NAMain getStationData(String deviceId) throws NetatmoException {
66         ListBodyResponse<NAMain> answer = getStationsData(deviceId, true).getBody();
67         if (answer != null) {
68             NAMain station = answer.getElement(deviceId);
69             if (station != null) {
70                 return station;
71             }
72         }
73         throw new NetatmoException("Unexpected answer searching device '%s' : not found.", deviceId);
74     }
75
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();
80     }
81
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;
87         }
88
89         MeasureBodyElem<?> result = getMeasure(deviceId, moduleId, scale, queryLimit.toLowerCase());
90         return result.getSingleValue();
91     }
92
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);
98
99         if (scale != null) {
100             uriBuilder.queryParam("scale", scale.toLowerCase());
101         }
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()) {
106                 return body.get(0);
107             }
108         } else {
109             NAMeasuresResponse response = get(uriBuilder, NAMeasuresResponse.class);
110             List<MeasureBodyElem<Double>> body = response.getBody();
111             if (body != null && !body.isEmpty()) {
112                 return body.get(0);
113             }
114         }
115         throw new NetatmoException("Empty response while getting measurements");
116     }
117 }