예제 #1
0
        protected override WeatherFeedResult ConvertResult(Base.ConnectionInfo connInfo, System.IO.Stream stream, Base.SettingsBase settings)
        {
            List <WeatherFeed> feeds  = new List <WeatherFeed>();
            XDocument          xmlDoc = MyHelper.ParseXmlDocument(stream);

            if (xmlDoc != null)
            {
                foreach (XElement feedNode in XPath.GetElements("//channel", xmlDoc))
                {
                    WeatherFeed feed = this.ToWeatherFeed(feedNode);
                    if (feed != null)
                    {
                        feeds.Add(feed);
                    }
                }
            }
            return(new WeatherFeedResult(feeds.ToArray()));
        }
예제 #2
0
        private WeatherFeed ToWeatherFeed(XElement channelNode)
        {
            System.Globalization.CultureInfo convCulture = new System.Globalization.CultureInfo("en-US");

            if (channelNode != null)
            {
                RSS.Feed defaultFeed = RSS.ImportExport.XML.ToFeed(channelNode);
                if (defaultFeed != null)
                {
                    defaultFeed.Items.Clear();
                    WeatherFeed feed = new WeatherFeed();
                    feed.CopyValues(defaultFeed);

                    foreach (XElement prpNode in channelNode.Elements())
                    {
                        if (prpNode.Name.NamespaceName.ToLower() == "yweather")
                        {
                            switch (prpNode.Name.LocalName.ToLower())
                            {
                            case "location":
                                feed.Location = new LocationInfo();
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "city":
                                        feed.Location.City = att.Value;
                                        break;

                                    case "region":
                                        feed.Location.Region = att.Value;
                                        break;

                                    case "country":
                                        feed.Location.Country = att.Value;
                                        break;
                                    }
                                }

                                break;

                            case "units":
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "temperature":
                                        if (att.Value.ToLower() == "f")
                                        {
                                            feed.TemperatureUnit = DegreesUnit.Fahrenheit;
                                        }
                                        else
                                        {
                                            feed.TemperatureUnit = DegreesUnit.Celsius;
                                        }
                                        break;

                                    case "distance":
                                        if (att.Value.ToLower() == "mi")
                                        {
                                            feed.DistanceUnit = DistanceUnit.Miles;
                                        }
                                        else
                                        {
                                            feed.DistanceUnit = DistanceUnit.Kilometer;
                                        }
                                        break;

                                    case "pressure":
                                        if (att.Value.ToLower() == "in")
                                        {
                                            feed.PressureUnit = PressureUnit.PoundsPerSquareInch;
                                        }
                                        else
                                        {
                                            feed.PressureUnit = PressureUnit.Milibars;
                                        }
                                        break;

                                    case "speed":
                                        if (att.Value.ToLower() == "mph")
                                        {
                                            feed.SpeedUnit = DistanceUnit.Miles;
                                        }
                                        else
                                        {
                                            feed.SpeedUnit = DistanceUnit.Kilometer;
                                        }
                                        break;
                                    }
                                }

                                break;

                            case "wind":
                                feed.Wind = new WindInfo();
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "chill":
                                        int n;
                                        if (int.TryParse(att.Value, out n))
                                        {
                                            feed.Wind.Chill = n;
                                        }
                                        break;

                                    case "direction":
                                        if (int.TryParse(att.Value, out n))
                                        {
                                            feed.Wind.Direction = n;
                                        }
                                        break;

                                    case "speed":
                                        double t;
                                        if (double.TryParse(att.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                        {
                                            feed.Wind.Speed = t;
                                        }
                                        break;
                                    }
                                }

                                break;

                            case "atmosphere":
                                feed.Atmosphere = new AtmosphereInfo();
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "humidity":
                                        int n;
                                        if (int.TryParse(att.Value, out n))
                                        {
                                            feed.Atmosphere.HumidityInPercent = n;
                                        }
                                        break;

                                    case "visibility":
                                        double t;
                                        if (double.TryParse(att.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                        {
                                            feed.Atmosphere.VisibilityDistance = t;
                                        }
                                        break;

                                    case "pressure":
                                        if (double.TryParse(att.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                        {
                                            feed.Atmosphere.Pressure = t;
                                        }
                                        break;

                                    case "rising":
                                        if (att.Value == "0")
                                        {
                                            feed.Atmosphere.StateOfBarometricPressure = PressureState.Steady;
                                        }
                                        else if (att.Value == "1")
                                        {
                                            feed.Atmosphere.StateOfBarometricPressure = PressureState.Rising;
                                        }
                                        else if (att.Value == "2")
                                        {
                                            feed.Atmosphere.StateOfBarometricPressure = PressureState.Falling;
                                        }
                                        break;
                                    }
                                }

                                break;

                            case "astronomy":
                                feed.Astronomy = new AstronomyInfo();
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "sunrise":
                                        DateTime d;
                                        if (System.DateTime.TryParse(att.Value, convCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out d))
                                        {
                                            feed.Astronomy.Sunrise = d;
                                        }
                                        break;

                                    case "sunset":
                                        if (System.DateTime.TryParse(att.Value, convCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out d))
                                        {
                                            feed.Astronomy.Sunset = d;
                                        }
                                        break;
                                    }
                                }

                                break;
                            }
                        }
                        else if (prpNode.Name.LocalName.ToLower() == "item")
                        {
                            RSS.FeedItem defaultItem = RSS.ImportExport.XML.ToFeedItem(prpNode);
                            if (defaultItem != null)
                            {
                                WeatherFeedItem newItem = new WeatherFeedItem();
                                newItem.CopyValues(defaultItem);

                                foreach (XElement itemNode in prpNode.Elements())
                                {
                                    if (itemNode.Name.NamespaceName == "yweather")
                                    {
                                        switch (itemNode.Name.LocalName.ToLower())
                                        {
                                        case "condition":
                                            newItem.ActualCondition = new ActualCondition();
                                            foreach (XAttribute att in itemNode.Attributes())
                                            {
                                                switch (att.Name.LocalName.ToLower())
                                                {
                                                case "text":
                                                    newItem.ActualCondition.Description = att.Value;
                                                    break;

                                                case "code":
                                                    int i = 0;
                                                    if (int.TryParse(att.Value, out i) && (i <= Convert.ToInt32(ConditionType.Isolated_Thundershowers) | i == Convert.ToInt32(ConditionType.Not_Available)))
                                                    {
                                                        newItem.ActualCondition.Type = (ConditionType)i;
                                                    }
                                                    break;

                                                case "temp":
                                                    int n;
                                                    if (int.TryParse(att.Value, out n))
                                                    {
                                                        newItem.ActualCondition.Temperature = n;
                                                    }
                                                    break;

                                                case "date":
                                                    //Wed, 17 Aug 2011 10:18 pm CEST

                                                    string dateStr = att.Value;
                                                    int    index   = Math.Max(dateStr.LastIndexOf("am"), dateStr.LastIndexOf("pm"));
                                                    if (index > 0)
                                                    {
                                                        System.DateTime d = default(System.DateTime);
                                                        if (System.DateTime.TryParseExact(att.Value.Substring(0, index + 2), "ddd, dd MMM yyyy hh:mm tt", convCulture, System.Globalization.DateTimeStyles.None, out d))
                                                        {
                                                            newItem.ActualCondition.ForecastDate = d;
                                                        }
                                                    }
                                                    break;
                                                }
                                            }

                                            break;

                                        case "forecast":
                                            Forecast cond = new Forecast();
                                            foreach (XAttribute att in itemNode.Attributes())
                                            {
                                                switch (att.Name.LocalName.ToLower())
                                                {
                                                case "text":
                                                    cond.Description = att.Value;
                                                    break;

                                                case "code":
                                                    int i = 0;
                                                    if (int.TryParse(att.Value, out i) && (i <= Convert.ToInt32(ConditionType.Isolated_Thundershowers) | i == Convert.ToInt32(ConditionType.Not_Available)))
                                                    {
                                                        cond.Type = (ConditionType)i;
                                                    }
                                                    break;

                                                case "day":
                                                    switch (att.Value.ToLower())
                                                    {
                                                    case "mon":
                                                        cond.Day = DayOfWeek.Monday;
                                                        break;

                                                    case "tue":
                                                        cond.Day = DayOfWeek.Tuesday;
                                                        break;

                                                    case "wed":
                                                        cond.Day = DayOfWeek.Wednesday;
                                                        break;

                                                    case "thu":
                                                        cond.Day = DayOfWeek.Thursday;
                                                        break;

                                                    case "fri":
                                                        cond.Day = DayOfWeek.Friday;
                                                        break;

                                                    case "sat":
                                                        cond.Day = DayOfWeek.Saturday;
                                                        break;

                                                    case "sun":
                                                        cond.Day = DayOfWeek.Sunday;
                                                        break;
                                                    }
                                                    break;

                                                case "date":
                                                    DateTime d;
                                                    if (System.DateTime.TryParse(att.Value, convCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out d))
                                                    {
                                                        cond.ForecastDate = d;
                                                    }
                                                    break;

                                                case "low":
                                                    int n;
                                                    if (int.TryParse(att.Value, out n))
                                                    {
                                                        cond.LowTemperature = n;
                                                    }
                                                    break;

                                                case "high":
                                                    if (int.TryParse(att.Value, out n))
                                                    {
                                                        cond.HighTemperature = n;
                                                    }
                                                    break;
                                                }
                                            }

                                            newItem.ForecastConditions.Add(cond);
                                            break;
                                        }
                                    }
                                    else if (itemNode.Name.NamespaceName == "geo")
                                    {
                                        switch (itemNode.Name.LocalName.ToLower())
                                        {
                                        case "lat":
                                            double d = 0;
                                            if (double.TryParse(itemNode.Value, System.Globalization.NumberStyles.Any, convCulture, out d))
                                            {
                                                newItem.Coordinates = new Geo.Coordinates(newItem.Coordinates.Longitude, d);
                                            }
                                            break;

                                        case ("long"):
                                            if (double.TryParse(itemNode.Value, System.Globalization.NumberStyles.Any, convCulture, out d))
                                            {
                                                newItem.Coordinates = new Geo.Coordinates(d, newItem.Coordinates.Latitude);
                                            }
                                            break;
                                        }
                                    }
                                }
                                feed.Items.Add(newItem);
                            }
                        }
                    }
                    return(feed);
                }
            }
            return(null);
        }