/// <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.Temperature = currentPeriod.Temperature; } else { currentObservation.Temperature = WeatherHelper.GetFahrenheit(currentPeriod.Temperature); } currentObservation.WeatherDescription = currentPeriod.ShortForecast; currentObservation.AdditionalInfo = string.Format(Common.GetLocalizedText("WeatherWindSpeedFormat"), currentPeriod.WindDirection, currentPeriod.WindSpeed); return(currentObservation); }
/// <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() }); }
/// <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.Contains(period.StartTime.Date)) { forecastDays.Add(new GenericForecastDay { Date = period.StartTime.Date, TemperatureHigh = ((period.TemperatureUnit == "F") ? period.Temperature : WeatherHelper.GetFahrenheit(period.Temperature)), TemperatureLow = ((period.TemperatureUnit == "F") ? period.Temperature : WeatherHelper.GetFahrenheit(period.Temperature)), WeatherIcon = (UseWebIcon) ? period.Icon : ConvertIconToEmoji(period.Icon), WeatherDescription = period.ShortForecast }); dateList.Add(period.StartTime.Date); } else { var existing = forecastDays.FirstOrDefault(x => x.Date == period.StartTime.Date); if (existing != null) { existing.TemperatureLow = ((period.TemperatureUnit == "F") ? period.Temperature : WeatherHelper.GetFahrenheit(period.Temperature)); } // Swap if needed if (existing.TemperatureLow > existing.TemperatureHigh) { var temp = existing.TemperatureLow; existing.TemperatureLow = existing.TemperatureHigh; existing.TemperatureHigh = temp; } } } return(new GenericForecast { Days = forecastDays.Take(5).ToArray() }); }
/// <summary> /// Returns GenericWeather, populated by weatherunlocked.com /// </summary> public async Task <GenericWeather> GetGenericWeatherAsync(double lat, double lon) { // Get the Timestamp of when the next refresh will be allowed and calculate the difference between that and now DateTime timeToRefresh = _lastRefreshedTime.Add(_refreshCooldown); // Return the cached weather data if the the refresh is still cooling down if (_cachedWeather != null && DateTime.Now.CompareTo(timeToRefresh) < 0) { return(_cachedWeather); } try { InternationalForecastWeather jsonForecastData; InternationalCurrentWeather jsonCurrentData; // If the user provided a WeatherToken.config file, then read the keys from that. Otherwise, read it from Keys.cs WeatherAPIInfo weatherInfo = await WeatherHelper.ReadWeatherAPIInfo(); using (var cancel = new CancellationTokenSource(20000)) using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("User-Agent", "SmartDisplay"); string data = string.Empty; Uri currentUri = new Uri("http://api.weatherunlocked.com/api/current/" + Convert.ToString(lat, CultureInfo.InvariantCulture) + "," + Convert.ToString(lon, CultureInfo.InvariantCulture) + "?lang=" + GetLanguage() + "&app_id=" + weatherInfo.AppId + "&app_key=" + weatherInfo.AppKey); var response = await client.GetAsync(currentUri).AsTask(cancel.Token); if (response.IsSuccessStatusCode) { data = await client.GetStringAsync(currentUri); jsonCurrentData = Newtonsoft.Json.JsonConvert.DeserializeObject <InternationalCurrentWeather>(data); if (data.Contains("error")) { throw new Exception("Location not found"); } } else { return(null); } Uri forecastUri = new Uri("http://api.weatherunlocked.com/api/forecast/" + Convert.ToString(lat, CultureInfo.InvariantCulture) + "," + Convert.ToString(lon, CultureInfo.InvariantCulture) + "?lang=" + GetLanguage() + "&app_id=" + weatherInfo.AppId + "&app_key=" + weatherInfo.AppKey); response = await client.GetAsync(forecastUri).AsTask(cancel.Token); data = string.Empty; if (response.IsSuccessStatusCode) { data = await client.GetStringAsync(forecastUri); jsonForecastData = Newtonsoft.Json.JsonConvert.DeserializeObject <InternationalForecastWeather>(data); if (data.Contains("error")) { throw new Exception("Location not found"); } } else { return(null); } } var result = new InternationalWeather(jsonCurrentData, jsonForecastData).AsGenericWeather(); result.Source = Name; // Update cached weather object and refresh timestamp _cachedWeather = result; _lastRefreshedTime = DateTime.Now; return(result); } catch (Exception ex) { Debug.WriteLine(ex); return(null); } }