import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
import java.time.ZonedDateTime;
+import java.util.Collection;
import java.util.List;
import javax.ws.rs.core.UriBuilder;
}
/**
- *
* Returns data from a user's Weather Stations (measures and device specific data);
*
* @param deviceId Id of the device you want to retrieve information of (optional)
* @param getFavorites Whether to include the user's favorite Weather Stations in addition to the user's
- * own Weather Stations (optional, default to false)
+ * own Weather Stations
* @return StationDataResponse
* @throws NetatmoException If fail to call the API, e.g. server error or deserializing
*/
- public StationDataResponse getStationsData(@Nullable String deviceId, boolean getFavorites)
+ private StationDataResponse getStationsData(@Nullable String deviceId, boolean getFavorites)
throws NetatmoException {
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GETSTATION, PARAM_DEVICEID, deviceId, //
PARAM_FAVORITES, getFavorites);
return response;
}
+ /**
+ * Returns data from a user's Weather Station, this stations can be a station owned by the user or a favorite
+ * station or a guest station.
+ *
+ * @param deviceId Id of the device you want to retrieve information
+ * @return NAMain
+ * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
+ */
public NAMain getStationData(String deviceId) throws NetatmoException {
ListBodyResponse<NAMain> answer = getStationsData(deviceId, true).getBody();
if (answer != null) {
throw new NetatmoException("Unexpected answer searching device '%s' : not found.", deviceId);
}
+ /**
+ * Returns data from a Weather Station owned by the user
+ *
+ * This method must be preferred to getStationData when you know that the device is a station owned by the user
+ * (because it avoids requesting additional data for favorite/guest stations).
+ *
+ * @param deviceId Id of the device you want to retrieve information
+ * @return NAMain
+ * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
+ */
+ public NAMain getOwnedStationData(String deviceId) throws NetatmoException {
+ ListBodyResponse<NAMain> answer = getStationsData(deviceId, false).getBody();
+ if (answer != null) {
+ NAMain station = answer.getElement(deviceId);
+ if (station != null) {
+ return station;
+ }
+ }
+ throw new NetatmoException("Unexpected answer searching device '%s' : not found.", deviceId);
+ }
+
+ public Collection<NAMain> getFavoriteAndGuestStationsData() throws NetatmoException {
+ ListBodyResponse<NAMain> answer = getStationsData(null, true).getBody();
+ return answer != null ? answer.getElements() : List.of();
+ }
+
public @Nullable Object getMeasures(String deviceId, @Nullable String moduleId, @Nullable String scale,
String apiDescriptor) throws NetatmoException {
MeasureBodyElem<?> result = getMeasure(deviceId, moduleId, scale, apiDescriptor);
if (readFriends) {
WeatherApi weatherApi = localHandler.getRestManager(WeatherApi.class);
if (weatherApi != null) { // Search favorite stations
- ListBodyResponse<NAMain> body = weatherApi.getStationsData(null, true).getBody();
- if (body != null) {
- body.getElements().stream().filter(NAMain::isReadOnly).forEach(station -> {
- ThingUID bridgeUID = createThing(station, apiBridgeUID);
- station.getModules().values().stream()
- .forEach(module -> createThing(module, bridgeUID));
- });
- }
+ weatherApi.getFavoriteAndGuestStationsData().stream().filter(NAMain::isReadOnly)
+ .forEach(station -> {
+ ThingUID bridgeUID = createThing(station, apiBridgeUID);
+ station.getModules().values().stream()
+ .forEach(module -> createThing(module, bridgeUID));
+ });
}
}
HomeApi homeApi = localHandler.getRestManager(HomeApi.class);
public class DeviceCapability extends Capability {
private static final int DATA_AGE_LIMIT_S = 3600;
+ /**
+ * Whether the device is owned or not by the user (a favorite station or a guest station is not owned by the user).
+ * It will be updated when handling the result of the getstationsdata API.
+ * It must be initialized to false to be sure that the first call to the API will not fail for a favorite/guest
+ * weather stations.
+ */
+ protected boolean owned;
+
public DeviceCapability(CommonInterface handler) {
super(handler);
}
@Override
protected void updateNAMain(NAMain newData) {
+ owned = !newData.isReadOnly();
if (firstLaunch) {
newData.getPlace().ifPresent(place -> {
place.getCity().map(city -> properties.put(PROPERTY_CITY, city));