private void weather_DataUpdated(object sender, EventArgs e) { CurrentWeather weather = (CurrentWeather)sender; bool metric = Settings.WeatherMetric; string _temp = Math.Round(weather.Temperature).ToString(); string _tempUnit = metric ? "°C" : "°F"; string _skyCondition = WeatherFunctions.ProcessSkyConditionString(weather.SkyCondition); string _icon = WeatherFunctions.ProcessSkyConditionImage(weather.SkyCondition, DateTime.Now.TimeOfDay <weather.Sunrise || DateTime.Now.TimeOfDay> weather.Sunset); string _wind = weather.WindDirection + " " + WeatherFunctions.FormatDouble(weather.WindSpeed, 1) + (metric ? " km/h" : " mph"); string _pressure = WeatherFunctions.FormatDouble(weather.Pressure, 1) + (metric ? " hPa" : " in"); string _sunrise = RandomFunctions.FormatTime(weather.Sunrise); string _sunset = RandomFunctions.FormatTime(weather.Sunset); if (weather.ShowTimeZone) { string tzID = " " + weather.TimeZone.Id.Acronym(); _sunrise += tzID; _sunset += tzID; } Dispatcher.BeginInvoke(() => { temp.Text = _temp; tempUnit.Text = _tempUnit; skyCondition.Text = _skyCondition; //ImageSource iconSource = new BitmapImage(_iconUri); //iconSource.Freeze(); //icon.Source = iconSource; icon.Text = _icon; wind.Text = _wind; humidity.Text = weather.Humidity + " %"; cloudCover.Text = weather.CloudCover + " %"; pressure.Text = _pressure; sunrise.Text = _sunrise; sunset.Text = _sunset; grid.Opacity = weather.Timestamp < DateTime.Now.AddHours(-6) ? 0.5 : 1; if (updateTimer != null) { updateTimer.Start(); } }); }
private void ShowForecastedWeather(XmlDocument doc) { bool metric = Settings.WeatherMetric; XmlNodeList nodes = doc.GetElementsByTagName("time"); DateTime current = DateTime.Now.Date; int count = -1; for (int i = 0; i < nodes.Count && i < 16; i++) { XmlNode each = nodes[i]; DateTime _date = DateTime.Parse(each.Attributes["day"].Value); if (_date >= current) { count++; string _skyCondition = each["symbol"].Attributes["number"].Value; XmlElement temp = each["temperature"]; string _temperature = Math.Round(metric ? WeatherFunctions.KelvinToCelsius(double.Parse(temp.Attributes["max"].Value)) : WeatherFunctions.KelvinToFarenheit(double.Parse(temp.Attributes["max"].Value)) ).ToString() + "° / " + Math.Round(metric ? WeatherFunctions.KelvinToCelsius(double.Parse(temp.Attributes["min"].Value)) : WeatherFunctions.KelvinToFarenheit(double.Parse(temp.Attributes["min"].Value)) ).ToString() + "°"; string _pressure = WeatherFunctions.FormatDouble(metric ? double.Parse(each["pressure"].Attributes["value"].Value) : WeatherFunctions.MillibarToInHg(double.Parse(each["pressure"].Attributes["value"].Value) ), 1) + (metric ? " hPa" : " in"); string _humidity = each["humidity"].Attributes["value"].Value + " %"; string _cloudPercent = each["clouds"].Attributes["all"].Value + " %"; string _wind = each["windDirection"].Attributes["code"].Value + " " + WeatherFunctions.FormatDouble( metric ? WeatherFunctions.MpsToKmH(double.Parse(each["windSpeed"].Attributes["mps"].Value)) : WeatherFunctions.MpsToMph(double.Parse(each["windSpeed"].Attributes["mps"].Value) ), 1) + (metric ? " km/h" : " mph"); XmlAttribute precip = each["precipitation"].Attributes["value"]; string _precipitation; if (precip != null) { _precipitation = WeatherFunctions.FormatDouble(metric ? double.Parse(precip.Value) / 10 : WeatherFunctions.MmToIn(double.Parse(precip.Value) ), 1) + (metric ? " cm" : " in"); } else { _precipitation = metric ? "0.0 cm" : "0.0 in"; } Dispatcher.Invoke(() => { ForecastControl fc = (ForecastControl)forecastBox.Children[count]; fc.Visibility = Visibility.Visible; fc.Date = _date; fc.SkyCondition = _skyCondition; fc.Temperature = _temperature; fc.Pressure = _pressure; fc.Humidity = _humidity; fc.CloudPercent = _cloudPercent; fc.Wind = _wind; fc.Precipitation = _precipitation; }); } } Dispatcher.Invoke(() => { // Handle just in case the weather service doesn't give us // the full 16-day forecast. (Yes, this has actually happened.) for (int i = count + 1; i < 16; i++) { UIElement fc = forecastBox.Children[i]; fc.Visibility = Visibility.Collapsed; } if (forecastBox.Children[0].Visibility == Visibility.Collapsed) { noForecastText.Visibility = Visibility.Visible; } else { noForecastText.Visibility = Visibility.Collapsed; } UpdateTimeFormat(); }); }
private async void ShowCurrentWeather(XmlDocument doc) { bool metric = Settings.WeatherMetric; double currentTemp = metric ? WeatherFunctions.KelvinToCelsius(double.Parse(doc.GetElementsByTagName("temperature")[0].Attributes["value"].Value)) : WeatherFunctions.KelvinToFarenheit(double.Parse(doc.GetElementsByTagName("temperature")[0].Attributes["value"].Value)); double windSpeed = metric ? WeatherFunctions.MpsToKmH(double.Parse(doc.GetElementsByTagName("speed")[0].Attributes["value"].Value)) : WeatherFunctions.MpsToMph(double.Parse(doc.GetElementsByTagName("speed")[0].Attributes["value"].Value)); double humidity = double.Parse(doc.GetElementsByTagName("humidity")[0].Attributes["value"].Value); string precip = doc.GetElementsByTagName("weather")[0].Attributes["number"].Value; string _tempCurrent = Math.Round(currentTemp).ToString(); string _tempUnit = "°" + (metric ? "C" : "F"); string _windChillFactor = "Feels like " + Math.Round( metric ? WeatherFunctions.ApparentTemperatureCelsius(currentTemp, windSpeed, humidity) : WeatherFunctions.ApparentTemperatureFahrenheit(currentTemp, windSpeed, humidity) ).ToString() + "°"; string _precipitation = WeatherFunctions.ProcessSkyConditionString(precip); XmlNode sun = doc.GetElementsByTagName("sun")[0]; XmlNode location = doc.GetElementsByTagName("coord")[0]; double longitude = double.Parse(location.Attributes["lon"].Value); double latitude = double.Parse(location.Attributes["lat"].Value); TimeZoneInfo tz = await TimeZoneLookup.TimeZone(latitude, longitude); bool showTimeZone = false; if (tz == null) { tz = TimeZoneInfo.Local; showTimeZone = true; } TimeSpan sunrise = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse(sun.Attributes["rise"].Value), tz).TimeOfDay; TimeSpan sunset = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse(sun.Attributes["set"].Value), tz).TimeOfDay; //TimeSpan sunrise = DateTime.Parse(sun.Attributes["rise"].Value).ToLocalTime().TimeOfDay; //TimeSpan sunset = DateTime.Parse(sun.Attributes["set"].Value).ToLocalTime().TimeOfDay; string _icon = WeatherFunctions.ProcessSkyConditionImage(precip, DateTime.Now.TimeOfDay <sunrise || DateTime.Now.TimeOfDay> sunset); string _cloudCoverCurrent = doc.GetElementsByTagName("clouds")[0].Attributes["value"].Value + " %"; string _windCurrent = doc.GetElementsByTagName("direction")[0].Attributes["code"].Value + " " + WeatherFunctions.FormatDouble(windSpeed, 1) + (metric ? " km/h" : " mph"); string _barometerCurrent = WeatherFunctions.FormatDouble(metric ? double.Parse(doc.GetElementsByTagName("pressure")[0].Attributes["value"].Value) : WeatherFunctions.MillibarToInHg(double.Parse(doc.GetElementsByTagName("pressure")[0].Attributes["value"].Value) ), 1) + (metric ? " hPa" : " in"); string _sunriseCurrent = RandomFunctions.FormatTime(sunrise); string _sunsetCurrent = RandomFunctions.FormatTime(sunset); if (showTimeZone) { string tzID = " " + tz.Id.Acronym(); _sunriseCurrent += tzID; _sunsetCurrent += tzID; } _lastUpdated = DateTime.Parse(doc.GetElementsByTagName("lastupdate")[0].Attributes["value"].Value).ToLocalTime(); Dispatcher.Invoke(() => { tempCurrent.Text = _tempCurrent; tempUnit.Text = _tempUnit; windChillFactor.Text = _windChillFactor; precipitationCurrent.Text = _precipitation; //ImageSource iconSource = new BitmapImage(_icon); //iconSource.Freeze(); //icon.Source = iconSource; icon.Text = _icon; //Background = new SolidColorBrush(WeatherFunctions.ProcessWeatherNumberBackground(precip)); cloudCoverCurrent.Text = _cloudCoverCurrent; windCurrent.Text = _windCurrent; humidityCurrent.Text = humidity.ToString() + " %"; barometerCurrent.Text = _barometerCurrent; sunriseCurrent.Text = _sunriseCurrent; sunsetCurrent.Text = _sunsetCurrent; currentWeatherDisplay.Opacity = _lastUpdated < DateTime.Now.AddHours(-6) ? 0.5 : 1; }); }