Пример #1
0
 private void getLocationForWeather(Event i_SelectedEvent, WeatherLocation i_Location)
 {
     if (i_SelectedEvent.Place.Location != null)
     {
         i_Location.City           = i_SelectedEvent.Place.Location.City;
         i_Location.Country        = i_SelectedEvent.Place.Location.Country;
         i_Location.CountryIsoCode = FacebookManager.GetCountryIso(i_Location.Country);
     }
     else
     {
         MessageBox.Show(string.Format("Could not find selected event location! setting to default values({0}, {1}", FacebookManager.DefaultCity, FacebookManager.DefaultCountry));
         FacebookManager.ResetToDefaultCity(i_Location);
     }
 }
        public WeatherNodes GetXMLForWeather(WeatherLocation i_Location)
        {
            bool          foundWeather  = false;
            XmlReader     reader        = null;
            XmlSerializer serializerObj = null;
            WeatherNodes  loadedObj     = null;

            ///Try with the event location first,if not found get the default
            while (foundWeather == false)
            {
                string url = string.Format(@"http://api.openweathermap.org/data/2.5/forecast?q={0},{1}&units=metric&mode=xml&appid=d6c795304f427b8b78559ab660464d80", i_Location.City, i_Location.CountryIsoCode);
                try
                {
                    reader        = XmlReader.Create(url);
                    foundWeather  = true;
                    serializerObj = new XmlSerializer(typeof(WeatherNodes));
                    loadedObj     = (WeatherNodes)serializerObj.Deserialize(reader);
                }
                catch (Exception ex)
                {
                    ////Handle case for city not found from this API:
                    if (ex is WebException)
                    {
                        WebException webExp = ex as WebException;

                        if (!(webExp == null) && ((WebException)ex).Status == WebExceptionStatus.ProtocolError)
                        {
                            HttpWebResponse resp = webExp.Response as HttpWebResponse;

                            if (resp != null && resp.StatusCode == HttpStatusCode.NotFound)
                            {
                                MessageBox.Show(string.Format("Sorry could not get weather for {0} fetching weather for {1} instead)", i_Location.City, k_defaultCity));
                                ResetToDefaultCity(i_Location);
                            }
                        }
                    }
                    else
                    {
                        throw ex;
                    }
                }
                finally
                {
                    reader.Close();
                }
            }

            return(loadedObj);
        }
Пример #3
0
        private void getWeatherForEvent(Event i_SelectedEvent)
        {
            WeatherLocation location = new WeatherLocation();

            getLocationForWeather(i_SelectedEvent, location);
            WeatherNodes loadedObj    = FacebookManager.GetXMLForWeather(location);
            Weather      eventWeather = null;

            foreach (Weather obj in loadedObj.WeatherList)
            {
                if (i_SelectedEvent.StartTime >= obj.FromTime && i_SelectedEvent.StartTime <= obj.ToTime)
                {
                    eventWeather = obj;
                }
            }

            recommendForWeather(i_SelectedEvent, eventWeather);
            pictureBoxWeatherIcon.Load(string.Format("http://openweathermap.org/img/w/{0}.png", eventWeather.WeatherSymbol.WeatherCode));
            pictureBoxEvent.Load(i_SelectedEvent.PictureLargeURL);
            panelEventDetails.Visible = true;
        }
 public void ResetToDefaultCity(WeatherLocation location)
 {
     location.City           = k_defaultCity;
     location.Country        = k_defaultCountry;
     location.CountryIsoCode = GetCountryIso(location.Country);
 }