214 lines
5.3 KiB
JavaScript
214 lines
5.3 KiB
JavaScript
/**
|
|
* Created by mdonnel on 22/03/2017.
|
|
*/
|
|
|
|
/**
|
|
*
|
|
* @type {any}
|
|
*/
|
|
const $ = require('jquery');
|
|
const _ = require('underscore');
|
|
const Backbone = require('backbone');
|
|
|
|
const BitcoinModel = Backbone.Model.extend({
|
|
'initialize': function () {
|
|
this.set('url', '/btc');
|
|
this.set('balanceUrl', '/balance');
|
|
const data = {
|
|
'lastGBP': 0.0,
|
|
'lastUSD': 0.0,
|
|
'lows': { 'gbp': 0, 'usd': 0 },
|
|
'highs': { 'gbp': 0, 'usd': 0 },
|
|
'eclass': '',
|
|
'balance': 0.0,
|
|
'trend': 0
|
|
};
|
|
this.set('btcdata', data);
|
|
this.set('balance', 0);
|
|
this.update();
|
|
this.updateHourly();
|
|
},
|
|
'update': function () {
|
|
this.getBTC();
|
|
},
|
|
'updateHourly': function () {
|
|
this.getBalance();
|
|
},
|
|
'recalc': function () {
|
|
let data = this.get('btcdata');
|
|
let lastGBP = data.lastGBP;
|
|
let lastUSD;
|
|
const g = data.gbp;
|
|
const u = data.usd;
|
|
const lows = data.lows;
|
|
const highs = data.highs;
|
|
let eclass = data.eclass;
|
|
const balance = data.balance;
|
|
let trend = data.trend;
|
|
|
|
if ((trend === undefined) || ( trend === null)) trend = 1;
|
|
|
|
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,
|
|
trend
|
|
};
|
|
}
|
|
data.stub = Math.random(Number.MAX_SAFE_INTEGER).toString(32);
|
|
|
|
this.set('btcdata', data);
|
|
// total = myBTC * g;
|
|
},
|
|
'getBTC': function () {
|
|
console.log('>> getBTC');
|
|
const self = this;
|
|
const url = this.get('url');
|
|
$.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;
|
|
const trend = data.trend;
|
|
const btcdata = self.get('btcdata');
|
|
btcdata.gbp = gbp;
|
|
btcdata.usd = usd;
|
|
btcdata.trend = trend;
|
|
self.set('btcdata', btcdata);
|
|
self.recalc();
|
|
},
|
|
'error': function (xhr, type) {
|
|
console.log('ajax error');
|
|
console.log(xhr);
|
|
console.log(type);
|
|
}
|
|
});
|
|
},
|
|
'getBalance': function() {
|
|
const self = this;
|
|
const 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) {
|
|
const balance = data.balance;
|
|
|
|
self.set('balance', balance);
|
|
self.recalc();
|
|
},
|
|
'error': function (xhr, type) {
|
|
console.log('ajax error');
|
|
console.log(xhr);
|
|
console.log(type);
|
|
}
|
|
});
|
|
},
|
|
'btcFromSocket': function (data) {
|
|
const self = this;
|
|
console.log('>> btc from the socket', data);
|
|
const gbp = data.bpi.GBP.rate_float;
|
|
const usd = data.bpi.USD.rate_float;
|
|
const trend = data.trend;
|
|
const btcdata = self.get('btcdata');
|
|
btcdata.gbp = gbp;
|
|
btcdata.usd = usd;
|
|
btcdata.trend = trend;
|
|
self.set('btcdata', btcdata);
|
|
self.recalc();
|
|
},
|
|
'balanceFromSocket': function (data) {
|
|
const self = this;
|
|
console.log('>> balance from the socket', data);
|
|
const balance = data.balance;
|
|
|
|
self.set('balance', balance);
|
|
self.recalc();
|
|
}
|
|
});
|
|
|
|
/**
|
|
*
|
|
* @type {any}
|
|
*/
|
|
const Bitcoin = Backbone.View.extend({
|
|
'tagName': 'div',
|
|
'initialize': function () {
|
|
_.bindAll(this, 'render');
|
|
this.model.bind('change', this.render);
|
|
this.$btc = $('#btc');
|
|
this.$trend = $('#trend');
|
|
},
|
|
'render': function () {
|
|
const btcdata = this.model.get('btcdata');
|
|
const balance = this.model.get('balance');
|
|
const owned = parseFloat(btcdata.lastGBP) * parseFloat(balance);
|
|
// console.log(`owned: ${owned}`);
|
|
const title = `High: $${ parseFloat(btcdata.highs.usd.toFixed(2)) } / Low $${ parseFloat(btcdata.lows.usd.toFixed(2))}`;
|
|
let trendClass = '';
|
|
|
|
if (btcdata.trend > 1.00)
|
|
trendClass = 'trendUp';
|
|
|
|
else if (btcdata.trend < 1.00)
|
|
trendClass = 'trendDown';
|
|
else
|
|
trendClass = '';
|
|
|
|
this.$trend.removeClass();
|
|
this.$trend.addClass(trendClass);
|
|
this.$btc.removeClass();
|
|
this.$btc.addClass(btcdata.eclass);
|
|
this.$btc.html(`$${parseFloat(btcdata.lastUSD.toFixed(2)) } / £${parseFloat(btcdata.lastGBP.toFixed(2))} <div>₿${balance} £${parseFloat(owned.toFixed(2))}</div>` );
|
|
this.$btc.prop('title', title);
|
|
}
|
|
});
|
|
|
|
module.exports = { BitcoinModel, Bitcoin };
|
|
|