Пример #1
0
        public List<Weather> GetWeatherFromLocation(Location location)
        {
            string lat = location.Latitude.ToString();
            string lng = location.Longitude.ToString();
            lat = lat.Replace(",", ".");
            lng = lng.Replace(",", ".");

            var URL = string.Format("http://api.openweathermap.org/data/2.5/forecast/daily?lat={0}&lon={1}&cnt=5&lang=se&mode=xml&app_id=0f30ca6751e3118751f156f2e8ac847a", lat, lng);
            var request = (HttpWebRequest)WebRequest.Create(URL);
            var xml = String.Empty;
            using (var response = request.GetResponse())
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                xml = reader.ReadToEnd();
            }
            var doc = XDocument.Parse(xml);
            var model = (from time in doc.Descendants("time")
                         select new Weather
                         {
                             LocationID = location.LocationID,
                             Date = DateTime.Parse(time.Attribute("day").Value),
                             WeatherIcon = time.Element("symbol").Attribute("var").Value,
                             Degree = Decimal.Round((decimal.Parse(time.Element("temperature").Attribute("eve").Value, CultureInfo.InvariantCulture) - (decimal)273.15), 2)
                         }).ToList();
            if (model.Count < 1)
            {
                throw new ApplicationException("Väderprognosen kunde inte hämtas från OpenWeatherMap's api");
            }

            return model;
        }
        public ActionResult Search(LocationName location)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    locationFinder = new GeoWebService();
                    weatherFinder = new WeatherWebService();
                    checkedLocation = new Location();
                    checkedLocation = locationFinder.GetLocationInfo(location);

                    List<WeatherInfo> weatherList = new List<WeatherInfo>();
                    weatherList = weatherFinder.GetWeatherInfo(checkedLocation);

                    _repository.AddWeather(weatherList);
                    _repository.Save();

                    return View("Index", weatherList);
                }
                else
                {
                    return View("Search");
                }
            }
            catch (Exception e)
            {
                string error = "This city was not found in the Geonames API";
                ModelState.AddModelError(String.Empty, error + e.InnerException);
                return View("Search");
            }
        }
Пример #3
0
        public List<Weather> GetWeatherFromLocation(Location location)
        {
            var forecast = _repository.GetWeatherFromLocation(location);
            if (forecast == null || forecast.Count < 5 || forecast[1].Date != DateTime.Today) // Bug days switch places when saving in db
            {
                // If there are old weather objects, delete them
                forecast.ForEach(weather => _repository.DeleteWeather(weather));
                forecast = new List<Weather>();

                var webservice = new WeatherWebservice();
                forecast = webservice.GetWeatherFromLocation(location);

                if (forecast != null)
                {
                    // Save in db!
                    forecast.ForEach(w => _repository.AddWeather(w));
                    _repository.Save();
                }
                else
                {
                    throw new ApplicationException("Det gick inte att hitta väderinformation varken i databasen eller via apiet.");
                }
            }
            else if (forecast[1].Date == DateTime.Today) // Bug fix that days switch places
            {
                var today = forecast[1];
                forecast[1] = forecast[0];
                forecast[0] = today;
            }
            return forecast;
        }