Example #1
0
        private void DownloadLatestData(int marketLocation)
        {
            bool retry = false;

            lock (DownloadLock)
            {
                try
                {
                    string lastDownloadKey = LastDownloadTs.FormatInvariant(marketLocation);
                    CacheItem <DateTimeOffset> lastDownload = _priceCache.Get <DateTimeOffset>(lastDownloadKey);

                    // check to see if there is newer data available.
                    MarketLocationData marketDataInfo = LastMarketUpdate(marketLocation);

                    if (lastDownload == null || (lastDownload.IsDirty && lastDownload.Data < marketDataInfo.Freshness))
                    {
                        IEnumerable <ItemOrderStats> locationData = DownloadData(marketLocation);

                        _priceCache.Add(
                            ItemKeyFormat.FormatInvariant(marketLocation),
                            locationData,
                            DateTimeOffset.Now.Add(_cacheTtl));

                        Trace.TraceInformation(Resources.NewMarketData);
                        //// MessageBox.Show(Resources.NewMarketData, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);

                        _priceCache.Add(lastDownloadKey, DateTimeOffset.Now, DateTimeOffset.Now.Add(_cacheTtl));
                    }
                    downloadError = false;
                }
                catch (Exception e)
                {
                    // log it.
                    Trace.TraceError(e.FormatException());
                    if (!downloadError &&
                        MessageBox.Show(
                            Resources.ErrorDownloadingData,
                            Resources.ErrorCaption,
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Error) == DialogResult.Yes)
                    {
                        retry = true;
                    }
                    else
                    {
                        string lastDownloadKey = LastDownloadTs.FormatInvariant(marketLocation);
                        _priceCache.Add(lastDownloadKey, DateTimeOffset.Now, DateTimeOffset.Now.AddMinutes(30));
                    }
                    downloadError = true;
                }
            }

            if (retry)
            {
                DownloadLatestData(marketLocation);
            }
        }
Example #2
0
        /// <summary>The last market update.</summary>
        /// <param name="marketLocationId">The market location id.</param>
        /// <returns>The <see cref="MarketLocationData" />.</returns>
        private MarketLocationData LastMarketUpdate(int marketLocationId)
        {
            Task <HttpResponseMessage> requestTask =
                _requestProvider.GetAsync(new Uri(EveHqBaseLocation + marketLocationId.ToInvariantString()),
                                          "application/json");

            requestTask.Wait();
            MarketLocationData results = null;

            if (requestTask.IsCompleted && requestTask.Result != null && !requestTask.IsCanceled &&
                !requestTask.IsFaulted && requestTask.Exception == null)
            {
                Task <string> readTask = requestTask.Result.Content.ReadAsStringAsync();
                readTask.Wait();

                results = JsonConvert.DeserializeObject <MarketLocationData>(readTask.Result);
            }

            return(results);
        }