示例#1
0
        private async void UpdateValues(XmlDocument doc)
        {
            bool metric = Settings.WeatherMetric;

            _temp = metric
                                ? WeatherFunctions.KelvinToCelsius(double.Parse(doc.GetElementsByTagName("temperature")[0].Attributes["value"].Value))
                                : WeatherFunctions.KelvinToFarenheit(double.Parse(doc.GetElementsByTagName("temperature")[0].Attributes["value"].Value));
            _windSpeed = metric
                                ? WeatherFunctions.MpsToKmH(double.Parse(doc.GetElementsByTagName("speed")[0].Attributes["value"].Value))
                                : WeatherFunctions.MpsToMph(double.Parse(doc.GetElementsByTagName("speed")[0].Attributes["value"].Value));
            _skyCondition  = doc.GetElementsByTagName("weather")[0].Attributes["number"].Value;
            _cloudCover    = doc.GetElementsByTagName("clouds")[0].Attributes["value"].Value;
            _windDirection = doc.GetElementsByTagName("direction")[0].Attributes["code"].Value;
            _humidity      = double.Parse(doc.GetElementsByTagName("humidity")[0].Attributes["value"].Value);
            _pressure      = metric
                                ? double.Parse(doc.GetElementsByTagName("pressure")[0].Attributes["value"].Value)
                                : WeatherFunctions.MillibarToInHg(double.Parse(doc.GetElementsByTagName("pressure")[0].Attributes["value"].Value));

            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);

            //_sunrise = DateTime.Parse(sun.Attributes["rise"].Value).ToLocalTime().TimeOfDay;
            //_sunset = DateTime.Parse(sun.Attributes["set"].Value).ToLocalTime().TimeOfDay;

            TimeZoneInfo tz = await TimeZoneLookup.TimeZone(latitude, longitude);

            if (tz == null)
            {
                tz            = TimeZoneInfo.Local;
                _showTimeZone = true;
            }
            else
            {
                _showTimeZone = false;
            }

            _timeZone = tz;

            _sunrise = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse(sun.Attributes["rise"].Value), tz).TimeOfDay;
            _sunset  = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse(sun.Attributes["set"].Value), tz).TimeOfDay;

            _timestamp = DateTime.Parse(doc.GetElementsByTagName("lastupdate")[0].Attributes["value"].Value).ToLocalTime();

            RaiseDataUpdatedEvent();
        }
示例#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;
            });
        }