Ejemplo n.º 1
0
        public async Task <TimeData> GetTimeDataAsync(string arg)
        {
            try
            {
                using HttpClient http = _http.CreateClient();
                GeolocationResult obj = await GetLocationDataAsync(arg);

                CultureInfo culture = GetLocalCulture(obj.Results[0].AddressComponents);

                long   currentSeconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
                string timeResult     = await http
                                        .GetStringAsync(
                    $"https://maps.googleapis.com/maps/api/timezone/json?location={obj.Results[0].Geometry.Location.Lat},{obj.Results[0].Geometry.Location.Lng}&timestamp={currentSeconds}&key={_config.GoogleApi}")
                                        .ConfigureAwait(false);

                var timeObj = timeResult.Deserialize <TimeZoneResult>();

                var timeData = new TimeData
                {
                    Address      = obj.Results[0].FormattedAddress,
                    Time         = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.UtcNow, timeObj.TimeZoneId),
                    TimeZoneName = timeObj.TimeZoneName,
                    Culture      = culture
                };

                return(timeData);
            }
            catch (Exception e)
            {
                Logger.Warn(e);
                return(null);
            }
        }
Ejemplo n.º 2
0
 public void SetWatchPosition(GeolocationResult watchResult)
 {
     WatchPositionReceived?.Invoke(this, new GeolocationEventArgs
     {
         GeolocationResult = watchResult
     });
 }
Ejemplo n.º 3
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.");
            }
        }
        private async void HandleWatchPositionReceived(object sender, GeolocationEventArgs e)
        {
            LastWatchPositionResult = e.GeolocationResult;
            if (LastWatchPositionResult.IsSuccess)
            {
                var latlng = LastWatchPositionResult.Position.ToLeafletLatLng();
                var marker = new Marker(latlng, null);
                if (WatchPath is null)
                {
                    WatchMarkers = new List <Marker> {
                        marker
                    };
                    WatchPath = new Polyline(WatchMarkers.Select(m => m.LatLng), new PolylineOptions());
                    await WatchPath.BindToJsRuntime(JSRuntime);

                    await WatchPath.AddTo(WatchMap);
                }
                else
                {
                    WatchMarkers.Add(marker);
                    await WatchPath.AddLatLng(latlng);
                }
                await marker.BindToJsRuntime(JSRuntime);

                await marker.AddTo(WatchMap);
            }
            StateHasChanged();
        }