Ejemplo n.º 1
0
        public async Task <ViewModel.Weather> GetWeather(Configuration.Configuration config)
        {
            ViewModel.Weather weather = new ViewModel.Weather();
            WeatherNet.ClientSettings.ApiUrl = config.WeatherAPIUrl;
            WeatherNet.ClientSettings.ApiKey = config.WeatherAPIKey;

            // get current weather info
            var currentWeather = await WeatherNet.Clients.CurrentWeather.GetByCityNameAsync(
                config.WeatherCity, config.WeatherCountry, config.WeatherLanguage, config.WeatherUnits);

            if (!currentWeather.Success)
            {
                return(null);
            }

            weather.BeaufortWindScale = WIWindBeaufort.Beaufort[WIWindBeaufort.KMHToBeaufort(currentWeather.Item.WindSpeed)];
            var now = DateTimeFactory.Instance.Now;

            weather.SunState     = WeatherIcons.Sunrise;
            weather.SunStateTime = currentWeather.Item.Sunrise.ToString("HH:mm");
            if (currentWeather.Item.Sunrise < now && currentWeather.Item.Sunset > now)
            {
                weather.SunState     = WeatherIcons.Sunset;
                weather.SunStateTime = currentWeather.Item.Sunset.ToString("HH:mm");
            }

            weather.Temperature = string.Format("{0:0.0}°", currentWeather.Item.Temp);
            weather.WeatherIcon = WeatherIcons.TryGetIcon(currentWeather.Item.Icon);

            var forecastDaily = await WeatherNet.Clients.FiveDaysForecast.GetByCityNameDailyAsync(config.WeatherCity, config.WeatherCountry, config.WeatherLanguage, config.WeatherUnits);

            var forecastDetail = await WeatherNet.Clients.FiveDaysForecast.GetByCityNameAsync(config.WeatherCity, config.WeatherCountry, config.WeatherLanguage, config.WeatherUnits);


            if (forecastDaily.Success)
            {
                foreach (var item in forecastDaily.Items)
                {
                    var forecast = new ViewModel.WeatherForecast();
                    forecast.Icon     = WeatherIcons.TryGetIcon(item.Icon);
                    forecast.Day      = item.Date.ToString("ddd.");
                    forecast.MaxTemp  = string.Format("{0:0.0}°", item.TempMax);
                    forecast.MinTemp  = string.Format("{0:0.0}°", item.TempMin);
                    forecast.Temp     = string.Format("{0:0.0}°", item.Temp);
                    forecast.DMaxTemp = item.TempMax;
                    forecast.DMinTemp = item.TempMin;
                    forecast.DateTime = item.Date.Date;
                    weather.Forecasts.Add(forecast);

                    if (forecastDaily.Success)
                    {
                        var detailForecast = new ViewModel.WeatherForecast();
                        detailForecast.Update(forecast);

                        if (detailForecast.DateTime.Day == now.Day)
                        {
                            detailForecast.Day = "Heute";
                        }
                        else
                        {
                            detailForecast.Day = forecast.DateTime.ToString("dddd");
                        }

                        var dtBegin = detailForecast.DateTime.Date;
                        var dtEnd   = dtBegin.AddDays(1);
                        foreach (var itemdetail in forecastDetail.Items.Where(A => A.Date >= dtBegin && A.Date < dtEnd))
                        {
                            ViewModel.WeatherForecastDetail detail = new ViewModel.WeatherForecastDetail();
                            detail.DateTime = itemdetail.Date;
                            detail.Time     = itemdetail.Date.ToString("HH:mm");
                            detail.Icon     = WeatherIcons.TryGetIcon(itemdetail.Icon);
                            detail.Temp     = string.Format("{0:0.0}°", itemdetail.Temp);
                            // TODO: MORE WEATHER INFO??

                            detailForecast.Details.Add(detail);
                        }

                        weather.ForecastDetails.Add(detailForecast);
                    }
                }

                if (weather.Forecasts.Count > 0)
                {
                    weather.Forecasts.RemoveAt(0);
                }
            }

            return(weather);
        }
Ejemplo n.º 2
0
        public async Task<ViewModel.Weather> GetWeather(Configuration.Configuration config)
        {
            ViewModel.Weather weather = new ViewModel.Weather();
            WeatherNet.ClientSettings.ApiUrl = config.WeatherAPIUrl;
            WeatherNet.ClientSettings.ApiKey = config.WeatherAPIKey;

            // get current weather info
            var currentWeather = await WeatherNet.Clients.CurrentWeather.GetByCityNameAsync(
                config.WeatherCity, config.WeatherCountry, config.WeatherLanguage, config.WeatherUnits);
            
            if (!currentWeather.Success) return null;

            weather.BeaufortWindScale = WIWindBeaufort.Beaufort[WIWindBeaufort.KMHToBeaufort(currentWeather.Item.WindSpeed)];
            var now = DateTimeFactory.Instance.Now;

            weather.SunState = WIWeather.Sunrise;
            weather.SunStateTime = currentWeather.Item.Sunrise.ToString("HH:mm");
            if (currentWeather.Item.Sunrise < now && currentWeather.Item.Sunset > now)
            {
                weather.SunState = WIWeather.Sunset;
                weather.SunStateTime = currentWeather.Item.Sunset.ToString("HH:mm");
            }

            weather.Temperature = string.Format("{0:0.0}°", currentWeather.Item.Temp);
            weather.WeatherIcon = WIWeather.IconTable[currentWeather.Item.Icon];

            var forecastDaily = await WeatherNet.Clients.FiveDaysForecast.GetByCityNameDailyAsync(config.WeatherCity, config.WeatherCountry, config.WeatherLanguage, config.WeatherUnits);
            var forecastDetail = await WeatherNet.Clients.FiveDaysForecast.GetByCityNameAsync(config.WeatherCity, config.WeatherCountry, config.WeatherLanguage, config.WeatherUnits);


            if (forecastDaily.Success)
            {
                foreach (var item in forecastDaily.Items)
                {
                    var forecast = new ViewModel.WeatherForecast();
                    forecast.Icon = WIWeather.IconTable[item.Icon];
                    forecast.Day = item.Date.ToString("ddd.");
                    forecast.MaxTemp = string.Format("{0:0.0}°", item.TempMax);
                    forecast.MinTemp = string.Format("{0:0.0}°", item.TempMin);
                    forecast.Temp = string.Format("{0:0.0}°", item.Temp);
                    forecast.DMaxTemp = item.TempMax;
                    forecast.DMinTemp = item.TempMin;
                    forecast.DateTime = item.Date.Date;
                    weather.Forecasts.Add(forecast);

                    if (forecastDaily.Success)
                    {
                        var detailForecast = new ViewModel.WeatherForecast();
                        detailForecast.Update(forecast);

                        if (detailForecast.DateTime.Day == now.Day)
                            detailForecast.Day = "Heute";
                        else
                            detailForecast.Day = forecast.DateTime.ToString("dddd");

                        var dtBegin = detailForecast.DateTime.Date;
                        var dtEnd = dtBegin.AddDays(1);
                        foreach (var itemdetail in forecastDetail.Items.Where(A => A.Date >= dtBegin && A.Date < dtEnd))
                        {
                            ViewModel.WeatherForecastDetail detail = new ViewModel.WeatherForecastDetail();
                            detail.DateTime = itemdetail.Date;
                            detail.Time = itemdetail.Date.ToString("HH:mm");
                            detail.Icon = WIWeather.IconTable[itemdetail.Icon];
                            detail.Temp = string.Format("{0:0.0}°", itemdetail.Temp);
                            // TODO: MORE WEATHER INFO??

                            detailForecast.Details.Add(detail);
                        }

                        weather.ForecastDetails.Add(detailForecast);
                    }
                }

                if (weather.Forecasts.Count > 0)
                    weather.Forecasts.RemoveAt(0);
            }

            return weather;
        }