-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmm-uah-currency.js
executable file
·109 lines (90 loc) · 2.77 KB
/
mmm-uah-currency.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//Code was written using core modules as example
Module.register("mmm-uah-currency",{
// Default module config.
defaults: {
apiBase: "https://bank.gov.ua/NBUStatService/",
apiVersion: "v1",
currencyEndpoint: "statdirectory/exchange",
currencyCodes: ["USD", "GBP", "EUR"],
retryDelay: 2500,
initialLoadDelay: 0, // 0 seconds delay
updateInterval: 60 * 60 * 6000, // every 6 hours
},
// Define start sequence.
start: function () {
Log.info("Starting module: " + this.name);
this.currencyHtml = null;
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
if (this.currencyHtml == null) {
wrapper.innerHTML = "Loading...";
} else {
wrapper.innerHTML = this.currencyHtml;
}
wrapper.className = "dimmed light small";
return wrapper;
},
updateCurrency: function () {
var url = this.config.apiBase + this.config.apiVersion + "/" + this.config.currencyEndpoint + this.getParams();
var self = this;
var currencyRequest = new XMLHttpRequest();
currencyRequest.open("GET", url, true);
currencyRequest.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
self.processCurrency(JSON.parse(this.response));
} else {
Log.error(self.name + ": Could not load currency.");
}
self.scheduleUpdate(self.loaded ? -1 : self.config.retryDelay);
}
};
currencyRequest.send();
},
getParams: function () {
var params = "?";
params += "date=" + moment().format('YYYYMMDD');
params += "&json";
return params;
},
/* processCurrency(data)
* Uses the received data to set the various values.
*
* argument data object - Currency information received from bank.gov.ua
*/
processCurrency: function (data) {
this.loaded = true;
var currencyCode = this.config.currencyCode
var currencyCodes = this.config.currencyCodes
this.currencyHtml = "";
currencyCodes.forEach(element => this.addCurrencyElement(element,data));
this.updateDom();
},
addCurrencyElement: function (element, data) {
this.currencyHtml += element + ": ";
this.currencyHtml += this.roundValue(data.find(x => x.cc === element).rate) + "  ";
},
// /* scheduleUpdate()
// * Schedule next update.
// *
// * argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
// */
scheduleUpdate: function (delay) {
// TODO: update should be running at night once.
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function () {
self.updateCurrency();
}, nextLoad);
},
roundValue: function (num) {
return parseFloat(num).toFixed(2);
}
});