Exemple #1
0
        private static void SetForecastData <TIn, TOut>(
            Dictionary <DateTime, ForecastInfo> forecasts,
            Dictionary <Period, TIn> periodData,
            Func <IEnumerable <KeyValuePair <Period, TIn> >, TOut> getValue,
            Action <ForecastInfo, TOut> setValue)
        {
            if (periodData != null)
            {
                foreach (var dateGroup in periodData.GroupBy(pair => pair.Key.Start.Date))
                {
                    DateTime date  = dateGroup.Key;
                    TOut     value = getValue(dateGroup);

                    if (!forecasts.TryGetValue(date, out ForecastInfo forecast))
                    {
                        forecast = new ForecastInfo
                        {
                            Day = date.ToString("ddd"),
#pragma warning disable MEN013 // Use UTC time. This compares against the user's local date.
                            IsToday = date == DateTime.Today,
#pragma warning restore MEN013 // Use UTC time
                        };
                        forecasts.Add(date, forecast);
                    }

                    setValue(forecast, value);
                }
            }
        }
Exemple #2
0
        protected override async Task UpdateAsync()
        {
            if (string.IsNullOrEmpty(this.Settings.UserLocation))
            {
                this.Weather.SetError("A US Zip code location must be specified.");
            }
            else
            {
                if (this.locationRequestParameters == null)
                {
                    this.locationRequestParameters = await this.CacheLocationRequestParametersAsync().ConfigureAwait(false);
                }

                if (this.locationRequestParameters == null)
                {
                    this.Weather.SetError("Unable to determine latitude and longitude.");
                }
                else
                {
                    Uri      weatherDataUri = BuildlUri("http://forecast.weather.gov/MapClick.php", this.locationRequestParameters);
                    XElement weatherData    = await this.GetXmlAsync(weatherDataUri).ConfigureAwait(false);

                    if (this.IsDwml(weatherData))
                    {
                        var dataElements = weatherData.Elements("data");
                        this.Weather.Current        = this.GetCurrent(dataElements.FirstOrDefault(e => (string)e.Attribute("type") == "current observations"));
                        this.Weather.DailyForecasts = this.GetForecasts(dataElements.FirstOrDefault(e => (string)e.Attribute("type") == "forecast"));

                        // After mid-day, Weather.gov stops giving the high temp and icon forecast for today, so we'll get it from Current.
                        ForecastInfo todaysForecast = this.Weather.DailyForecasts.FirstOrDefault(f => f.IsToday);
                        if (todaysForecast != null && this.Weather.Current != CurrentInfo.Missing)
                        {
                            todaysForecast.ImageUri = this.Weather.Current.ImageUri;
                            if (todaysForecast.High == null)
                            {
                                todaysForecast.High = this.Weather.Current.TemperatureValue;
                                if (todaysForecast.High < todaysForecast.Low)
                                {
                                    todaysForecast.Low = todaysForecast.High;
                                }
                            }
                        }
                    }
                }
            }
        }