private static Common.Weather.WeatherPeriod ConvertToWeatherPeriod(WeatherForecast source, WeatherArea area) { var destination = new Common.Weather.WeatherPeriod(); destination.TimeFrom = source.Date.ToUniversalTime().ToLocalTime(); destination.TimeTo = destination.TimeFrom.AddDays(1); if (area != null) { destination.Location = new Common.Weather.Location(); destination.Location.Country = area.Country; destination.Location.Locality = area.AreaName; destination.Location.Latitude = area.Latitude; destination.Location.Longitude = area.Longitude; } destination.Weather = new Common.Weather.WeatherPeriodData(); destination.Weather.Condition = ConvertToWeatherCondition(source.WeatherCode); if (source.MinTemperatureCelsius.HasValue) { destination.Weather.MinTemperature = new Common.Weather.Temperature() { Celsius = source.MinTemperatureCelsius.Value }; } else if (source.MinTemperatureFahrenheit.HasValue) { destination.Weather.MinTemperature = new Common.Weather.Temperature() { Fahrenheit = source.MinTemperatureFahrenheit.Value }; } if (source.MaxTemperatureCelsius.HasValue) { destination.Weather.MaxTemperature = new Common.Weather.Temperature() { Celsius = source.MaxTemperatureCelsius.Value }; } else if (source.MaxTemperatureFahrenheit.HasValue) { destination.Weather.MaxTemperature = new Common.Weather.Temperature() { Fahrenheit = source.MaxTemperatureFahrenheit.Value }; } destination.Weather.WindDirection = source.WindDirection; destination.Weather.WindSpeed = source.WindspeedKmph; destination.Weather.Precipitation = source.Precipitation; return destination; }
private static Common.Weather.WeatherPeriod ConvertToWeatherPeriod(WeatherResponse source) { var destination = new Common.Weather.WeatherPeriod(); destination.Location = new Common.Weather.Location(); destination.Location.Latitude = source.Latitude; destination.Location.Longitude = source.Longitude; return destination; }
private static Common.Weather.WeatherPeriod ConvertToWeatherPeriod(DailyWeatherResponse source, DailyWeather day) { var destination = new Common.Weather.WeatherPeriod(); destination.TimeFrom = UnixToDateTime(day.Timestamp.Value); destination.TimeTo = destination.TimeFrom.AddDays(1); destination.Weather = ConvertToWeatherPeriodData(day); return destination; }
public override List<Common.Weather.WeatherPeriod> GetDailyForecastWeather(decimal latitude, decimal longitude) { var weatherPoints = GetMetwitWeather(latitude, longitude); var list = new List<Common.Weather.WeatherPeriod>(); if (weatherPoints.Count > 0) { Common.Weather.WeatherPeriod weatherPeriod = null; foreach (var weatherPoint in weatherPoints) { if (weatherPeriod == null || weatherPoint.Timestamp >= weatherPeriod.TimeTo) { weatherPeriod = new Common.Weather.WeatherPeriod() { TimeFrom = weatherPoint.Timestamp.Date, TimeTo = weatherPoint.Timestamp.Date.AddDays(1), Location = new Common.Weather.Location(), Weather = new WeatherPeriodData() }; ConvertWeatherLocation(weatherPeriod.Location, weatherPoint); list.Add(weatherPeriod); } if (weatherPoint.Weather.Measured.Temperature.HasValue) { if (weatherPeriod.Weather.MaxTemperature == null || weatherPeriod.Weather.MaxTemperature.Kelvin < weatherPoint.Weather.Measured.Temperature) { weatherPeriod.Weather.MaxTemperature = new Temperature() { Kelvin = weatherPoint.Weather.Measured.Temperature.Value }; weatherPeriod.Weather.MaxTemperatureTime = weatherPoint.Timestamp; } if (weatherPeriod.Weather.MinTemperature == null || weatherPeriod.Weather.MaxTemperature.Kelvin > weatherPoint.Weather.Measured.Temperature) { weatherPeriod.Weather.MinTemperature = new Temperature() { Kelvin = weatherPoint.Weather.Measured.Temperature.Value }; weatherPeriod.Weather.MinTemperatureTime = weatherPoint.Timestamp; } } } } return list; }
public List<WeatherPeriod> GetDailyForecastWeather(string woeId) { try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://weather.yahooapis.com/forecastrss?w={0}&u=c", woeId)); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { var document = new System.Xml.XPath.XPathDocument(response.GetResponseStream()); var navigator = document.CreateNavigator(); XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable); manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0"); var forecasts = navigator.Select("rss/channel/item/yweather:forecast", manager); var list = new List<Common.Weather.WeatherPeriod>(); foreach (System.Xml.XPath.XPathNavigator forecast in forecasts) { var weatherPeriod = new Common.Weather.WeatherPeriod() { Weather = new Common.Weather.WeatherPeriodData() }; var date = forecast.SelectSingleNode("@date", manager); var day = forecast.SelectSingleNode("@day", manager); var high = forecast.SelectSingleNode("@high", manager); var low = forecast.SelectSingleNode("@low", manager); var weatherCode = forecast.SelectSingleNode("@code", manager); weatherPeriod.Weather.MaxTemperature = new Common.Weather.Temperature() { Celsius = decimal.Parse(high.Value, System.Globalization.NumberFormatInfo.InvariantInfo) }; weatherPeriod.Weather.MinTemperature = new Common.Weather.Temperature() { Celsius = decimal.Parse(low.Value, System.Globalization.NumberFormatInfo.InvariantInfo) }; weatherPeriod.Weather.Condition = GetWeatherCondition(weatherCode.Value); //TODO: Tag bestimmen: DateTime dateTime; if (DateTime.TryParse(date.Value, System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.AssumeLocal, out dateTime)) { weatherPeriod.TimeFrom = dateTime; weatherPeriod.TimeTo = dateTime.AddDays(1); } //weatherInformation.DayOfWeek = day.Value; list.Add(weatherPeriod); } return list; } } catch (Exception ex) { return null; } }