コード例 #1
0
ファイル: METARStation.cs プロジェクト: Zunair/OpenWeatherC
        /// <summary>
        /// Gets current data from NOAA
        /// </summary>
        protected override void GetWeather()
        {
            var wc = new WebClient();

            wc.Headers.Add(HttpRequestHeader.UserAgent, "OpenWeather Client");
            var doc = XDocument.Parse(wc.DownloadString(LookupUrl));

            double result_count = 0;

            try
            {
                result_count = Convert.ToDouble(XmlTools.GetElementContent("result_count", doc, "data"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            if (result_count != 0)
            {
                var temp      = Convert.ToDouble(XmlTools.GetElementContent("temp_c", doc, "data", "METAR") + 0);
                var windSpeed = Convert.ToDouble(XmlTools.GetElementContent("wind_speed_kt", doc, "data", "METAR") + 0);
                var pressure  =
                    Convert.ToDouble(XmlTools.GetElementContent("altim_in_hg", doc, "data", "METAR") + 0) / 0.0393700732914;
                var windHeading = Convert.ToInt32(XmlTools.GetElementContent("wind_dir_degrees", doc, "data", "METAR") + 0);
                var dewpoint    = Convert.ToDouble(XmlTools.GetElementContent("dewpoint_c", doc, "data", "METAR") + 0);
                var visibility  =
                    Convert.ToDouble(XmlTools.GetElementContent("visibility_statute_mi", doc, "data", "METAR") + 0);

                temp       = Temperature.From(temp, TemperatureUnit.DegreeCelsius).As(Units.TemperatureUnit);
                windSpeed  = Speed.From(windSpeed, SpeedUnit.Knot).As(Units.WindSpeedUnit);
                pressure   = Pressure.From(pressure, PressureUnit.Torr).As(Units.PressureUnit);
                visibility = Length.From(visibility, LengthUnit.Mile).As(Units.VisibilityUnit);

                Weather = new Weather(temp, dewpoint, windSpeed, windHeading, pressure, visibility);
            }
            else
            {
                Console.WriteLine("No resluts found.");
            }

            wc.Dispose();
        }