示例#1
0
        public WeatherData GetForecastWeatherData(string locId, bool force)
        {
            var result = new WeatherData();

            if (String.IsNullOrEmpty(locId))
            {
                return(null);
            }
            try
            {
                if (!LoadWeatherData(locId, force, true))
                {
                    return(null);
                }
                var data = new XmlDocument();
                data.Load(_cacheDir + "/" + locId + ".forecast.xml");
                var ns = new XmlNamespaceManager(data.NameTable);
                ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");


                XmlNode node = data.SelectSingleNode("/rss/channel/yweather:units", ns);
                result.TempUnit = node.Attributes["temperature"].InnerText;

                node = data.SelectSingleNode("/rss/channel/yweather:location", ns);
                result.LocationName = node.Attributes["city"].InnerText;

                node = data.SelectSingleNode("/rss/channel/item/yweather:condition", ns);

                result.Today = new WeatherCurrentDetail
                {
                    Temperature = node.Attributes["temp"].InnerText,
                    Icon        = node.Attributes["code"].InnerText
                };


                result.Forecast.Clear();

                XmlNodeList nodes = data.SelectNodes("/rss/channel/item/yweather:forecast", ns);
                var         diff  = 0;
                foreach (XmlNode inode in nodes)
                {
                    var temp = new WeatherForecastDetail
                    {
                        DayDiff   = diff,
                        DayName   = inode.Attributes["day"].InnerText,
                        DayDate   = inode.Attributes["date"].InnerText,
                        DayIcon   = inode.Attributes["code"].InnerText,
                        NightIcon = inode.Attributes["code"].InnerText,
                        MaxTemp   = inode.Attributes["high"].InnerText,
                        LowTemp   = inode.Attributes["low"].InnerText
                    };
                    result.Forecast.Add(temp);
                    diff++;
                }
                return(result);
            }
            catch (XmlException) { }
            catch (XPathException) { }
            catch (IOException) { }
            catch (NullReferenceException) { }
            return(null);
        }
示例#2
0
/*
        public WeatherData GetForecastWeatherDataOLD(string locId, bool force)
        {
            var result = new WeatherData();
            if (String.IsNullOrEmpty(locId))
                return null;
            try
            {
                 if (!LoadWeatherData(locId, force, true))
                    return null;
                var data = new XmlDocument();
                data.Load(_cacheDir + "/" + locId + ".forecast.xml");
                var ns = new XmlNamespaceManager(data.NameTable);
                ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");


                XmlNode node = data.SelectSingleNode("/rss/channel/yweather:units", ns);
                result.TempUnit = node.Attributes["temperature"].InnerText;

                node = data.SelectSingleNode("/rss/channel/yweather:location", ns);
                result.LocationName = node.Attributes["city"].InnerText;

                node = data.SelectSingleNode("/rss/channel/item/yweather:condition", ns);

                result.Today = new WeatherCurrentDetail
                {
                    Temperature = node.Attributes["temp"].InnerText,
                    Icon = node.Attributes["code"].InnerText
                };


                result.Forecast.Clear();

                XmlNodeList nodes = data.SelectNodes("/rss/channel/item/yweather:forecast", ns);
                var diff = 0;
                foreach (XmlNode inode in nodes)
                {

                    var temp = new WeatherForecastDetail
                    {
                        DayDiff = diff,
                        DayName = inode.Attributes["day"].InnerText,
                        DayDate = inode.Attributes["date"].InnerText,
                        DayIcon = inode.Attributes["code"].InnerText,
                        NightIcon = inode.Attributes["code"].InnerText,
                        MaxTemp = inode.Attributes["high"].InnerText,
                        LowTemp = inode.Attributes["low"].InnerText
                    };
                    result.Forecast.Add(temp);
                    diff++;
                }
                return result;
            }
            catch (XmlException) { }
            catch (XPathException) { }
            catch (IOException) { }
            catch (NullReferenceException) { }
            return null;
        }
 */
        public WeatherData GetForecastWeatherDataNew(string locId, bool force)
        {

            bool night = false;
            var result = new WeatherData();
            Logger.Instance().Trace("Weather:", "GetForecastWeatherDataNew");
            Logger.Instance().Trace("Weather:", "Weather API Equals: " + _weatherAPI);
            string str = "";

            string newLocID = locId;
            string invalid = @";/:\";
            foreach (char c in invalid)
            {
                newLocID = newLocID.Replace(c.ToString(), "");
            }
            string path = _cacheDir + @"\" + newLocID + ".forecast.json";
            string path2 = _cacheDir + @"\" + newLocID + ".json";

            if (String.IsNullOrEmpty(locId))
            {
                Logger.Instance().Trace("Weather:", "GetForecastWeatherDataNew:  locID Null or Empty");
                return null;
            }

           // GetWeatherDataNew(locId, false);    //loads conditions --> probably need to move this

            if (!LoadWeatherData(locId, force, true))
            {  
                Logger.Instance().Trace("Weather:", "!LoadWeatherData ForecastWeatherDataNull returning null.");
                return null;
            }
            //First check Conditions as need this to get name amongst other things.

            try
            {
                using (StreamReader client = new StreamReader(path2))
                {
                    try
                    {
                        str = client.ReadToEnd();//DataUrl + _weatherAPI + @"/conditions" + locId + ".json");
                        Logger.Instance().Trace("Weather:", "GetWeatherForecast COnditions Data: Trying DATA FILE " + path2);
                        //Logger.Instance().Trace("Weather:", "GetWeatherData: Data " + str);
                    }
                    catch (WebException ex)
                    {
                        Logger.Instance().Trace("Weather:", "Underground Error" + ex);
                        return null;
                    }
                }
            
            }
            catch (Exception ex)
            {
                    Logger.Instance().Trace("Weather:", "Underground Error" + ex);
                    return null;
            }


            try
            {
                    string json = str;
                    
                    if (str.Contains("keynotfound"))
                    {
                        Logger.Instance().Log("Weather:", "ERORR in Weather API Key Not Found");
                        return null;
                    }


                    var deserializer = new JavaScriptSerializer();
                    var server = deserializer.Deserialize<WeatherAPIWUndergroundConditions.Rootobject>(json);
                    result.LocationName = server.current_observation.display_location.full;
                    result.Forecast.Clear();


                    try
                    {
                        Uri uri = new Uri(server.current_observation.icon_url);
                        Logger.Instance().Trace("Weather**", "Getting icon url to find night or not:" + uri.AbsolutePath);
                        string filename = Path.GetFileName(uri.AbsolutePath);
                        Logger.Instance().Trace("Weather**", "IS File True: icon url to find night or not:" + filename);
                        if (filename.StartsWith("nt"))
                        {
                            night = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance().Trace("Weather*", "Exception Caught in Icon Night or Day test" + ex);
                    }

                    if (_unit == "c")
                    {
                        result.TempUnit = "celsius";
                        result.Today = new WeatherCurrentDetail
                            {
                                Temperature = server.current_observation.temp_c.ToString(),
                                Icon = night == true ? "nt_" + server.current_observation.icon : server.current_observation.icon
                            };
                    }

                    else if (_unit == "f")
                    {
                        result.TempUnit = @"F";
                        result.Today = new WeatherCurrentDetail
                            {
                                Temperature = server.current_observation.temp_f.ToString(),
                                Icon = night == true ? "nt_" + server.current_observation.icon : server.current_observation.icon
                            };
                    }

                }
                catch (Exception ex)
                {
                    Logger.Instance().Trace("Weather", "Underground Error" + ex);
                    return null;
                }
            
            // Now downloading Forecast stuff...


            try
            {
                using (StreamReader client = new StreamReader(path))
                {
                    try
                    {
                        str = client.ReadToEnd();
                        Logger.Instance().Trace("Weather:", "GetForecastWeather Data Trying Data FILE: " + path);
                        //Logger.Instance().Trace("Weather:", "Result is :" + str);
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance().Trace("Weather:", "Underground Error" + ex);
                        return null;
                    }
                }
            } 
            catch (Exception ex)
            {
                Logger.Instance().Trace("Weather", "Underground Error" + ex);
            }


            try
            {
                string json = str;


                if (str.Contains("keynotfound"))
                {
                    Logger.Instance().Log("Weather:", "ERORR in Weather API Key Not Found");
                    return null;
                }

                var deserializer = new JavaScriptSerializer();
                var server = deserializer.Deserialize<WeatherAPIWUnderground10day.Rootobject>(json);

                result.Forecast.Clear();

                var diff = 0;

                if (server != null)
                {
                    foreach (var element in server.forecast.simpleforecast.forecastday)
                    {

                        var temp = new WeatherForecastDetail
                        {
                            DayDiff = diff,
                            DayName = element.date.weekday,
                            DayDate = element.date.pretty,
                            DayIcon = night == true ? "nt_" + element.icon : element.icon,
                            NightIcon = "nt_"+element.icon,
                            MaxTemp = (_unit == "c") ? element.high.celsius : element.high.fahrenheit,
                            LowTemp = (_unit == "c") ? element.low.celsius : element.low.fahrenheit,

                        };
                        result.Forecast.Add(temp);
                        diff++;
                    }
                }
                return result;


            }
            catch (Exception ex)
            {
                Logger.Instance().Trace("Weather", "Underground Error" + ex);
                return null;
            }
        }
示例#3
0
        public WeatherData GetForecastWeatherDataNew(string locId, bool force)
        {
            bool night  = false;
            var  result = new WeatherData();

            Logger.Instance().Trace("Weather:", "GetForecastWeatherDataNew");
            Logger.Instance().Trace("Weather:", "Weather API Equals: " + _weatherAPI);
            string str = "";

            string newLocID = locId;
            string invalid  = @";/:\";

            foreach (char c in invalid)
            {
                newLocID = newLocID.Replace(c.ToString(), "");
            }
            string path  = _cacheDir + @"\" + newLocID + ".json";
            string path2 = _cacheDir + @"\" + newLocID + ".json";

            if (String.IsNullOrEmpty(locId))
            {
                Logger.Instance().Trace("Weather:", "GetForecastWeatherDataNew:  locID Null or Empty");
                return(null);
            }

            // GetWeatherDataNew(locId, false);    //loads conditions --> probably need to move this

            if (!LoadWeatherData(locId, force, true))
            {
                Logger.Instance().Trace("Weather:", "!LoadWeatherData ForecastWeatherDataNull returning null.");
                return(null);
            }
            //First check Conditions as need this to get name amongst other things.

            try
            {
                using (StreamReader client = new StreamReader(path2))
                {
                    try
                    {
                        str = client.ReadToEnd();//DataUrl + _weatherAPI + @"/conditions" + locId + ".json");
                        Logger.Instance().Trace("Weather:", "GetWeatherForecast COnditions Data: Trying DATA FILE " + path2);
                        //Logger.Instance().Trace("Weather:", "GetWeatherData: Data " + str);
                    }
                    catch (WebException ex)
                    {
                        Logger.Instance().Trace("Weather:", " Error" + ex);
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance().Trace("Weather:", " Error" + ex);
                return(null);
            }


            try
            {
                string json = str;

                if (str.Contains("keynotfound"))
                {
                    Logger.Instance().Log("Weather:", "ERORR in Weather API Key Not Found");
                    return(null);
                }


                var deserializer = new JavaScriptSerializer();
                var server       = deserializer.Deserialize <OpenWeatherAPI.Root>(json);
                result.LocationName = server.timezone.ToString();
                result.Forecast.Clear();
                var currentWeather = server.current.weather.First();

                try
                {
                    Logger.Instance().Trace("Weather**", "Getting icon url to find night or not:" + currentWeather.icon);
                    Logger.Instance().Trace("Weather**", "IS File True: icon url to find night or not:" + currentWeather.icon);
                    if (currentWeather.icon.EndsWith("n"))
                    {
                        night = true;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance().Trace("Weather*", "Exception Caught in Icon Night or Day test" + ex);
                }

                result.TempUnit = "°";
                result.Today    = new WeatherCurrentDetail
                {
                    Temperature = server.current.temp.ToString(),
                    Icon        = convertIcons(currentWeather.icon)
                };
            }
            catch (Exception ex)
            {
                Logger.Instance().Trace("Weather", " Error" + ex);
                return(null);
            }

            // Now downloading Forecast stuff...


            try
            {
                using (StreamReader client = new StreamReader(path))
                {
                    try
                    {
                        str = client.ReadToEnd();
                        Logger.Instance().Trace("Weather:", "GetForecastWeather Data Trying Data FILE: " + path);
                        //Logger.Instance().Trace("Weather:", "Result is :" + str);
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance().Trace("Weather:", "Underground Error" + ex);
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance().Trace("Weather", "Underground Error" + ex);
            }


            try
            {
                string json = str;


                if (str.Contains("keynotfound"))
                {
                    Logger.Instance().Log("Weather:", "ERORR in Weather API Key Not Found");
                    return(null);
                }

                var deserializer = new JavaScriptSerializer();
                var server       = deserializer.Deserialize <OpenWeatherAPI.Root>(json);

                result.Forecast.Clear();

                var diff = 0;

                if (server != null)
                {
                    foreach (var element in server.daily)
                    {
                        var currentdatetime = UnixTimeStampToDateTime(element.dt);
                        var daysWeather     = element.weather.First();

                        var temp = new WeatherForecastDetail
                        {
                            DayDiff   = diff,
                            DayName   = currentdatetime.DayOfWeek.ToString(),
                            DayDate   = currentdatetime.Date.ToString(),
                            DayIcon   = convertIcons(daysWeather.icon),
                            NightIcon = convertIcons(daysWeather.icon),
                            MaxTemp   = element.temp.max.ToString(),
                            LowTemp   = element.temp.min.ToString(),
                        };
                        result.Forecast.Add(temp);
                        diff++;
                    }
                }
                return(result);
            }
            catch (Exception ex)
            {
                Logger.Instance().Trace("Weather", "Underground Error" + ex);
                return(null);
            }
        }
示例#4
0
        public WeatherData GetForecastWeatherData(string locId, bool force)
        {
            var result = new WeatherData();
            if (String.IsNullOrEmpty(locId))
                return null;
            try
            {
                if (!LoadWeatherData(locId, force, true))
                    return null;
                var data = new XmlDocument();
                data.Load(_cacheDir + "/" + locId + ".forecast.xml");
                var ns = new XmlNamespaceManager(data.NameTable);
                ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");


                XmlNode node = data.SelectSingleNode("/rss/channel/yweather:units", ns);
                result.TempUnit = node.Attributes["temperature"].InnerText;

                node = data.SelectSingleNode("/rss/channel/yweather:location", ns);
                result.LocationName = node.Attributes["city"].InnerText;

                node = data.SelectSingleNode("/rss/channel/item/yweather:condition", ns);

                result.Today = new WeatherCurrentDetail
                {
                    Temperature = node.Attributes["temp"].InnerText,
                    Icon = node.Attributes["code"].InnerText
                };


                result.Forecast.Clear();

                XmlNodeList nodes = data.SelectNodes("/rss/channel/item/yweather:forecast", ns);
                var diff = 0;
                foreach (XmlNode inode in nodes)
                {

                    var temp = new WeatherForecastDetail
                    {
                        DayDiff = diff,
                        DayName = inode.Attributes["day"].InnerText,
                        DayDate = inode.Attributes["date"].InnerText,
                        DayIcon = inode.Attributes["code"].InnerText,
                        NightIcon = inode.Attributes["code"].InnerText,
                        MaxTemp = inode.Attributes["high"].InnerText,
                        LowTemp = inode.Attributes["low"].InnerText
                    };
                    result.Forecast.Add(temp);
                    diff++;
                }
                return result;
            }
            catch (XmlException) { }
            catch (XPathException) { }
            catch (IOException) { }
            catch (NullReferenceException) { }
            return null;
        }