2using System.Collections.Generic;
4using System.Threading.Tasks;
23 private static string apiKey;
24 private static int rateMaxAgeSeconds;
27 private Dictionary<string, CurrencySymbol> currencyDictionary;
28 private Dictionary<CaseInsensitiveString, decimal> ratesReference;
30 private DateTime ratesTimestamp = DateTime.MinValue;
31 private readonly SemaphoreSlim syncObject =
new SemaphoreSlim(1);
71 #region IModule interface
79 await this.CheckCurrencies();
81 Gateway.OnAfterBackup += this.Gateway_OnAfterBackup;
95 private async Task Gateway_OnAfterBackup(
object sender, EventArgs e)
97 if (!
string.IsNullOrEmpty(apiKey))
101 await this.LoadCurrencies();
110 private async Task CheckCurrencies()
112 if (this.currencies is
null && !
string.IsNullOrEmpty(apiKey))
113 await this.LoadCurrencies();
116 private async Task LoadCurrencies()
121 Dictionary<string, CurrencySymbol> Sorted =
new Dictionary<string, CurrencySymbol>();
123 foreach (CurrencySymbol Symbol
in Currencies)
124 Sorted[Symbol.Currency] = Symbol;
126 this.currencies = Currencies;
127 this.currencyDictionary = Sorted;
140 apiKey =
string.Empty;
141 return Task.CompletedTask;
146 #region IConfigurableModule interface
156 new ConfigurablePage(
"openexchangerates.org",
"/OpenExchangeRates/Settings.md")
162 #region API interface
166 #region ICurrencyConverterService interface
176 return Grade.NotAtAll;
180 if (!(this.currencyDictionary is
null))
182 return this.currencyDictionary.ContainsKey(Object.
From.
Value.ToUpper()) &&
183 this.currencyDictionary.ContainsKey(Object.
To.
Value.ToUpper()) ? Grade.Ok :
Grade.NotAtAll;
186 this.CheckCurrencies().Wait();
188 if (this.currencyDictionary is
null)
189 return Grade.NotAtAll;
191 return this.currencyDictionary.ContainsKey(Object.
From.
Value.ToUpper()) &&
192 this.currencyDictionary.ContainsKey(Object.
To.
Value.ToUpper()) ? Grade.Ok :
Grade.NotAtAll;
196 return Grade.NotAtAll;
209 DateTime Now = DateTime.Now;
211 await this.syncObject.WaitAsync();
214 if (Now.Subtract(
this.ratesTimestamp).TotalSeconds >= rateMaxAgeSeconds)
216 Dictionary<CaseInsensitiveString, decimal> Sorted =
new Dictionary<CaseInsensitiveString, decimal>();
222 this.ratesReference = Sorted;
223 this.ratesTimestamp = Now;
226 if (this.ratesReference.TryGetValue(FromCurrency, out decimal FromRate) &&
227 this.ratesReference.TryGetValue(ToCurrency, out decimal ToRate))
229 return new ExchangeRate(FromCurrency, ToCurrency, ToRate / FromRate,
230 this.ratesTimestamp,
"openexchangerates.org");
237 this.syncObject.Release();
Contains a pair of currencies, for currency conversion.
CaseInsensitiveString From
From Currency
CaseInsensitiveString To
To Currency
decimal Rate
Exchange rate.
CaseInsensitiveString ToCurrency
Conversion to this currency.
Task< CurrencySymbol[]> GetCurrencies()
Gets supported currency symbols.
Task< ExchangeRate[]> GetLatest()
Gets latest exchange rates.
Currency Converter service from openexchangerates.org.
async Task< ICurrencyConverterQuote > GetCurrencyConversionQuote(CaseInsensitiveString FromCurrency, CaseInsensitiveString ToCurrency)
Gets a Currency Exchange Rate from one currency to another.
Grade Supports(CurrencyPair Object)
If the interface understands objects such as Object .
OpenExchangeRatesService(string ApiKey)
Currency Converter service from openexchangerates.org.
async Task Start()
Starts the module.
Task Stop()
Stops the module.
static OpenExchangeRateClient Client
Reference to the API client
string ApiKey
Current API Key
static async Task InvalidateCurrent()
Invalidates current settings.
Task< IConfigurablePage[]> GetConfigurablePages()
Gets an array of configurable pages for the module.
OpenExchangeRatesService()
Currency Converter service from openexchangerates.org.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Configuration page for a configurable module.
Represents a case-insensitive string.
string Value
String-representation of the case-insensitive string. (Representation is case sensitive....
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
Interface for currency converter services
Interface for configurable modules.
Configuration page for a configurable module.