silvrgit/app/js/modules/bitcoin.js
2017-08-08 01:06:02 +01:00

173 lines
5.5 KiB
JavaScript

/**
* Created by mdonnel on 22/03/2017.
*/
let BitcoinModel = Backbone.Model.extend({
initialize: function () {
this.set('url', '/btc');
this.set('balanceUrl', '/balance');
let data = {
lastGBP: 0.0,
lastUSD: 0.0,
lows: {gbp: 0, usd: 0},
highs: {gbp: 0, usd: 0},
eclass: '',
balance: 0.0
};
this.set('btcdata', data);
this.set('balance', 0);
this.update();
this.updateHourly();
},
update: function () {
this.getBTC();
//this.getBalance();
const now = new Date();
const mod = 300000 - (now.getTime() % 300000);
let btcupdateFn = function() {
this.update();
};
setTimeout(btcupdateFn.bind(this), mod + 10);
},
updateHourly: function () {
this.getBalance();
const now = new Date();
const mod = 3600000 - (now.getTime() % 3600000);
let btcupdateFn = function() {
this.update();
};
setTimeout(btcupdateFn.bind(this), mod + 10);
},
recalc: function () {
let data = this.get('btcdata');
let lastGBP = data.lastGBP;
let lastUSD;
let g = data.gbp;
let u = data.usd;
let lows = data.lows;
let highs = data.highs;
let eclass = data.eclass;
let balance = data.balance;
if (g !== undefined) {
if (data.lastGBP !== 0) {
if (g > lastGBP) {
eclass = 'up';
} else {
eclass = 'down';
}
} else {
lows.gbp = g;
lows.usd = u;
highs.gbp = g;
highs.usd = u;
}
lastGBP = g;
lastUSD = u;
if (g < lows.gbp) lows.gbp = g;
if (u < lows.usd) lows.usd = u;
if (highs.gbp < g) highs.gbp = g;
if (highs.usd < u) highs.usd = u;
data = {
lastGBP,
lastUSD,
lows,
highs,
eclass,
balance
};
}
data.stub = Math.random(Number.MAX_SAFE_INTEGER).toString(32);
this.set('btcdata', data);
// total = myBTC * g;
},
getBTC: function () {
let self = this;
let url = this.get('url');
console.log('>> getBTC');
$.ajax({
type: 'GET',
url: url,
data: '',
dataType: 'json',
timeout: 10000,
//contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function (data) {
let gbp = data.bpi.GBP.rate_float,
usd = data.bpi.USD.rate_float;
let btcdata = self.get('btcdata');
btcdata.gbp = gbp;
btcdata.usd = usd;
self.set('btcdata', btcdata);
self.recalc();
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
},
getBalance: function() {
let self = this;
let url = this.get('balanceUrl');
$.ajax({
type: 'GET',
url: url,
data: '',
dataType: 'json',
timeout: 10000,
//contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function (data) {
let balance = data.balance;
self.set('balance', balance);
self.recalc();
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
}
});
let Bitcoin = Backbone.View.extend({
tagName: 'div',
initialize: function () {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.$btc = $('#btc');
},
render: function () {
let btcdata = this.model.get('btcdata');
let balance = this.model.get('balance');
//console.log(`Balance: ${btcdata.balance.toFixed(4)}`);
//console.log(btcdata.lastGBP);
let owned = parseFloat(btcdata.lastGBP) * parseFloat(balance);
//console.log(`owned: ${owned}`);
let title = 'High: $' + parseFloat(btcdata.highs.usd.toFixed(2)) + ' / Low $' + parseFloat(btcdata.lows.usd.toFixed(2));
this.$btc.removeClass();
this.$btc.addClass(btcdata.eclass);
this.$btc.html(`&#36;${parseFloat(btcdata.lastUSD.toFixed(2)) } / &pound;${parseFloat(btcdata.lastGBP.toFixed(2))} <div>&#8383;${balance} &pound;${parseFloat(owned.toFixed(2))}</div>` );
this.$btc.prop('title', title);
}
});