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();
                }
            });
        }
Esempio n. 2
0
        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;
            });
        }