public WeatherProviderService(ISb49SecureString apiKey, int providerId, RequestCounterBase requestCounter)
     : base(apiKey, providerId, requestCounter)
 {
 }
 protected WeatherProviderServiceBase(ISb49SecureString apiKey, int providerId, RequestCounterBase requestCounter)
     : this(providerId, requestCounter)
 {
     ApiKey = apiKey;
 }
Exemple #3
0
        private WeatherForecast GetWeatherData(Context context, ISb49SecureString apiKey, IWeatherProviderService weatherProvider,
                                               IWeatherLocationAddress address, string languageCode, CancellationToken token)
        {
            if (weatherProvider == null)
            {
                throw new WeatherProviderException();
            }

            if (apiKey == null || !apiKey.Validate())
            {
                throw new WeatherApiKeyException();
            }

            if (address == null || !address.IsValid())
            {
                throw new LocationException();
            }

            var permissionChecker = new Sb49PermissionChecker();
            var permissions       = new[] { Manifest.Permission.Internet, Manifest.Permission.AccessNetworkState };
            var status            = permissionChecker.CheckPermissionAsync(context, permissions).Result;

            if (status != PermissionStatus.Granted)
            {
                throw new Java.Lang.SecurityException(string.Format("Permission denied. Missing {0} permissions.",
                                                                    string.Join(", ", permissions.Select(p => string.Format("'{0}'", p)))));
            }

            weatherProvider.ApiKey = apiKey;
            if (weatherProvider.OptionalParameters == null)
            {
                weatherProvider.OptionalParameters = new OptionalParameters();
            }
            weatherProvider.OptionalParameters.LanguageCode = languageCode;

            using (var connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService))
            {
                if (!AndroidUtil.IsNetworkAvailable(connectivityManager))
                {
                    throw new Sb49HttpRequestException();
                }

                Log.DebugFormat(
                    "Weather provider: '{0}', Uri: '{1}', Lan: '{2}', Count: '{3}', Connectivity type: '{4}', Address: '{5}'.",
                    weatherProvider.ProviderId, weatherProvider.BaseUri, languageCode,
                    weatherProvider.RequestCounter?.Count,
                    connectivityManager.ActiveNetworkInfo?.Type,
                    address);
            }

            // TwilightCache
            var twilightCacheManager = new TwilightCacheManager();

            try
            {
                var twilightCache = twilightCacheManager.RetrieveData(context, AppSettings.TwilightCacheKey);
                weatherProvider.SetSunInfoCache(twilightCache);
            }
            catch (Newtonsoft.Json.JsonSerializationException ex)
            {
                Log.Error(ex);
                twilightCacheManager.Remove(context, AppSettings.TwilightCacheKey);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

            var result = weatherProvider.GetForecast(address, token: token);

            if (result != null)
            {
                if (!address.Latitude.HasValue)
                {
                    address.Latitude = weatherProvider.Latitude;
                }
                if (!address.Longitude.HasValue)
                {
                    address.Longitude = weatherProvider.Longitude;
                }

                result.AddressHashCode = address.GetMd5Code();

                // TwilightCache
                try
                {
                    var cache = weatherProvider.GetSunInfoCache(TwilightCacheManager.MaxCount);
                    twilightCacheManager.CacheData(context, AppSettings.TwilightCacheKey, cache);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }

            Log.Debug("Weather data updated.");

            return(result);
        }