//address to the site and building weatherdata sturcture public WeatherData GetWeatherData(Location location) { XDocument xdoc=null; WeatherData WD=new WeatherData(); string addr=""; try { if ( location.countryName == null || location.cityName == null) { throw (new WeatherDataServiceException("WeatherDataServiceException : location.countryName or location.countryName are null")); } //sending a correct sturcure of query string to the site addr = "http://api.wunderground.com/api/" + Key + "/conditions/q/" + location.countryName + "/" + location.cityName + ".xml"; xdoc = XDocument.Load(addr); } catch (Exception e) { Console.WriteLine(e.Message); } try { //parsing the XML recieved from the site by its unique stucture var list = from item in xdoc.Descendants("response") select new { Name = item.Element("current_observation").Element("display_location").Element("city").Value, Temp = item.Element("current_observation").Element("temp_c").Value, Pressure = item.Element("current_observation").Element("pressure_mb").Value, Humidity = item.Element("current_observation").Element("relative_humidity").Value, WindSpeed = item.Element("current_observation").Element("wind_kph").Value, WindDirection = item.Element("current_observation").Element("wind_dir").Value }; foreach (var item in list) { //building the weatherdata structure WD.cityName = item.Name; WD.temp = double.Parse(item.Temp); WD.pressure = int.Parse(item.Pressure); WD.humidity = item.Humidity; WD.windSpeed = double.Parse(item.WindSpeed); WD.windDirection = item.WindDirection; } } catch (Exception e) { Console.WriteLine(e.Message + " : caused from parsing error or wrong values in the Location"); } return WD; }
//address to the site and building weatherdata sturcture public WeatherData GetWeatherData(Location location) { XDocument xdoc=null; WeatherData WD=new WeatherData(); string addr=""; try { if (location.countryName == null || location.cityName == null) { throw (new WeatherDataServiceException("WeatherDataServiceException : location.countryName or location.countryName are null")); } //sending a correct sturcure of query string to the site addr = "http://api.openweathermap.org/data/2.5/weather?q=" + location.cityName + "," + location.countryName + "&mode=xml"; xdoc = XDocument.Load(addr); } catch (Exception e) { Console.WriteLine(e.Message); } try { //parsing the XML recieved from the site by its unique stucture var list = from item in xdoc.Descendants("current") select new { Name = item.Element("city").Attribute("name").Value, Temp = item.Element("temperature").Attribute("value").Value, Pressure = item.Element("pressure").Attribute("value").Value, Humidity = item.Element("humidity").Attribute("value").Value, WindSpeed = item.Element("wind").Element("speed").Attribute("value").Value, WindDirection = item.Element("wind").Element("direction").Attribute("code").Value }; foreach (var item in list) { //building the weatherdata structure WD.cityName = item.Name; WD.temp = FarToCell(double.Parse(item.Temp)); WD.pressure = int.Parse(item.Pressure); WD.humidity = item.Humidity + "%"; WD.windSpeed = double.Parse(item.WindSpeed) * 3.6; //convert from mps to kph WD.windDirection = item.WindDirection; } } catch (Exception e) { Console.WriteLine(e.Message + " : caused from internet problem or wrong values in the Location"); } return WD; }
//address to the site and building weatherdata sturcture public WeatherData GetWeatherData(Location location) { XDocument xdoc=null; WeatherData WD=new WeatherData(); string addr=""; try { if (location.countryName == null || location.cityName == null) { throw (new WeatherDataServiceException("WeatherDataServiceException : location.countryName or location.countryName are null")); } //sending a correct sturcure of query string to the site addr = "http://api.worldweatheronline.com/free/v2/weather.ashx?key=" + Key + "&q=" + location.cityName + ","+ location.countryName + "&format=xml"; xdoc = XDocument.Load(addr); } catch (Exception e) { Console.WriteLine(e.Message); } try { //parsing the XML recieved from the site by its unique stucture var list = from item in xdoc.Descendants("data") select new { Name = item.Element("request").Element("query").Value, Temp = item.Element("current_condition").Element("temp_C").Value, Pressure = item.Element("current_condition").Element("pressure").Value, Humidity = item.Element("current_condition").Element("humidity").Value, WindSpeed = item.Element("current_condition").Element("windspeedKmph").Value, WindDirection = item.Element("current_condition").Element("winddir16Point").Value }; foreach (var item in list) { //building the weatherdata structure WD.cityName = item.Name; WD.temp = double.Parse(item.Temp); WD.pressure = int.Parse(item.Pressure); WD.humidity = item.Humidity + "%"; WD.windSpeed = double.Parse(item.WindSpeed); WD.windDirection = item.WindDirection; } } catch (Exception e) { Console.WriteLine(e.Message + " : caused from internet problem or wrong values in the Location"); } return WD; }
static void Main() { //example of use , sample code of how to implement the factory and the interface //declaring and assiging Location object Location loc = new Location(); loc.cityName = "london"; loc.countryName = "uk"; //requesting the 1st provider using the provider identifier in the factory IWeatherDataService service = WeatherDataServiceFactory.getWeatherDataService(WeatherDataServiceFactory.Providers.WUNDERGROUND); //recieving a WeatherData object from the first provider WeatherData weatherData = service.GetWeatherData(loc); //printing the WeatherData fields, OUTPUT Example : //WUNDERGROUND: city: London, temp: 14 celsius, humidity: 94 %, pressure: 1004 hPa, wind: 20 kPh West Console.WriteLine("WUNDERGROUND : city : " + weatherData.cityName + ", temp : " + weatherData.temp + " celsius" + ", humidity : " + weatherData.humidity + ", pressure : " + weatherData.pressure + " hPa" + ", wind : " + weatherData.windSpeed + " kPh" + " " + weatherData.windDirection); //requesting the 2nd provider using the provider identifier in the factory service = WeatherDataServiceFactory.getWeatherDataService(WeatherDataServiceFactory.Providers.OPENMAP); //recieving a WeatherData object from the 2nd provider weatherData = service.GetWeatherData(loc); //printing the WeatherData fields, OUTPUT Example : //OPENMAP : city : London, temp : 14.1 celsius, humidity : 82%, pressure : 1002 hPa, wind : 24.12 kPh W Console.WriteLine("OPENMAP : city : " + weatherData.cityName + ", temp : " + weatherData.temp + " celsius" + ", humidity : " + weatherData.humidity + ", pressure : " + weatherData.pressure + " hPa" + ", wind : " + weatherData.windSpeed + " kPh" + " " + weatherData.windDirection); //requesting the 3rd provider using the provider identifier in the factory service = WeatherDataServiceFactory.getWeatherDataService(WeatherDataServiceFactory.Providers.WORLDWEATHERONLINE); //recieving a WeatherData object from the 3rd provider weatherData = service.GetWeatherData(loc); //printing the WeatherData fields,OUTPUT Example : //WORLDWEATHERONLINE : city : London, United Kingdom, temp : 16 celsius, humidity : 82%, pressure : 1003 hPa, wind : 24 kPh W Console.WriteLine("WORLDWEATHERONLINE : city : " + weatherData.cityName + ", temp : " + weatherData.temp + " celsius" + ", humidity : " + weatherData.humidity + ", pressure : " + weatherData.pressure + " hPa" + ", wind : " + weatherData.windSpeed + " kPh" + " " + weatherData.windDirection); }
public void WunderGroundTest() { Location loc = new Location(); WeatherData WDCheck = null; string actualCity = "London"; loc.cityName = "london"; loc.countryName = "UK"; WeatherDataProviderWunderGround WD = WeatherDataProviderWunderGround.Instance; WDCheck = WD.GetWeatherData(loc); Assert.AreEqual(WDCheck.cityName, actualCity); }
public void WorldWeatherOnlineTest() { Location loc = new Location(); WeatherData WDCheck = null; string actualCity = "London, United Kingdom"; loc.cityName = "london"; loc.countryName = "UK"; WeatherDataProviderWorldWeatherOnline WD = WeatherDataProviderWorldWeatherOnline.Instance; WDCheck = WD.GetWeatherData(loc); Assert.AreEqual(WDCheck.cityName, actualCity); }