/// <summary> /// Converts NWS data into GenericCurrentObservation object /// </summary> /// <returns></returns> public GenericCurrentObservation GetGenericCurrentObservation() { if (Properties == null || Properties.Periods == null || Properties.Periods.Length < 1) { return(null); } var currentObservation = new GenericCurrentObservation(); var currentPeriod = Properties.Periods.FirstOrDefault(); currentObservation.Icon = (UseWebIcon) ? currentPeriod.Icon : ConvertIconToEmoji(currentPeriod.Icon); if (currentPeriod.TemperatureUnit == "F") { currentObservation.TemperatureFahrenheit = currentPeriod.Temperature; currentObservation.TemperatureCelsius = WeatherHelper.GetCelsius(currentPeriod.Temperature); } else { currentObservation.TemperatureCelsius = currentPeriod.Temperature; currentObservation.TemperatureFahrenheit = WeatherHelper.GetFahrenheit(currentPeriod.Temperature); } currentObservation.WeatherDescription = currentPeriod.ShortForecast; currentObservation.AdditionalInfo = string.Format(Common.GetLocalizedText("WeatherWindSpeedFormat"), currentPeriod.WindDirection, currentPeriod.WindSpeed); return(currentObservation); }
public GenericWeather AsGenericWeather() { var weather = new GenericWeather { CurrentObservation = new GenericCurrentObservation(), Forecast = new GenericForecast() }; var forecastObjects = ParseForecastDayObjects(); var currentForecast = forecastObjects.FirstOrDefault(x => x.StartValidTime.Date == DateTime.Now.Date && DateTime.Now >= x.StartValidTime); if (currentForecast != null) { weather.CurrentObservation.Icon = GetIconFromUSWeather(currentForecast.IconLink); weather.CurrentObservation.TemperatureFahrenheit = (float)currentForecast.Temperature; weather.CurrentObservation.TemperatureCelsius = WeatherHelper.GetCelsius((int)weather.CurrentObservation.TemperatureFahrenheit); weather.CurrentObservation.WeatherDescription = currentForecast.Weather; } // forecast.weather.gov gives 15 weather reports, including 2 for each day (one during the day, and one at night), // possibly starting with either "This Afternoon" or "Tonight" depending on the time of day. // Only grab the first forecast for each date, up to the number of days we specify var forecastDays = new List <GenericForecastDay>(); int numDays = 5; foreach (var forecastObject in forecastObjects) { if (forecastDays.Count == numDays) { break; } var date = forecastObject.StartValidTime.Date; // If we haven't already gotten a forecast for the day and it isn't today, add it to the list if (forecastDays.FirstOrDefault(x => x.DayOfWeek == date.ToString("dddd")) == null && date != DateTime.Now.Date) { forecastDays.Add(GetGenericForecastDay( date.ToString("dddd"), ((int)forecastObject.Temperature).ToString(), forecastObject.IconLink, forecastObject.Weather)); } } weather.Forecast.Days = forecastDays.ToArray(); return(weather); }
public GenericForecastDay GetGenericForecastDay(string dayName, string tempF, string icon, string desc) { GenericForecastDay day = new GenericForecastDay { DayOfWeek = dayName, TemperatureFahrenheit = tempF + "°F", WeatherIcon = GetIconFromUSWeather(icon), WeatherDescription = desc }; if (int.TryParse(tempF, out var temp)) { day.TemperatureCelsius = WeatherHelper.GetCelsius(temp).ToString() + "°C"; } return(day); }
/// <summary> /// Converts NWS data to GenericForcast object /// </summary> /// <returns></returns> public GenericForecast GetGenericForecast() { if (Properties == null || Properties.Periods == null || Properties.Periods.Length < 1) { return(null); } var forecastDays = new List <GenericForecastDay>(); var dateList = new List <DateTime>(); foreach (var period in Properties.Periods) { if (dateList.Count == 5) { break; } if (!dateList.Contains(period.StartTime.Date)) { forecastDays.Add(new GenericForecastDay { DayOfWeek = period.StartTime.ToString("dddd"), TemperatureFahrenheit = ((period.TemperatureUnit == "F") ? period.Temperature : WeatherHelper.GetFahrenheit(period.Temperature)).ToString() + "°F", TemperatureCelsius = ((period.TemperatureUnit == "C") ? period.Temperature : WeatherHelper.GetCelsius(period.Temperature)).ToString() + "°C", WeatherIcon = (UseWebIcon) ? period.Icon : ConvertIconToEmoji(period.Icon), WeatherDescription = period.ShortForecast }); dateList.Add(period.StartTime.Date); } } return(new GenericForecast { Days = forecastDays.ToArray() }); }