private async Task <Metar> MakeRequest() { var metar = new Metar(); HttpClient client = new HttpClient(); Stream dataStream = await client.GetStreamAsync(RequestString); // System.Uri uri = new Uri("https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=ksea&hoursBeforeNow=2"); //try //{ // //var dataStream = await client.GetStreamAsync(RequestString); // //dataStream = await client.GetStreamAsync(uri); // for (int i = 0; i < 5; i++) // { // // client.Timeout = new System.TimeSpan(30000); // dataStream = await client.GetStreamAsync(RequestString); // // dataStream = await client.GetStreamAsync("https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=ksea&hoursBeforeNow=2"); // if (dataStream != null) // { // break; // } // Thread.Sleep(2000); // } //} //catch (Exception e) //{ // // Type of exception? //} //var document = XDocument.Load(dataStream); //var metarElement = document.Descendants().FirstOrDefault(a => a.Name == "METAR"); //if (metarElement == null) throw new AirportNotFoundException("Invalid Station ID Entered: No METARs found"); //metar.RawText = document.Descendants().FirstOrDefault(a => a.Name == "raw_text").Value; //metar.Time = DateTime.Parse(document.Descendants().FirstOrDefault(a => a.Name == "observation_time").Value, CultureInfo.InvariantCulture); //metar.Time = TimeZoneInfo.ConvertTimeToUtc(metar.Time); //metar.Temp = Convert.ToSingle(document.Descendants().FirstOrDefault(a => a.Name == "temp_c").Value); //metar.DewPoint = Convert.ToSingle(document.Descendants().FirstOrDefault(a => a.Name == "dewpoint_c").Value); //metar.WindDirection = Convert.ToInt16(document.Descendants().FirstOrDefault(a => a.Name == "wind_dir_degrees").Value); //metar.WindSpeed = Convert.ToInt16(document.Descendants().FirstOrDefault(a => a.Name == "wind_speed_kt").Value); //metar.Visibility = Convert.ToSingle(document.Descendants().FirstOrDefault(a => a.Name == "visibility_statute_mi").Value); //var skyElements = metarElement.Descendants().Where(a => a.Name == "sky_condition"); //foreach (var skyElement in skyElements) //{ // if (skyElement.Attribute("cloud_base_ft_agl") != null) // { // metar.SkyCondition += ConvertSkyCondition(skyElement.Attribute("sky_cover").Value, // skyElement.Attribute("cloud_base_ft_agl").Value); // } // else // { // metar.SkyCondition += ConvertSkyCondition(skyElement.Attribute("sky_cover").Value, ""); // } //} return(metar); }
private async Task<Metar> MakeRequest() { var metar = new Metar(); await Task.Delay(3000); HttpClient client = new HttpClient(); var dataStream = await client.GetStreamAsync(RequestString); var document = XDocument.Load(dataStream); var metarElement = document.Descendants().FirstOrDefault(a => a.Name == "METAR"); if (metarElement == null) throw new AirportNotFoundException("Invalid Station ID Entered: No METARs found"); metar.RawText = document.Descendants().FirstOrDefault(a => a.Name == "raw_text").Value; metar.Time = DateTime.Parse(document.Descendants().FirstOrDefault(a => a.Name == "observation_time").Value, CultureInfo.InvariantCulture); metar.Time = TimeZoneInfo.ConvertTimeToUtc(metar.Time); metar.Temp = Convert.ToSingle(document.Descendants().FirstOrDefault(a => a.Name == "temp_c").Value); metar.DewPoint = Convert.ToSingle(document.Descendants().FirstOrDefault(a => a.Name == "dewpoint_c").Value); metar.WindDirection = Convert.ToInt16(document.Descendants().FirstOrDefault(a => a.Name == "wind_dir_degrees").Value); metar.WindSpeed = Convert.ToInt16(document.Descendants().FirstOrDefault(a => a.Name == "wind_speed_kt").Value); metar.Visibility = Convert.ToSingle(document.Descendants().FirstOrDefault(a => a.Name == "visibility_statute_mi").Value); var skyElements = metarElement.Descendants().Where(a => a.Name == "sky_condition"); foreach (var skyElement in skyElements) { if (skyElement.Attribute("cloud_base_ft_agl") != null) { metar.SkyCondition += ConvertSkyCondition(skyElement.Attribute("sky_cover").Value, skyElement.Attribute("cloud_base_ft_agl").Value); } else { metar.SkyCondition += ConvertSkyCondition(skyElement.Attribute("sky_cover").Value, ""); } } return metar; }