Пример #1
0
        public async Task <FxRateDetails> Parse(string fromCurrency, string toCurrency, string url)
        {
            try
            {
                WebClient web      = new WebClient();
                string    response = await web.DownloadStringTaskAsync(url);

                //string[] values = Regex.Split(response,",");
                //double rate = System.Convert.ToDouble(values[1]);

                var matches = Regex.Matches(response, "<span class=\"?bld\"?>([^<]+)</span>");

                if (matches.Count > 0)
                {
                    var matches2 = Regex.Split(matches[0].Groups[1].Value, @"[^0-9\.]+");

                    if (matches2 != null && matches2.Length > 1)
                    {
                        var rate = Convert.ToDouble(matches2[0]);

                        var fxRateDetails = new FxRateDetails();
                        fxRateDetails.FromCurrency = fromCurrency;
                        fxRateDetails.ToCurrency   = toCurrency;
                        fxRateDetails.Rate         = rate;
                        return(fxRateDetails);
                    }
                }
            }
            catch (Exception)
            {
                // ignored, if exception
            }
            return(null);
        }
Пример #2
0
        private void RatesService_FxRateUpdate(FXRateService.FxRateDetails rate)
        {
            lock (locker)
            {
                RateViewModel exisistingRate;
                if (rateDictionary.TryGetValue(rate.FromCurrency + rate.ToCurrency, out exisistingRate))
                {
                    exisistingRate.UpdateCount += 1;

                    if (exisistingRate.UpdateCount == 1)
                    {
                        exisistingRate.Rate       = rate.Rate;
                        exisistingRate.UpdateTime = rate.UpdateTime;
                        return;
                    }
                    else if (exisistingRate.Rate == rate.Rate && exisistingRate.UpdateCount % 3 == 0)
                    {
                        exisistingRate.Rate       = rate.Rate + 0.01;
                        exisistingRate.UpdateTime = rate.UpdateTime;
                    }
                    else
                    {
                        exisistingRate.Rate       = rate.Rate;
                        exisistingRate.UpdateTime = rate.UpdateTime;
                    }

                    //Apply calculation to change currency rates for all other currencies conversion using ToCurrency

                    //Update all currencies in row
                    var dependentCurrenciesInRow = rateDictionary.Values.Where(v => v.FromCurrency == rate.ToCurrency).ToList();

                    if (dependentCurrenciesInRow != null && dependentCurrenciesInRow.Count > 0)
                    {
                        foreach (var cur in dependentCurrenciesInRow)
                        {
                            RateViewModel exisistingRate2;
                            if (rateDictionary.TryGetValue("USD" + cur.ToCurrency, out exisistingRate2))
                            {
                                try
                                {
                                    cur.Rate       = exisistingRate2.Rate / exisistingRate.Rate;
                                    cur.UpdateTime = exisistingRate.UpdateTime;
                                }
                                catch (DivideByZeroException)
                                {
                                }
                            }
                        }
                    }

                    //update all currencies in columns
                    var dependentCurrenciesInColumn = rateDictionary.Values.Where(v => v.ToCurrency == rate.ToCurrency && v.ToCurrency != "USD").ToList();

                    if (dependentCurrenciesInColumn != null && dependentCurrenciesInColumn.Count > 0)
                    {
                        foreach (var cur in dependentCurrenciesInColumn)
                        {
                            RateViewModel exisistingRate2;
                            if (rateDictionary.TryGetValue("USD" + cur.FromCurrency, out exisistingRate2))
                            {
                                try
                                {
                                    cur.Rate       = exisistingRate.Rate / exisistingRate2.Rate;
                                    cur.UpdateTime = exisistingRate.UpdateTime;
                                }
                                catch (DivideByZeroException)
                                {
                                }
                            }
                        }
                    }
                }
            }
        }