/// <summary>
        /// This method is responsible to send http request to the provided URL and get the reponse to XML,
        /// The XML will provide all the detalis we need to build Weather Data instance and return it.
        /// </summary>   
        public WeatherData GetWeatherData(Location location)
        {
            //wData is the instance that holds the weather info.
            var wData = new WeatherData();

            //xdoc is var that holds the xml elements and parse them
            XDocument xdoc;

            //api var holds the http response.
            var api = string.Format("http://api.worldweatheronline.com/free/v2/weather.ashx?key=dbc688a2aa9e77e11005ceb43ab37&q={0}&format=xml", location.Name);
            //Parse the XML
            try
            {
                xdoc = XDocument.Load(api);

                var list = from item in xdoc.Descendants("data")
                    select new
                    {

                        Name = item.Element("request").Element("query").Value,
                        LastTime = item.Element("current_condition").Element("observation_time").Value,
                        TempValue = item.Element("current_condition").Element("temp_C").Value,
                        LastDate = item.Element("weather").Element("date").Value,
                        SunRise = item.Element("weather").Element("astronomy").Element("sunrise").Value,
                        SunSet = item.Element("weather").Element("astronomy").Element("sunset").Value,

                        WindSpeedValue = item.Element("weather").Element("hourly").Element("windspeedMiles").Value,

                        IconName = item.Element("weather").Element("hourly").Element("weatherIconUrl").Value,
                        IconText = item.Element("weather").Element("hourly").Element("weatherDesc").Value

                    };
                //Insert the relevant parameters to wData instance.
                foreach (var data in list)
                {
                    wData.City = new City();
                    var tokens = data.Name.Split(',');
                    wData.City.Name = tokens[0];
                    wData.City.Country = tokens[1];

                    wData.City.Sun.Rise = DateTime.Parse(data.SunRise);
                    wData.City.Sun.Set = DateTime.Parse(data.SunSet);

                    wData.Temp = new Temperature();
                    wData.Temp.Value = double.Parse(data.TempValue);

                    wData.Wind = new Wind();
                    wData.Wind.Speed = new Speed();
                    wData.Wind.Speed.Value = double.Parse(data.WindSpeedValue);

                    wData.Weather = new Weather();
                    wData.Weather.Icon = data.IconName;
                    wData.Weather.Value = data.IconText;

                    wData.LastUpdateTime = DateTime.Parse(data.LastDate + " " + data.LastTime);
                }
            }

            //Exceptions
            catch (WebException)
            {
                throw new WeatherDataServiceException("Web connectivity Exception");
            }
            catch (Exception)
            {

                throw new WeatherDataServiceException("Parsing Exception");
            }

            //return the wData instance which now is updated .
            return wData;
        }
        public void GetWeatherDataTest()
        {
            //location is a city that I choose : "bay yam" to compare the data .
            Location location = new Location("bat yam");
            ///wDataTest holds the Weather Data info for Bat yam city in IL, it's the object that return from the method.
            WeatherData wDataTest = OpenMapDataService.Instance.GetWeatherData(location);

            //request the data from web service.
            XDocument xdoc;

            //api var holds the http response.
            var api = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&mode=xml", location.Name);

            //Parse the xdoc and get the relevant info.
            try
            {
                xdoc = XDocument.Load(api);
                var list = from item in xdoc.Descendants("current")
                           select new
                           {

                               Name = item.Element("city").Attribute("name").Value,
                               CoordLat = item.Element("city").Element("coord").Attribute("lat").Value,
                               CoordLon = item.Element("city").Element("coord").Attribute("lon").Value,
                               Country = item.Element("city").Element("country").Value,
                               SunRise = item.Element("city").Element("sun").Attribute("rise").Value,
                               SunSet = item.Element("city").Element("sun").Attribute("set").Value,

                               TempValue = item.Element("temperature").Attribute("value").Value,

                               WindSpeedValue = item.Element("wind").Element("speed").Attribute("value").Value,
                               WindSpeedText = item.Element("wind").Element("speed").Attribute("name").Value,

                               IconName = item.Element("weather").Attribute("icon").Value,
                               IconText = item.Element("weather").Attribute("value").Value,
                               LastUpdate = item.Element("lastupdate").Attribute("value").Value

                           };
                //Check if the Data is Equal
                foreach (var data in list)
                {
                    //Check City parameters

                                Assert.AreEqual(wDataTest.City.Name, data.Name);
                                Assert.AreEqual(wDataTest.City.Country, data.Country);
                                Assert.AreEqual(wDataTest.City.Coords.Lat,double.Parse(data.CoordLat));
                                Assert.AreEqual(wDataTest.City.Coords.Lon,double.Parse(data.CoordLon));
                                Assert.AreEqual(wDataTest.City.Sun.Rise,DateTime.Parse(data.SunRise));
                                Assert.AreEqual(wDataTest.City.Sun.Set,DateTime.Parse(data.SunSet));

                                //Check Temp parameters
                                Console.WriteLine(double.Parse(data.TempValue)- 272.15);
                                Console.WriteLine(wDataTest.Temp.Value);

                                Assert.AreEqual(wDataTest.Temp.Value, double.Parse(data.TempValue) - 272.15);

                                //Check Wind parameters
                                Assert.AreEqual(wDataTest.Wind.Speed.Value,double.Parse(data.WindSpeedValue));
                                Assert.AreEqual(wDataTest.Wind.Speed.Name,data.WindSpeedText);

                                //Check the Time
                                Assert.AreEqual(wDataTest.LastUpdateTime,DateTime.Parse(data.LastUpdate));

                                Assert.Inconclusive("Verify the correctness of this test method.");

                }
            }
            //Exceptions
            catch (AssertFailedException)
            {
                throw new WeatherDataServiceException("The Comparsion between the objects is failed");
            }
            catch (WebException)
            {
                throw new WeatherDataServiceException("Web connectivity Exception");
            }
            catch (Exception)
            {

                throw new WeatherDataServiceException("Parsing Exception");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This method is responsible to send http request to the web service and get the reponse to XML,
        /// The XML will provide all the info we need to build wData instance and return it.
        /// </summary>
        public WeatherData GetWeatherData(Location location)
        {
            //xdoc is var that holds the xml elements and parse them
            XDocument xdoc;

            //wData is the instance that holds the weather info.
            var wData = new WeatherData();

            //api var holds the http response.
            var api = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&mode=xml", location.Name);

            try
            {
                //Parse the xdoc and get the relevant info.

                xdoc = XDocument.Load(api);

                var list = from item in xdoc.Descendants("current")
                    select new
                    {

                        Name = item.Element("city").Attribute("name").Value,
                        CoordLat = item.Element("city").Element("coord").Attribute("lat").Value,
                        CoordLon = item.Element("city").Element("coord").Attribute("lon").Value,
                        Country = item.Element("city").Element("country").Value,
                        SunRise = item.Element("city").Element("sun").Attribute("rise").Value,
                        SunSet = item.Element("city").Element("sun").Attribute("set").Value,

                        TempValue = item.Element("temperature").Attribute("value").Value,

                        WindSpeedValue = item.Element("wind").Element("speed").Attribute("value").Value,
                        WindSpeedText = item.Element("wind").Element("speed").Attribute("name").Value,

                        IconName = item.Element("weather").Attribute("icon").Value,
                        IconText = item.Element("weather").Attribute("value").Value,
                        LastUpdate = item.Element("lastupdate").Attribute("value").Value

                    };

                //Insert the relevant parameters to wData instance.
                foreach (var data in list)
                {
                    //Insert the city parameters
                    wData.City = new City();
                    wData.City.Name = data.Name;
                    wData.City.Country = data.Country;
                    wData.City.Coords.Lat = double.Parse(data.CoordLat);
                    wData.City.Coords.Lon = double.Parse(data.CoordLon);
                    wData.City.Sun.Rise = DateTime.Parse(data.SunRise);
                    wData.City.Sun.Set = DateTime.Parse(data.SunSet);

                    //Insert the temperature parameters
                    wData.Temp = new Temperature();
                    wData.Temp.Value = double.Parse(data.TempValue);

                    //Kelvin unit becomes C
                    wData.Temp.Value -= 272.15;

                    //Insert the wind parameters
                    wData.Wind = new Wind();
                    wData.Wind.Speed = new Speed();
                    wData.Wind.Speed.Value = double.Parse(data.WindSpeedValue);
                    wData.Wind.Speed.Name = data.WindSpeedText;

                    //Insert the weather conclusion with the suitable icon.
                    wData.Weather = new Weather();
                    wData.Weather.Icon = "http://openweathermap.org/img/w/" + data.IconName + ".png";
                    wData.Weather.Value = data.IconText;

                    //Insert the last update time
                    wData.LastUpdateTime = DateTime.Parse(data.LastUpdate);
                }
            }
            //Exceptions
            catch (WebException)
            {
                throw new WeatherDataServiceException("Web connectivity Exception");
            }
            catch (Exception)
            {

                throw new WeatherDataServiceException("Parsing Exception");
            }
            //return the wData instance.
            return wData;
        }