silvrgit/app/js/modules/bitcoin.js

173 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-03-22 15:48:36 +00:00
/**
* Created by mdonnel on 22/03/2017.
*/
let BitcoinModel = Backbone.Model.extend({
initialize: function () {
this.set('url', '/btc');
2017-08-07 14:14:32 +00:00
this.set('balanceUrl', '/balance');
2017-03-22 15:48:36 +00:00
let data = {
lastGBP: 0.0,
lastUSD: 0.0,
lows: {gbp: 0, usd: 0},
highs: {gbp: 0, usd: 0},
2017-08-07 14:14:32 +00:00
eclass: '',
balance: 0.0
2017-03-22 15:48:36 +00:00
};
this.set('btcdata', data);
2017-08-08 00:06:02 +00:00
this.set('balance', 0);
2017-03-22 15:48:36 +00:00
this.update();
2017-08-07 14:14:32 +00:00
this.updateHourly();
2017-03-22 15:48:36 +00:00
},
update: function () {
this.getBTC();
2017-08-07 14:14:32 +00:00
//this.getBalance();
2017-04-02 23:07:55 +00:00
const now = new Date();
2017-08-08 00:06:02 +00:00
const mod = 300000 - (now.getTime() % 300000);
2017-03-22 15:48:36 +00:00
let btcupdateFn = function() {
this.update();
};
setTimeout(btcupdateFn.bind(this), mod + 10);
},
2017-08-07 14:14:32 +00:00
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);
},
2017-03-22 15:48:36 +00:00
recalc: function () {
let data = this.get('btcdata');
let lastGBP = data.lastGBP;
2017-04-02 23:07:55 +00:00
let lastUSD;
2017-03-22 15:48:36 +00:00
let g = data.gbp;
let u = data.usd;
let lows = data.lows;
let highs = data.highs;
let eclass = data.eclass;
2017-08-07 14:14:32 +00:00
let balance = data.balance;
if (g !== undefined) {
if (data.lastGBP !== 0) {
if (g > lastGBP) {
eclass = 'up';
} else {
eclass = 'down';
}
2017-03-22 15:48:36 +00:00
} else {
2017-08-07 14:14:32 +00:00
lows.gbp = g;
lows.usd = u;
highs.gbp = g;
highs.usd = u;
2017-03-22 15:48:36 +00:00
}
2017-08-07 14:14:32 +00:00
lastGBP = g;
lastUSD = u;
2017-03-22 15:48:36 +00:00
2017-08-07 14:14:32 +00:00
if (g < lows.gbp) lows.gbp = g;
if (u < lows.usd) lows.usd = u;
2017-03-22 15:48:36 +00:00
2017-08-07 14:14:32 +00:00
if (highs.gbp < g) highs.gbp = g;
if (highs.usd < u) highs.usd = u;
data = {
lastGBP,
lastUSD,
lows,
highs,
eclass,
balance
};
}
2017-08-08 00:06:02 +00:00
data.stub = Math.random(Number.MAX_SAFE_INTEGER).toString(32);
2017-03-22 15:48:36 +00:00
this.set('btcdata', data);
// total = myBTC * g;
},
getBTC: function () {
let self = this;
let url = this.get('url');
2017-08-08 00:06:02 +00:00
console.log('>> getBTC');
2017-03-22 15:48:36 +00:00
$.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);
}
});
2017-08-07 14:14:32 +00:00
},
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;
2017-08-08 00:06:02 +00:00
self.set('balance', balance);
2017-08-07 14:14:32 +00:00
self.recalc();
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
2017-03-22 15:48:36 +00:00
}
});
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');
2017-08-08 00:06:02 +00:00
let balance = this.model.get('balance');
2017-08-07 14:14:32 +00:00
//console.log(`Balance: ${btcdata.balance.toFixed(4)}`);
//console.log(btcdata.lastGBP);
2017-08-08 00:06:02 +00:00
let owned = parseFloat(btcdata.lastGBP) * parseFloat(balance);
2017-08-07 14:14:32 +00:00
//console.log(`owned: ${owned}`);
2017-03-22 15:48:36 +00:00
let title = 'High: $' + parseFloat(btcdata.highs.usd.toFixed(2)) + ' / Low $' + parseFloat(btcdata.lows.usd.toFixed(2));
this.$btc.removeClass();
this.$btc.addClass(btcdata.eclass);
2017-08-08 00:06:02 +00:00
this.$btc.html(`&#36;${parseFloat(btcdata.lastUSD.toFixed(2)) } / &pound;${parseFloat(btcdata.lastGBP.toFixed(2))} <div>&#8383;${balance} &pound;${parseFloat(owned.toFixed(2))}</div>` );
2017-03-22 15:48:36 +00:00
this.$btc.prop('title', title);
}
});