Esempio n. 1
0
        public ICollection <Protein> GetProteins(IProgress <ProgressInfo> progress)
        {
            var requestUri   = new Uri(_preferences.Get <string>(Preference.ProjectDownloadUrl));
            var webOperation = WebOperation.Create(requestUri);

            if (progress != null)
            {
                webOperation.ProgressChanged += (sender, e) =>
                {
                    int    progressPercentage = Convert.ToInt32(e.Length / (double)e.TotalLength * 100);
                    string message            = $"Downloading {e.Length} of {e.TotalLength} bytes...";
                    progress.Report(new ProgressInfo(progressPercentage, message));
                };
            }
            webOperation.WebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            webOperation.WebRequest.Proxy       = WebProxyFactory.Create(_preferences);
            using (var stream = new MemoryStream())
            {
                webOperation.Download(stream);
                stream.Position = 0;

                var serializer = new ProjectSummaryJsonDeserializer();
                return(serializer.Deserialize(stream));
            }
        }
        /// <summary>
        /// Refreshes the service data and returns a collection of objects detailing how the service data was changed.
        /// </summary>
        /// <param name="progress">The object used to report refresh progress.</param>
        /// <returns>A collection of objects detailing how the service data was changed</returns>
        public IReadOnlyCollection <ProteinDictionaryChange> Refresh(IProgress <ProgressInfo> progress)
        {
            IReadOnlyCollection <ProteinDictionaryChange> dictionaryChanges;

            using (var stream = new MemoryStream())
            {
                Logger.Info("Downloading new project data from Stanford...");
                _downloader.Download(stream, progress);
                stream.Position = 0;

                var serializer    = new ProjectSummaryJsonDeserializer();
                var newDictionary = ProteinDictionary.CreateFromExisting(_dictionary, serializer.Deserialize(stream));
                dictionaryChanges = newDictionary.Changes;
                _dictionary       = newDictionary;
            }

            foreach (var info in dictionaryChanges.Where(info => info.Result != ProteinDictionaryChangeResult.NoChange))
            {
                Logger.Info(info.ToString());
            }

            var now = DateTime.UtcNow;

            foreach (var key in _projectsNotFound.Keys.ToList())
            {
                if (_dictionary.ContainsKey(key))
                {
                    _projectsNotFound.Remove(key);
                }
                else
                {
                    _projectsNotFound[key] = now;
                }
            }
            _lastRefreshTime = now;

            Write();
            return(dictionaryChanges);
        }