示例#1
0
        public List <Weather> GetWeather(DateRange dateRange, Location location)
        {
            // dateRange is not used because method returns forecast for today and three more days

            ThreeDaysForecastModel model  = ThreeDaysForecast(location);
            List <Weather>         result = ConvertForecastModel(model);

            return(result);
        }
示例#2
0
        private ThreeDaysForecastModel ThreeDaysForecast(Location location)
        {
            using (var client = new WebClient())
            {
                ThreeDaysForecastModel model = null;
                try
                {
                    string requestUrl = string.Format(@"http://api.wunderground.com/api/{0}/forecast/q/{1},{2}.json",
                                                      ApiKey, location.Latitude, location.Longitude);
                    var json       = client.DownloadString(requestUrl);
                    var serializer = new JavaScriptSerializer();

                    model = serializer.Deserialize <ThreeDaysForecastModel>(json);
                }
                catch (Exception e)
                {
                }
                return(model);
            }
        }
示例#3
0
        private List <Weather> ConvertForecastModel(ThreeDaysForecastModel model)
        {
            List <Weather> result = new List <Weather>();

            if (model != null && model.Forecast != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    Weather resultItem = new Weather();

                    resultItem.Temperature =
                        DataExtractor.GetAverageTemperature(model.Forecast.SimpleForecast.ForecastDay[i]);
                    resultItem.Cloudness     = DataExtractor.GetCloudness(model.Forecast.SimpleForecast.ForecastDay[i]);
                    resultItem.Precipitation =
                        DataExtractor.GetPrecipitation(model.Forecast.SimpleForecast.ForecastDay[i]);
                    var forecastDayDate = model.Forecast.SimpleForecast.ForecastDay[i].Date;
                    resultItem.Date = new DateTime(forecastDayDate.Year, forecastDayDate.Month, forecastDayDate.Day);
                    result.Add(resultItem);
                }
            }

            return(result);
        }