Skip to content

Commit

Permalink
Avoids NPE if no station provided by API (openhab#15832)
Browse files Browse the repository at this point in the history
Signed-off-by: clinique <gael@lhopital.org>
Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
  • Loading branch information
clinique authored and austvik committed Mar 27, 2024
1 parent b93fc1d commit 73e4480
Showing 1 changed file with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openhab.binding.vigicrues.internal.api.ApiHandler;
import org.openhab.binding.vigicrues.internal.api.VigiCruesException;
import org.openhab.binding.vigicrues.internal.dto.hubeau.HubEauResponse;
import org.openhab.binding.vigicrues.internal.dto.hubeau.HubEauResponse.StationData;
import org.openhab.binding.vigicrues.internal.dto.opendatasoft.OpenDatasoftResponse;
import org.openhab.binding.vigicrues.internal.dto.vigicrues.CdStationHydro;
import org.openhab.binding.vigicrues.internal.dto.vigicrues.InfoVigiCru;
Expand Down Expand Up @@ -129,17 +130,22 @@ private Map<String, String> discoverAttributes(StationConfiguration config) {

try {
HubEauResponse stationDetails = apiHandler.discoverStations(config.id);
stationDetails.stations.stream().findFirst().ifPresent(station -> {
PointType stationLocation = new PointType(
String.format(Locale.US, "%f,%f", station.latitudeStation, station.longitudeStation));
properties.put(LOCATION, stationLocation.toString());
PointType serverLocation = locationProvider.getLocation();
if (serverLocation != null) {
DecimalType distance = serverLocation.distanceFrom(stationLocation);
properties.put(DISTANCE, new QuantityType<>(distance, SIUnits.METRE).toString());
}
properties.put(RIVER, station.libelleCoursEau);
});
List<StationData> stations = stationDetails.stations;
if (stations != null && stations.size() > 0) {
stationDetails.stations.stream().findFirst().ifPresent(station -> {
PointType stationLocation = new PointType(
String.format(Locale.US, "%f,%f", station.latitudeStation, station.longitudeStation));
properties.put(LOCATION, stationLocation.toString());
PointType serverLocation = locationProvider.getLocation();
if (serverLocation != null) {
DecimalType distance = serverLocation.distanceFrom(stationLocation);
properties.put(DISTANCE, new QuantityType<>(distance, SIUnits.METRE).toString());
}
properties.put(RIVER, station.libelleCoursEau);
});
} else {
throw new VigiCruesException("No stations provided");
}
} catch (VigiCruesException e) {
logger.info("Unable to retrieve station location details {} : {}", config.id, e.getMessage());
}
Expand Down

0 comments on commit 73e4480

Please sign in to comment.