/// <summary> /// A method that implements the singleton Design Pattern that creates /// only 1 object. /// </summary> /// <returns>return a WeatherData object in type of WeatherDatasite1</returns> public static WeatherDataSite2 Instance() { if (weatherdata == null) { weatherdata = new WeatherDataSite2(); } return weatherdata; }
/// <summary> /// A method that implements the singleton Design Pattern that creates /// only 1 object. /// </summary> /// <returns>return a WeatherData object in type of WeatherDatasite1</returns> public static WeatherDataSite2 Instance() { if (weatherdata == null) { weatherdata = new WeatherDataSite2(); } return(weatherdata); }
/// <summary> /// This method get from the user the web service that he want /// to create an object, and create a singleton object. /// </summary> /// <param name="str">The desired service.</param> /// <returns>Return an object from the type of the chosen REStful Web Service.</returns> public static WeatherData getWeatherDataService(string str) { WeatherData wd = null; if (str.Equals("OPEN_WEATHER_MAP")) { wd = WeatherDataSite1.Instance(); } else if (str.Equals("WORLD_WEATHER_ONLINE")) { wd = WeatherDataSite2.Instance(); } return(wd); }
/// <summary> /// A virtual method that get the weather object of a chosen country. /// </summary> /// <param name="location">Location requested by user to get service.</param> /// <returns>return a WeatherData object with all the params. </returns> public override WeatherData getWeatherData(Location location) { WeatherDataSite2 wd = new WeatherDataSite2(); try { wd.location.Country = location.Country; XMLFunction(wd, location); } catch (WeatherDataServiceException ex) { Console.WriteLine(ex.Message); } return wd; }
/// <summary> /// A virtual method that get the weather object of a chosen country. /// </summary> /// <param name="location">Location requested by user to get service.</param> /// <returns>return a WeatherData object with all the params. </returns> public override WeatherData getWeatherData(Location location) { WeatherDataSite2 wd = new WeatherDataSite2(); try { wd.location.Country = location.Country; XMLFunction(wd, location); } catch (WeatherDataServiceException ex) { Console.WriteLine(ex.Message); } return(wd); }
/// <summary> /// A virtual method that get the weather object of a chosen country. /// </summary> /// <param name="wd">The WeatherData object. </param> /// <param name="location">Location requested by user to get service.</param> /// <returns>return a WeatherData object with all the params. </returns> public void XMLFunction(WeatherDataSite2 wd, Location location) { String URLString = "http://api.worldweatheronline.com/premium/v1/weather.ashx?key=" + key + "&q=" + location.Country + "&num_of_days=1&tp=24&format=xml"; string xml; using (WebClient client = new WebClient()) { try { xml = client.DownloadString(URLString);// xml url to string } catch (WebException) { throw new WeatherDataServiceException("There is not internet connection"); } } try { XDocument ob = XDocument.Parse(xml); //A linq to xml that get all the values from the site var weather = from x in ob.Descendants("data") select new { City = x.Descendants("query").First().Value, Ip = x.Descendants("type").First().Value, Sun = x.Descendants("sunrise").First().Value, Set = x.Descendants("sunset").First().Value, Tempat = x.Descendants("temp_C").First().Value, Cloud = x.Descendants("cloudcover").First().Value, Humidity = x.Descendants("humidity").First().Value, Speed = x.Descendants("windspeedKmph").First().Value, Direction = x.Descendants("winddir16Point").First().Value, Update = x.Descendants("date").First().Value, }; //Get all the values from the linq vairables and set //them into the WeatherData service values. foreach (var data in weather) { //This restful web service also support an ip search. //this check is to confrim that the user pressed a country. if(data.Ip=="IP") { throw new XmlException(); } wd.location.Country = data.City; wd.sunrise.Sunrise = data.Sun; wd.sunset.Sunset = data.Set; wd.lastupdate.Update = data.Update; wd.temperature.Celsius = Double.Parse(data.Tempat); wd.cloud.Clouds = data.Cloud; wd.humidity.Humitidy = data.Humidity; wd.wind.Speed = Double.Parse(data.Speed); wd.wind.Direction = data.Direction; } } catch (XmlException) { throw new WeatherDataServiceException("Wrong Country"); } catch (WebException) { throw new WeatherDataServiceException("There is not internet connection"); } catch (InvalidOperationException ex) { throw new WeatherDataServiceException(ex.Message); } }
/// <summary> /// A virtual method that get the weather object of a chosen country. /// </summary> /// <param name="wd">The WeatherData object. </param> /// <param name="location">Location requested by user to get service.</param> /// <returns>return a WeatherData object with all the params. </returns> public void XMLFunction(WeatherDataSite2 wd, Location location) { String URLString = "http://api.worldweatheronline.com/premium/v1/weather.ashx?key=" + key + "&q=" + location.Country + "&num_of_days=1&tp=24&format=xml"; string xml; using (WebClient client = new WebClient()) { try { xml = client.DownloadString(URLString);// xml url to string } catch (WebException) { throw new WeatherDataServiceException("There is not internet connection"); } } try { XDocument ob = XDocument.Parse(xml); //A linq to xml that get all the values from the site var weather = from x in ob.Descendants("data") select new { City = x.Descendants("query").First().Value, Ip = x.Descendants("type").First().Value, Sun = x.Descendants("sunrise").First().Value, Set = x.Descendants("sunset").First().Value, Tempat = x.Descendants("temp_C").First().Value, Cloud = x.Descendants("cloudcover").First().Value, Humidity = x.Descendants("humidity").First().Value, Speed = x.Descendants("windspeedKmph").First().Value, Direction = x.Descendants("winddir16Point").First().Value, Update = x.Descendants("date").First().Value, }; //Get all the values from the linq vairables and set //them into the WeatherData service values. foreach (var data in weather) { //This restful web service also support an ip search. //this check is to confrim that the user pressed a country. if (data.Ip == "IP") { throw new XmlException(); } wd.location.Country = data.City; wd.sunrise.Sunrise = data.Sun; wd.sunset.Sunset = data.Set; wd.lastupdate.Update = data.Update; wd.temperature.Celsius = Double.Parse(data.Tempat); wd.cloud.Clouds = data.Cloud; wd.humidity.Humitidy = data.Humidity; wd.wind.Speed = Double.Parse(data.Speed); wd.wind.Direction = data.Direction; } } catch (XmlException) { throw new WeatherDataServiceException("Wrong Country"); } catch (WebException) { throw new WeatherDataServiceException("There is not internet connection"); } catch (InvalidOperationException ex) { throw new WeatherDataServiceException(ex.Message); } }