From 4241688a550499b67c202767440f1775e50eef91 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 7 Feb 2024 16:47:42 +0300 Subject: CurrencyRates: CB RF provider --- protocols/CurrencyRates/Forex.vcxproj | 1 + protocols/CurrencyRates/Forex.vcxproj.filters | 3 + protocols/CurrencyRates/docs/Utility/cbrf.xml | 110 +++++++++++++++++++++ .../src/CurrencyRatesProviderCbrf.cpp | 77 +++++++++++++++ .../CurrencyRates/src/CurrencyRatesProviders.cpp | 2 + 5 files changed, 193 insertions(+) create mode 100644 protocols/CurrencyRates/docs/Utility/cbrf.xml create mode 100644 protocols/CurrencyRates/src/CurrencyRatesProviderCbrf.cpp (limited to 'protocols') diff --git a/protocols/CurrencyRates/Forex.vcxproj b/protocols/CurrencyRates/Forex.vcxproj index f65c410009..e833771ab7 100644 --- a/protocols/CurrencyRates/Forex.vcxproj +++ b/protocols/CurrencyRates/Forex.vcxproj @@ -31,6 +31,7 @@ + diff --git a/protocols/CurrencyRates/Forex.vcxproj.filters b/protocols/CurrencyRates/Forex.vcxproj.filters index f01423621c..7f9353f2b7 100644 --- a/protocols/CurrencyRates/Forex.vcxproj.filters +++ b/protocols/CurrencyRates/Forex.vcxproj.filters @@ -59,6 +59,9 @@ Source Files + + Source Files + diff --git a/protocols/CurrencyRates/docs/Utility/cbrf.xml b/protocols/CurrencyRates/docs/Utility/cbrf.xml new file mode 100644 index 0000000000..d5cdc15aae --- /dev/null +++ b/protocols/CurrencyRates/docs/Utility/cbrf.xml @@ -0,0 +1,110 @@ + + + + Russian Central Bank Rates API + https://www.cbr-xml-daily.ru/ + https://www.cbr-xml-daily.ru/latest.js + +
+ Currencies + + AMDAMDArmenian Dram (AMD) + + + ANGANGNetherlands Antillean Gulden (ANG) + + + AUDAUDAustralian Dollar (AUD) + + + BGNBGNBulgarian Lev (BGN) + + + BRLBRLBrazilian Real (BRL) + + + BYNBYNBelarusian Ruble (BYN) + + + CADCADCanadian Dollar (CAD) + + + CHFCHFSwiss Franc (CHF) + + + CNYCNYChinese Yuan (CNY) + + + DKKDKKDanish Krone (DKK) + + + EGPEGPEgyptian Pound (EGP) + + + EUREUREuro (EUR) + + + GBPGBPBritish Pound (GBP) + + + GELGELGeorgian Lari (GEL) + + + HUFHUFHungarian Forint (HUF) + + + INRINRIndian Rupee (INR) + + + JPYJPYJapanese Yen (JPY) + + + KGSKGSKyrgystani Som (KGS) + + + KRWKRWSouth Korean Won (KRW) + + + KZTKZTKazakhstani Tenge (KZT) + + + NOKNOKNorwegian Krone (NOK) + + + NZDNZDNew Zealand Dollar (NZD) + + + PLNPLNPolish Zloty (PLN) + + + RUBRUBRussian Ruble (RUB) + + + SEKSEKSwedish Krona (SEK) + + + THBTHBThai Baht (THB) + + + TJSTJSTajikistani Somoni (TJS) + + + TMTTMTTurkmenistani Manat (TMT) + + + TRYTRYTurkish Lira (TRY) + + + UAHUAHUkrainian Hryvnia (UAH) + + + USDUSDUnited States Dollar (USD) + + + UZSUZSUzbekistani Som (UZS) + + + ZARZARSouth African Rand (ZAR) + +
+
\ No newline at end of file diff --git a/protocols/CurrencyRates/src/CurrencyRatesProviderCbrf.cpp b/protocols/CurrencyRates/src/CurrencyRatesProviderCbrf.cpp new file mode 100644 index 0000000000..ed7a94bb3a --- /dev/null +++ b/protocols/CurrencyRates/src/CurrencyRatesProviderCbrf.cpp @@ -0,0 +1,77 @@ +#include "stdafx.h" + +///////////////////////////////////////////////////////////////////////////////////////// +// CCurrencyRatesProviderExchangeRates implementation + +class CCurrencyRatesProviderCbrf : public CCurrencyRatesProviderBase +{ + std::map m_lastRates; + + double GetRate(const CMStringA &id1, const CMStringA &id2) const + { + if (!id1.IsEmpty() && !id2.IsEmpty()) { + auto r1 = m_lastRates.find(id1), r2 = m_lastRates.find(id2); + if (r1 != m_lastRates.end() && r2 != m_lastRates.end()) + return r2->second / r1->second; + } + + return 0.0; + } + +public: + CCurrencyRatesProviderCbrf() + {} + + void RefreshCurrencyRates(TContacts &anContacts) override + { + CHTTPSession http; + if (true == http.OpenURL(GetURL())) { + CMStringW sHTML; + if (true == http.ReadResponce(sHTML)) { + JSONNode root = JSONNode::parse(_T2A(sHTML)); + if (!root) + return; + + auto rates = root["rates"]; + if (!rates) + return; + + auto &qs = GetSection(); + + m_lastRates.clear(); + for (auto &it : qs.GetCurrencyRates()) { + CMStringA id = it.GetID(); + m_lastRates[id] = (id == "RUB") ? 1.0 : rates[id].as_float(); + } + } + } + + for (auto &hContact : anContacts) { + double rate = GetRate(g_plugin.getMStringA(hContact, DB_STR_FROM_ID), g_plugin.getMStringA(hContact, DB_STR_TO_ID)); + if (rate != 0.0) { + WriteContactRate(hContact, rate); + continue; + } + SetContactStatus(hContact, ID_STATUS_NA); + } + } + + double Convert(double dAmount, const CCurrencyRate &from, const CCurrencyRate &to) const override + { + CMStringA id1 = from.GetID(), id2 = to.GetID(); + return dAmount * GetRate(id1, id2); + } + + wchar_t* GetXmlFilename() const override + { + return L"cbrf.xml"; + } +}; + +///////////////////////////////////////////////////////////////////////////////////////// +// Module entry point + +void InitCbrf() +{ + g_apProviders.push(new CCurrencyRatesProviderCbrf()); +} diff --git a/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp b/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp index 70d9979b06..5aa6dac52e 100644 --- a/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp +++ b/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp @@ -3,6 +3,7 @@ #define LAST_RUN_VERSION "LastRunVersion" void InitCC(); +void InitCbrf(); void InitExchangeRates(); TCurrencyRatesProviders g_apProviders; @@ -14,6 +15,7 @@ CCurrencyRatesProviderBase *g_pCurrentProvider = nullptr; void InitProviders() { InitCC(); + InitCbrf(); InitExchangeRates(); g_pCurrentProvider = FindProvider(g_plugin.getMStringW(DB_STR_PROVIDER)); -- cgit v1.2.3