Ejemplo n.º 1
0
        public WeatherInformation GetWeather()
        {
            var client       = _serviceProvider.GetService <IRestClient>();
            var response     = client.GetString(CreateRequestUrl());
            var deserialized = JsonConvert.DeserializeObject <JObject>(response);

            var url = $"http://openweathermap.org/img/w/{deserialized["weather"][0]["icon"].Value<string>()}.png";

            if (!_imagesCache.ContainsKey(url))
            {
                using (var imageStream = client.GetStream(url))
                {
                    _imagesCache[url] = Image.FromStream(imageStream);
                }
            }

            var weatherInformation = new WeatherInformation
            {
                Description    = deserialized["weather"][0]["description"].Value <string>(),
                Cloudiness     = deserialized["clouds"]["all"].Value <double>(),
                Image          = (Image)_imagesCache[url].Clone(),
                MaxTemperature = deserialized["main"]["temp_max"].Value <double>(),
                MinTemperature = deserialized["main"]["temp_min"].Value <double>(),
                SunRise        = UnixTimeStampToDateTime(deserialized["sys"]["sunrise"].Value <long>()),
                SunSet         = UnixTimeStampToDateTime(deserialized["sys"]["sunset"].Value <long>()),
                Temperature    = deserialized["main"]["temp"].Value <double>(),
                WindDegree     = deserialized["wind"]["deg"]?.Value <double>() ?? 0,
                WindSpeed      = deserialized["wind"]["speed"].Value <double>()
            };

            return(weatherInformation);
        }
 public WeatherInformation GetWeather()
 {
     try
     {
         var weatherInformation = _baseWeatherProvider.GetWeather();
         _weatherInformation = weatherInformation;
         return weatherInformation;
     }
     catch (Exception)
     {
         if (_weatherInformation != null)
             return _weatherInformation;
         else
         {
             throw;
         }
     }
 }