Ejemplo n.º 1
0
        public static async Task <GeoLocation> FindOrUpdateInDatabase(double latitude, double longitude)
        {
            if (BlacklistTimestamp > DateTime.UtcNow.ToUnixTime())
            {
                return(null);
            }

            using (var db = new GeoLocationConfigContext())
            {
                latitude  = Math.Round(latitude, GEOLOCATION_PRECISION);
                longitude = Math.Round(longitude, GEOLOCATION_PRECISION);

                var geoLocation = db.GeoLocation.Where(x => x.Latitude == latitude && x.Longitude == longitude).FirstOrDefault();

                if (geoLocation != null)
                {
                    return(geoLocation);
                }

                geoLocation = new GeoLocation(latitude, longitude);

                for (var i = 0; i < GEOCODING_MAX_RETRIES; i++)
                {
                    try
                    {
                        await geoLocation.ReverseGeocode().ConfigureAwait(false);

                        break;
                    }
                    catch (GoogleGeocodingException e)
                    {
                        if (e.Status == GoogleStatus.OverQueryLimit)
                        {
                            BlackList();
                            return(null);
                        }

                        // Just ignore exception and retry after delay
                        await Task.Delay(i * 100).ConfigureAwait(false);
                    }
                    catch (Exception)
                    {
                        if (i == GEOCODING_MAX_RETRIES - 1)
                        {
                            BlackList();
                            return(null);
                        }

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

                // Before we store it to the database, ensure it must at least have country field set.
                if (string.IsNullOrEmpty(geoLocation.Country))
                {
                    return(null);
                }

                db.GeoLocation.Add(geoLocation);
                await db.SaveChangesAsync().ConfigureAwait(false);

                return(geoLocation);
            }
        }