예제 #1
0
파일: WWO.cs 프로젝트: cincoutprabu/YtoX
        public static WeatherDetails GetWeather(double latitude, double longitude)
        {
            try
            {
                string apiKey = "c9f0be46ae133902122810";
                string url    = string.Format("http://free.worldweatheronline.com/feed/weather.ashx?q={0},{1}&format=xml&num_of_days=1&key={2}", latitude, longitude, apiKey);

                string      xml      = WebHelper.ReadUrl(url);
                XmlDocument document = new XmlDocument();
                document.LoadXml(xml);

                WeatherDetails weatherDetails = new WeatherDetails();

                //read current weather
                XmlNode currentNode = document.SelectSingleNode("data/current_condition");
                if (currentNode != null)
                {
                    WeatherInfo weather = new WeatherInfo();
                    weather.ObservedOn        = DateTime.Now;
                    weather.WeatherDate       = DateTime.Now.Date;
                    weather.Temperature_C_Min = Convert.ToSingle(currentNode.SelectSingleNode("temp_C").InnerText);
                    weather.Temperature_C_Max = Convert.ToSingle(currentNode.SelectSingleNode("temp_C").InnerText);
                    weather.Temperature_F_Min = Convert.ToSingle(currentNode.SelectSingleNode("temp_F").InnerText);
                    weather.Temperature_F_Max = Convert.ToSingle(currentNode.SelectSingleNode("temp_F").InnerText);
                    weather.Description       = currentNode.SelectSingleNode("weatherDesc").InnerText;
                    weather.WindSpeed_kmph    = Convert.ToSingle(currentNode.SelectSingleNode("windspeedKmph").InnerText);
                    weather.WindDirection     = currentNode.SelectSingleNode("winddir16Point").InnerText;
                    weather.Humidity          = currentNode.SelectSingleNode("humidity").InnerText;

                    weatherDetails.CurrentWeather = weather;
                }

                //read future weathers
                XmlNodeList futureNodes = document.SelectNodes("data/weather");
                foreach (XmlNode node in futureNodes)
                {
                    WeatherInfo weather = new WeatherInfo();
                    weather.ObservedOn        = DateTime.Now;
                    weather.WeatherDate       = DateTime.Now.AddDays(1).Date;
                    weather.Temperature_C_Min = Convert.ToSingle(node.SelectSingleNode("tempMinC").InnerText);
                    weather.Temperature_C_Max = Convert.ToSingle(node.SelectSingleNode("tempMaxC").InnerText);
                    weather.Temperature_F_Min = Convert.ToSingle(node.SelectSingleNode("tempMinF").InnerText);
                    weather.Temperature_F_Max = Convert.ToSingle(node.SelectSingleNode("tempMaxF").InnerText);
                    weather.Description       = node.SelectSingleNode("weatherDesc").InnerText;
                    weather.WindSpeed_kmph    = Convert.ToSingle(node.SelectSingleNode("windspeedKmph").InnerText);
                    weather.WindDirection     = node.SelectSingleNode("winddirection").InnerText;
                    weather.Humidity          = string.Empty;

                    weatherDetails.FutureWeathers.Add(weather);
                }

                return(weatherDetails);
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }

            return(null);
        }
예제 #2
0
        public static WeatherDetails GetWeather(double latitude, double longitude)
        {
            try
            {
                string url = string.Format("http://api.wunderground.com/api/{0}/conditions/q/{1},{2}.json", ApiKey, latitude, longitude);

                string       jsonString  = WebHelper.ReadUrl(url);
                BsonDocument json        = BsonDocument.Parse(jsonString);
                BsonDocument observation = (BsonDocument)json["current_observation"];

                //build 'WeatherDetails' object
                WeatherDetails weatherDetails = new WeatherDetails();
                weatherDetails.CurrentWeather.ObservedOn        = DateTime.Now;
                weatherDetails.CurrentWeather.WeatherDate       = DateTime.Now.Date;
                weatherDetails.CurrentWeather.Temperature_C_Min = float.Parse(observation["temp_c"].ToString());
                weatherDetails.CurrentWeather.Temperature_C_Max = weatherDetails.CurrentWeather.Temperature_C_Min;
                weatherDetails.CurrentWeather.Temperature_F_Min = float.Parse(observation["temp_f"].ToString());
                weatherDetails.CurrentWeather.Temperature_F_Max = weatherDetails.CurrentWeather.Temperature_F_Min;
                weatherDetails.CurrentWeather.Description       = observation["weather"].ToString();
                weatherDetails.CurrentWeather.WindSpeed_kmph    = float.Parse(observation["wind_kph"].ToString());
                weatherDetails.CurrentWeather.WindDirection     = observation["wind_dir"].ToString();
                weatherDetails.CurrentWeather.Humidity          = observation["relative_humidity"].ToString();
                return(weatherDetails);
                //return weatherDetails.CurrentWeather.Temperature_C_Min;
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }

            return(null);
            //return -1;
        }
예제 #3
0
        public async static Task ReadWeather()
        {
            WeatherRead = false;

            //ignore if weather is read very recently
            if (CurrentWeather != null && DateTime.Now.Subtract(CurrentWeather.FetchedOn).TotalMinutes < Constants.WEATHER_READ_INTERVAL)
            {
                return;
            }

            try
            {
                if (LocationHelper.CurrentPosition == null)
                {
                    await LocationHelper.ReadLocation(false);
                }

                if (LocationHelper.CurrentPosition != null)
                {
                    WeatherEntry entry = new WeatherEntry();
                    entry.Latitude  = LocationHelper.CurrentPosition.Coordinate.Latitude;
                    entry.Longitude = LocationHelper.CurrentPosition.Coordinate.Longitude;

                    WeatherDetails weatherDetails = Wunderground.GetWeather(entry.Latitude, entry.Longitude);
                    if (weatherDetails != null)
                    {
                        entry.Temperature = Math.Round(weatherDetails.CurrentWeather.Temperature_C_Min, 2);
                    }

                    entry.Forecast  = Wunderground.GetForecast(entry.Latitude, entry.Longitude);
                    entry.FetchedOn = DateTime.Now;
                    CurrentWeather  = entry;
                    WeatherHistoryDAC.Insert(CurrentWeather);

                    WeatherRead = true;
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }
        }