-
Notifications
You must be signed in to change notification settings - Fork 8
/
MMM-bitcoin.js
86 lines (74 loc) · 2.04 KB
/
MMM-bitcoin.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
'use strict';
Module.register("MMM-bitcoin", {
result: {},
defaults: {
fiat: 'usd',
showBefore: null,
exchange: 'bitstamp',
updateInterval: 60000,
// Used to work out url and symbols
fiatTable: {
usd: {
symbol: '$',
exchangeCode: 'btcusd'
},
eur: {
symbol: '€',
exchangeCode: 'btceur'
}
}
},
getStyles: function() {
return ["MMM-bitcoin.css"];
},
start: function() {
this.getTickers();
this.scheduleUpdate();
},
getDom: function() {
var wrapper = document.createElement("ticker");
wrapper.className = 'medium bright';
wrapper.className = 'ticker';
var data = this.result;
var symbolElement = document.createElement("span");
var exchange = this.config.exchange;
var fiat = this.config.fiat;
var fiatSymbol = this.config.fiatTable[fiat].symbol;
var lastPrice = data.last;
if (this.config.showBefore == null) {
var showBefore = this.config.exchange;
} else {
var showBefore = this.config.showBefore
}
if (lastPrice) {
symbolElement.innerHTML = showBefore + ' ' + fiatSymbol;
wrapper.appendChild(symbolElement);
var priceElement = document.createElement("span");
priceElement.innerHTML = lastPrice;
wrapper.appendChild(priceElement);
}
return wrapper;
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setInterval(function() {
self.getTickers();
}, nextLoad);
},
getTickers: function () {
var fiat = this.config.fiat;
var url = 'https://www.bitstamp.net/api/v2/ticker/' + this.config.fiatTable[fiat].exchangeCode + '/';
this.sendSocketNotification('GET_TICKERS', url);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "TICKERS_RESULT") {
var self = this;
this.result = payload;
this.updateDom(self.config.fadeSpeed);
}
},
});