Exemple #1
0
        private void _init()
        {
            using (SqlCommand cmd = new SqlCommand()
            {
                CommandType = CommandType.Text,
                CommandText = "SELECT TOP 1 * FROM Weather WHERE FrameId=@frameId",
            })
            {
                int provider = 0, accountId = 0;

                cmd.Parameters.AddWithValue("@frameId", this.FrameId);
                cmd.ExecuteReaderExt((dr) =>
                {
                    provider  = dr.IntOrZero("Provider");
                    accountId = dr.IntOrZero("AccountId");

                    return(false);
                });

                ProviderAccount = new OAuthAccount(provider, accountId);
            }

            // TODO: add own Woeid to Weather model

            /*Location location = new Location(DisplayId);
             * if (location.LocationId != 0)
             * {
             *  Woeid = location.Woeid;
             *  TemperatureUnit = location.TemperatureUnit;
             * }
             * else
             * {
             *  Woeid = 56199578;      // Old Sacramento
             *  TemperatureUnit = "f";
             * }*/
        }
        private async Task <Dictionary <string, object> > GetYahooWeatherAsync(OAuthAccount account, string temperatureUnit, int woeid)
        {
            string response = await YahooUtil.GetYahooWeatherDataAsync(
                account.AppId, account.ClientId, account.ClientSecret, temperatureUnit, woeid
                );

            XmlDocument doc = new XmlDocument();

            using (System.IO.StringReader sreader = new System.IO.StringReader(response))
                using (XmlReader xmlreader = new XmlTextReader(sreader) /*{ Namespaces = false }*/)
                {
                    doc.Load(xmlreader);
                }

            XmlElement channel = doc.SelectSingleNode(@"//channel[1]") as XmlElement;

            if (channel == null)
            {
                return(null);
            }

            Dictionary <string, object> map = new Dictionary <string, object>();
            XmlElement e = null;

            // wind
            e = channel.SelectSingleNode(@"*[local-name()='wind']") as XmlElement;
            if (e != null)
            {
                Dictionary <string, object> o = new Dictionary <string, object>();
                o.Add("chill", e.GetAttribute("chill"));
                o.Add("direction", e.GetAttribute("direction"));
                o.Add("speed", e.GetAttribute("speed"));
                map.Add(e.LocalName, o);
            }

            // location
            e = channel.SelectSingleNode(@"*[local-name()='location']") as XmlElement;
            if (e != null)
            {
                Dictionary <string, object> o = new Dictionary <string, object>();
                o.Add("city", e.GetAttribute("city"));
                o.Add("region", e.GetAttribute("region"));
                o.Add("country", e.GetAttribute("country"));
                map.Add(e.LocalName, o);
            }

            // units
            e = channel.SelectSingleNode(@"*[local-name()='units']") as XmlElement;
            if (e != null)
            {
                Dictionary <string, object> o = new Dictionary <string, object>();
                o.Add("temperature", e.GetAttribute("temperature"));
                o.Add("distance", e.GetAttribute("distance"));
                o.Add("pressure", e.GetAttribute("pressure"));
                o.Add("speed", e.GetAttribute("speed"));
                map.Add(e.LocalName, o);
            }

            // atmosphere
            e = channel.SelectSingleNode(@"*[local-name()='atmosphere']") as XmlElement;
            if (e != null)
            {
                Dictionary <string, object> o = new Dictionary <string, object>();
                o.Add("humidity", e.GetAttribute("humidity"));
                o.Add("visibility", e.GetAttribute("visibility"));
                o.Add("pressure", e.GetAttribute("pressure"));
                o.Add("rising", e.GetAttribute("rising"));
                map.Add(e.LocalName, o);
            }

            // astronomy
            e = channel.SelectSingleNode(@"*[local-name()='astronomy']") as XmlElement;
            if (e != null)
            {
                Dictionary <string, object> o = new Dictionary <string, object>();
                o.Add("sunrise", e.GetAttribute("sunrise"));
                o.Add("sunset", e.GetAttribute("sunset"));
                map.Add(e.LocalName, o);
            }

            // item
            XmlElement item = channel.SelectSingleNode(@"item[1]") as XmlElement;

            if (item != null)
            {
                // condition
                e = item.SelectSingleNode(@"*[local-name()='condition']") as XmlElement;
                if (e != null)
                {
                    Dictionary <string, object> o = new Dictionary <string, object>();
                    o.Add("text", e.GetAttribute("text"));
                    o.Add("code", e.GetAttribute("code"));
                    o.Add("temp", e.GetAttribute("temp"));
                    o.Add("date", e.GetAttribute("date"));
                    map.Add(e.LocalName, o);
                }

                // forecast
                XmlNodeList forecastList = item.SelectNodes(@"*[local-name()='forecast']");
                if (forecastList.Count > 0)
                {
                    List <object> fmap = new List <object>(forecastList.Count);
                    foreach (XmlNode n in forecastList)
                    {
                        Dictionary <string, object> o = new Dictionary <string, object>();
                        XmlElement f = n as XmlElement;
                        o.Add("day", f.GetAttribute("day"));
                        o.Add("date", f.GetAttribute("date"));
                        o.Add("low", f.GetAttribute("low"));
                        o.Add("high", f.GetAttribute("high"));
                        o.Add("text", f.GetAttribute("text"));
                        o.Add("code", f.GetAttribute("code"));
                        fmap.Add(o);
                    }
                    map.Add("forecast", fmap);
                }
            }

            return(map);
        }