Пример #1
0
        [Route("{city}")] // weather/Sydney,%22 AU
        public async Task <WeatherForecast> Get(string city)
        {
            var forecast = await client.GetCurrentWeatherAsync(city);

            return(new WeatherForecast {
                Summary = forecast.weather[0].description,
                TemperatureC = (int)forecast.main.temp,
                Date = DateTimeOffset.FromUnixTimeSeconds(forecast.dt).DateTime
            });
        }
        public void UpdateWeatherData()
        {
            try
            {
                //If no settings, do nothing
                if (string.IsNullOrEmpty(_citySetting.Value) || string.IsNullOrEmpty(_unitOfMeasurementSetting.Value))
                    return;

                //TODO: Use settings
                string accessKey = _apiKeySetting.Value;
                WeatherClient client = new WeatherClient(accessKey);
                CurrrentWeatherModel data = client.GetCurrentWeatherAsync<CurrrentWeatherModel>(_citySetting.Value, "en", _unitOfMeasurementSetting.Value).Result;

                // Weather Measurements
                DataModel.Weather = (WeatherConditions)Enum.Parse(typeof(WeatherConditions), data.Weather.FirstOrDefault().Main);
                DataModel.Temp = data.Main.Temp;
                DataModel.FeelsLike = data.Main.FeelsLike;
                DataModel.TempMin = data.Main.TempMin;
                DataModel.TempMax = data.Main.TempMax;
                DataModel.Pressure = data.Main.Pressure;
                DataModel.Humidity = data.Main.Humidity;

                // Visibility
                DataModel.Clouds = data.Clouds.All; // Cloudiness
                DataModel.Visibility = data.Visibility; // Meters

                // Sunrise Sunset


                DataModel.Sunrise = DateTimeOffset.FromUnixTimeSeconds(data.Sys.Sunrise).DateTime.ToLocalTime(); // unix, UTC
                DataModel.Sunset = DateTimeOffset.FromUnixTimeSeconds(data.Sys.Sunset).DateTime.ToLocalTime(); // unix, UTC
                // unix, UTC

                // Wind
                DataModel.Wind.Speed = data.Wind.Speed;
                DataModel.Wind.Deg = data.Wind.Deg;
                DataModel.Wind.WindDirection = (WindDirectionCodes)Enum.Parse(typeof(WindDirectionCodes), data.Wind.WindDirection);
            }
            catch (Exception e)
            {
                _logger.Error(e.ToString());
            }
        }
Пример #3
0
        public void GetCurrentWeatherTest()
        {
            List <string> cities = new List <string>()
            {
                "Canberra",
                "Brussels",
                "Beijing",
                "London",
                "Moscow",
                "Prague",
                "Tallinn",
                "Helsinki",
                "Paris",
                "Berlin",
                "Reykjavik"
            };

            WeatherClient client = new WeatherClient(AccessKey);

            foreach (string city in cities)
            {
                CurrrentWeatherModel data = client.GetCurrentWeatherAsync <CurrrentWeatherModel>(city, "en").Result;
            }
        }