public static async Task <ElevationLocation> FindOrUpdateInDatabase(ElevationConfigContext db, ulong c, IElevationService service) { var cellId = new S2CellId(c); var latlng = cellId.ToLatLng(); return(await FindOrUpdateInDatabase(db, latlng.LatDegrees, latlng.LngDegrees, service).ConfigureAwait(false)); }
public static async Task <ElevationLocation> FindOrUpdateInDatabase(ElevationConfigContext db, double latitude, double longitude, IElevationService service) { latitude = Math.Round(latitude, GEOLOCATION_PRECISION); longitude = Math.Round(longitude, GEOLOCATION_PRECISION); var elevationLocation = db.ElevationLocation.FirstOrDefault(x => x.Latitude == latitude && x.Longitude == longitude); if (elevationLocation != null) { return(elevationLocation); } for (var i = 0; i < MAX_RETRIES; i++) { try { var altitude = await service.GetElevation(latitude, longitude).ConfigureAwait(false); if (altitude == 0 || altitude < -100) { // Invalid altitude return(null); } db.ElevationLocation.Add(new ElevationLocation(latitude, longitude) { Altitude = altitude }); await db.SaveChangesAsync().ConfigureAwait(false); return(elevationLocation); } catch (Exception) { // Just ignore exception and retry after delay await Task.Delay(i * 100).ConfigureAwait(false); } } return(null); }