internal static List <GeolocationModel> GetUserLocations(int accountId)
        {
            List <GeolocationModel> modelList = new List <GeolocationModel>();

            using (DBConnector connector = new DBConnector("get_UserLocation", CommandType.StoredProcedure))
            {
                connector.Cmd.Parameters.AddWithValue("@AccountID", accountId);
                connector.Execute(DBOperation.GetWhileReader);

                if (connector.Rdr.HasRows)
                {
                    while (connector.Rdr.Read())
                    {
                        GeolocationModel model = new GeolocationModel();

                        model.UserID    = Convert.ToInt32(connector.Rdr["UserID"]);
                        model.Name      = connector.Rdr["Name"].ToString();
                        model.Avatar    = connector.Rdr["Avatar"].ToString();
                        model.MaxDate   = (DateTime)connector.Rdr["MaxDate"];
                        model.Latitude  = (float)connector.Rdr["Latitude"];
                        model.Longitude = (float)connector.Rdr["Longitude"];

                        modelList.Add(model);
                    }
                }
            }
            return(modelList);
        }
Ejemplo n.º 2
0
        public async Task Weather([Leftover] string query = "toronto")
        {
            try
            {
                using IDisposable _ = Context.Channel.EnterTypingState();
                GeolocationResult location = await Service.GetLocationDataAsync(query).ConfigureAwait(false);

                if (location == null)
                {
                    await Context.Channel.SendErrorAsync("Cannot find specified location. Please try again.");

                    return;
                }

                GeolocationModel addr   = location.Results[0];
                string           result = await Service.GetWeatherDataAsync(addr.Geometry.Location.Lat, addr.Geometry.Location.Lng).ConfigureAwait(false);

                TimeZoneResult tz = await Service.GetLocalDateTime(addr.Geometry.Location.Lat, addr.Geometry.Location.Lng).ConfigureAwait(false);

                DateTimeOffset localDt = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.UtcNow, tz.TimeZoneId);
                await Context.Channel.EmbedAsync(new EmbedBuilder().WithDynamicColor(Context)
                                                 .WithAuthor("Weather Report")
                                                 .WithDescription(addr.FormattedAddress + "\n" + Format.Code(result))
                                                 .WithFooter($"{localDt:HH:mm, MMM dd, yyyy}, {tz.TimeZoneName}, UTC{localDt:zz}"))
                .ConfigureAwait(false);
            }
            catch
            {
                await Context.Channel.SendErrorAsync("Failed to get weather.");
            }
        }
        public static async Task <WeatherModel> GetWeatherFromAPI()
        {
            string           ipv4        = await new HttpClient().GetStringAsync("https://api.ipify.org/");
            GeolocationModel geolocation = URL_Geolocation.Url.AppendPathSegments("json", ipv4).SetQueryParams("fields=lat,lon").GetJsonAsync <GeolocationModel>().Result;
            string           resGeolat   = string.Format(CultureInfo.InvariantCulture, "{0:N1}", geolocation.lat);
            string           resGeolon   = string.Format(CultureInfo.InvariantCulture, "{0:N}", geolocation.lon);
            var request  = await new HttpClient().GetStringAsync(URL_Weather.Url.AppendPathSegments("api", "location", "search").SetQueryParams($"lattlong={resGeolat},{resGeolon}"));
            var response = JsonConvert.DeserializeObject <List <WeatherGeo> >(request);

            return(await URL_Weather.Url.AppendPathSegments("api", "location", $"{response.ElementAt(0).woeid}").GetJsonAsync <WeatherModel>());
        }
Ejemplo n.º 4
0
        private double DistanceBetween(GeolocationModel locationOne, GeolocationModel locationTwo)
        {
            var R      = 6371e3; //earth’s radius (mean radius = 6,371km)
            var rlat1  = Math.PI * locationOne.Lat / 180;
            var rlat2  = Math.PI * locationTwo.Lat / 180;
            var rtheta = Math.PI * (locationTwo.Lng - locationOne.Lng) / 180;

            var a = Math.Sin(rlat1) * Math.Sin(rlat2) + Math.Cos(rlat1) * Math.Cos(rlat2) * Math.Cos(rtheta);

            var c = Math.Acos(a);

            return(R * c);
        }
 public async Task <bool> AddGeolocation(GeolocationModel geolocationModel)
 {
     try
     {
         using (GeolocationsContext ctx = new GeolocationsContext())
         {
             ctx.Geolocations.Add(geolocationModel);
             return(await ctx.SaveChangesAsync() > 0);
         }
     }
     catch (Exception ex)
     {
         _logger.Error("Saving geolocation error " + ex.Message);
         throw ex;
     }
 }
Ejemplo n.º 6
0
        public async Task PickWinner(int rollId, GeolocationModel winningPoint)
        {
            var winningRoll = await _transactionRepository
                              .GetAll <UserRollEntity>(r => r.RollId == rollId)
                              .OrderBy(u => DistanceBetween(new GeolocationModel {
                Lng = u.Lng, Lat = u.Lat
            }, winningPoint))
                              .FirstOrDefaultAsync();

            if (winningRoll != null)
            {
                winningRoll.HasWon = true;

                if (_transactionRepository.Update(winningRoll) == 0)
                {
                    throw new TransactionRepositoryException("Failed to update winning user roll");
                }
            }
        }
Ejemplo n.º 7
0
        public async Task <GeolocationModel> RequestGeoInfoAsync(string ip)
        {
            StringBuilder sb     = new StringBuilder();
            string        apiUrl = sb.AppendFormat(_url, ip).ToString();

            using (HttpResponseMessage response = await _client.PostAsJsonAsync("https://localhost:44316/api/IpInfo", apiUrl))
            {
                if (response.IsSuccessStatusCode)
                {
                    GeolocationModel result = await response.Content.ReadAsAsync <GeolocationModel>();

                    return(result);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }
Ejemplo n.º 8
0
        public async Task <RollEntity> FinishRoll(GeolocationModel winningPoint)
        {
            var roll = await _transactionRepository.GetAll <RollEntity>(r => !r.IsFinished).SingleOrDefaultAsync();

            if (roll != null)
            {
                roll.IsFinished = true;
                roll.WinningLat = winningPoint.Lat;
                roll.WinningLng = winningPoint.Lng;

                if (_transactionRepository.Update(roll) == 0)
                {
                    throw new TransactionRepositoryException("Failed to update old roll");
                }

                return(roll);
            }

            return(null);
        }
Ejemplo n.º 9
0
        public static async Task GetGeolocationInfo(GeolocationModel mod)
        {
            try
            {
                var geoLocation = await Geolocation.GetLastKnownLocationAsync();

                if (geoLocation != null)
                {
                    mod.Lat = geoLocation.Latitude.ToString();
                    mod.Lon = geoLocation.Longitude.ToString();
                    //Preferences.Set("Latitude", geoLocation.Latitude.ToString());
                    //Preferences.Set("Longitude", geoLocation.Longitude.ToString());
                }

                var geoLocations = await Geocoding.GetPlacemarksAsync(geoLocation);

                var geoPlacemark = geoLocations.FirstOrDefault();
                mod.Location = geoPlacemark.Locality;
            }
            catch (Exception ex)
            {
                await App.Current.MainPage.DisplayAlert("Błąd", $"Coś poszło nie tak {ex}", "OK");
            }
        }
Ejemplo n.º 10
0
        public GeolocationModelTests()
        {
            _geolocationModel = new GeolocationModel();

            _geopoint = new Geopoint(_geoposition);
        }
 public GetLocationResponse MapGeolocationModelToResponse(GeolocationModel geolocationModel)
 {
     return(_mapper.Map <GeolocationModel, GetLocationResponse>(geolocationModel));
 }