Avoids NPE if no station provided by API (#15832)

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2023-11-03 18:17:36 +01:00 committed by GitHub
parent 740f80fff1
commit 15a673a027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 11 deletions

View File

@ -34,6 +34,7 @@ import org.openhab.binding.vigicrues.internal.StationConfiguration;
import org.openhab.binding.vigicrues.internal.api.ApiHandler; import org.openhab.binding.vigicrues.internal.api.ApiHandler;
import org.openhab.binding.vigicrues.internal.api.VigiCruesException; 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;
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.opendatasoft.OpenDatasoftResponse;
import org.openhab.binding.vigicrues.internal.dto.vigicrues.CdStationHydro; import org.openhab.binding.vigicrues.internal.dto.vigicrues.CdStationHydro;
import org.openhab.binding.vigicrues.internal.dto.vigicrues.InfoVigiCru; import org.openhab.binding.vigicrues.internal.dto.vigicrues.InfoVigiCru;
@ -129,17 +130,22 @@ public class VigiCruesHandler extends BaseThingHandler {
try { try {
HubEauResponse stationDetails = apiHandler.discoverStations(config.id); HubEauResponse stationDetails = apiHandler.discoverStations(config.id);
stationDetails.stations.stream().findFirst().ifPresent(station -> { List<StationData> stations = stationDetails.stations;
PointType stationLocation = new PointType( if (stations != null && stations.size() > 0) {
String.format(Locale.US, "%f,%f", station.latitudeStation, station.longitudeStation)); stationDetails.stations.stream().findFirst().ifPresent(station -> {
properties.put(LOCATION, stationLocation.toString()); PointType stationLocation = new PointType(
PointType serverLocation = locationProvider.getLocation(); String.format(Locale.US, "%f,%f", station.latitudeStation, station.longitudeStation));
if (serverLocation != null) { properties.put(LOCATION, stationLocation.toString());
DecimalType distance = serverLocation.distanceFrom(stationLocation); PointType serverLocation = locationProvider.getLocation();
properties.put(DISTANCE, new QuantityType<>(distance, SIUnits.METRE).toString()); if (serverLocation != null) {
} DecimalType distance = serverLocation.distanceFrom(stationLocation);
properties.put(RIVER, station.libelleCoursEau); properties.put(DISTANCE, new QuantityType<>(distance, SIUnits.METRE).toString());
}); }
properties.put(RIVER, station.libelleCoursEau);
});
} else {
throw new VigiCruesException("No stations provided");
}
} catch (VigiCruesException e) { } catch (VigiCruesException e) {
logger.info("Unable to retrieve station location details {} : {}", config.id, e.getMessage()); logger.info("Unable to retrieve station location details {} : {}", config.id, e.getMessage());
} }