예제 #1
0
        public async Task <double> GetElevation(double lat, double lng)
        {
            IElevationService service = GetService();

            if (service is RandomElevationService)
            {
                // Don't hit the database for random elevation service.
                return(await service.GetElevation(lat, lng).ConfigureAwait(false));
            }

            ElevationLocation elevationLocation = await ElevationLocation.FindOrUpdateInDatabase(_context, lat, lng, service).ConfigureAwait(false);

            if (elevationLocation == null)
            {
                Logger.Write(
                    $"{service.GetServiceId()} response not reliable and will be blacklisted for one hour.",
                    LogLevel.Warning
                    );
                BlacklistStrategy(service.GetType());

                Logger.Write(
                    $"Falling back to next elevation strategy: {GetService().GetServiceId()}.",
                    LogLevel.Warning
                    );

                // After blacklisting, retry.
                return(await service.GetElevation(lat, lng).ConfigureAwait(false));
            }

            return(BaseElevationService.GetRandomElevation(elevationLocation.Altitude));
        }
예제 #2
0
        public double GetElevation(double lat, double lng)
        {
            IElevationService service   = GetService();
            double            elevation = service.GetElevation(lat, lng);

            if (elevation == 0 || elevation < -100)
            {
                // Error getting elevation so just return 0.
                Logger.Write(
                    $"{service.GetServiceId()} response not reliable: {elevation.ToString()}, and will be blacklisted for one hour.",
                    LogLevel.Warning
                    );
                BlacklistStrategy(service.GetType());

                Logger.Write(
                    $"Falling back to next elevation strategy: {GetService().GetServiceId()}.",
                    LogLevel.Warning
                    );

                // After blacklisting, retry.
                return(GetElevation(lat, lng));
            }

            return(elevation);
        }
예제 #3
0
        public static async Task <ElevationLocation> FindOrUpdateInDatabase(double latitude, double longitude, IElevationService service)
        {
            using (await DB_LOCK.LockAsync().ConfigureAwait(false))
            {
                if (!Directory.Exists(CACHE_DIR))
                {
                    Directory.CreateDirectory(CACHE_DIR);
                }

                LiteEngine.Upgrade(DB_NAME, null, true);
                using (var db = new LiteDatabase(DB_NAME))
                {
                    db.GetCollection <ElevationLocation>("locations").EnsureIndex(s => s.Id);

                    var locationsCollection = db.GetCollection <ElevationLocation>("locations");
                    var id = GetIdTokenFromLatLng(latitude, longitude);
                    var elevationLocation = locationsCollection.FindOne(x => x.Id == id);

                    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);
                            }

                            elevationLocation = new ElevationLocation(latitude, longitude)
                            {
                                Altitude = altitude
                            };
                            locationsCollection.Insert(elevationLocation);

                            return(elevationLocation);
                        }
                        catch (Exception)
                        {
                            // Just ignore exception and retry after delay
                            await Task.Delay(i * 100).ConfigureAwait(false);
                        }
                    }

                    return(null);
                }
            }
        }
예제 #4
0
        public static async Task <double> getElevation(IElevationService elevationService, double lat, double lon)
        {
            if (elevationService != null)
            {
                return(await elevationService.GetElevation(lat, lon).ConfigureAwait(false));
            }

            Random random  = new Random();
            double maximum = 11.0f;
            double minimum = 8.6f;
            double return1 = random.NextDouble() * (maximum - minimum) + minimum;

            return(return1);
        }
예제 #5
0
        public static double getElevation(IElevationService elevationService, double lat, double lon)
        {
            if (elevationService != null)
            {
                return(elevationService.GetElevation(lat, lon));
            }

            Random random  = new Random();
            double maximum = 11.0f;
            double minimum = 8.6f;
            double return1 = random.NextDouble() * (maximum - minimum) + minimum;

            return(return1);
        }
예제 #6
0
        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);
        }