2017-08-18 14:08:28 +00:00
|
|
|
const http = require('http');
|
|
|
|
const https = require('https');
|
|
|
|
const LimitedArray = require('limitedarray');
|
|
|
|
const trend = require('trend');
|
|
|
|
const logger = require('log4js').getLogger('btc');
|
2017-06-12 12:37:38 +00:00
|
|
|
let btcCache = {};
|
2017-08-07 14:14:32 +00:00
|
|
|
let balanceCache = {};
|
2017-08-18 14:08:28 +00:00
|
|
|
let history = new LimitedArray(288); // one days worth in 5 minute chunks
|
|
|
|
|
|
|
|
function getBitcoin () {
|
|
|
|
logger.info('>> getting bitcoin');
|
|
|
|
function btcQuery (callback) {
|
|
|
|
const options = {
|
2017-06-12 12:37:38 +00:00
|
|
|
host: 'api.coindesk.com',
|
2017-08-18 14:08:28 +00:00
|
|
|
// port: 80,
|
2017-06-12 12:37:38 +00:00
|
|
|
path: '/v1/bpi/currentprice.json',
|
2017-08-18 14:08:28 +00:00
|
|
|
// method: 'GET',
|
2017-06-12 12:37:38 +00:00
|
|
|
headers: {
|
2017-08-18 14:08:28 +00:00
|
|
|
/* 'Content-Type': 'application/json',
|
|
|
|
'Content-Length': Buffer.byteLength(data)*/
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2017-06-12 12:37:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
try {
|
2017-08-18 14:08:28 +00:00
|
|
|
http.request(options).on('response', response => {
|
|
|
|
let data = '';
|
|
|
|
response.on('data', chunk => {
|
2017-06-12 12:37:38 +00:00
|
|
|
data += chunk;
|
|
|
|
});
|
2017-08-18 14:08:28 +00:00
|
|
|
response.on('end', () => {
|
|
|
|
|
2017-06-12 12:47:24 +00:00
|
|
|
let cData = {};
|
2017-04-05 13:22:31 +00:00
|
|
|
try {
|
2017-06-12 12:47:24 +00:00
|
|
|
cData = JSON.parse(data);
|
2017-08-18 14:08:28 +00:00
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
} finally{
|
|
|
|
callback(cData);
|
2017-06-12 12:37:38 +00:00
|
|
|
}
|
2017-08-18 14:08:28 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
response.on('error', e => {
|
|
|
|
logger.error(e);
|
|
|
|
});
|
|
|
|
}).end();
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
btcQuery(a => {
|
|
|
|
// logger.info(a);
|
|
|
|
logger.info('Got btc data. Storing it');
|
2017-09-05 10:36:24 +00:00
|
|
|
logger.debug('>> btc', a);
|
2017-08-18 14:08:28 +00:00
|
|
|
btcCache = a;
|
|
|
|
history.push(a.bpi.USD.rate_float);
|
|
|
|
btcCache.history = history.get();
|
|
|
|
btcCache.trend = trend(btcCache.history, {avgPoints: 12});
|
|
|
|
GLOBAL.lastcheck = new Date();
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBalance() {
|
|
|
|
logger.info('>> getting Balance');
|
|
|
|
function balanceQuery (callback, r) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
https.get('https://blockexplorer.com/api/addr/18sLVW5Aswp2KWLr4hMFZsuSPtvAauFiif').on('response', response => {
|
|
|
|
let data = '';
|
|
|
|
response.on('data', chunk => {
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
response.on('end', () => {
|
|
|
|
let cData = {};
|
|
|
|
try {
|
|
|
|
cData = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
} finally{
|
2017-06-12 12:47:24 +00:00
|
|
|
callback(cData, r);
|
|
|
|
}
|
2017-06-12 12:37:38 +00:00
|
|
|
|
|
|
|
});
|
2017-08-18 14:08:28 +00:00
|
|
|
response.on('error', e => {
|
|
|
|
logger.error(e);
|
2017-06-12 12:37:38 +00:00
|
|
|
});
|
|
|
|
}).end();
|
|
|
|
} catch (e) {
|
2017-08-18 14:08:28 +00:00
|
|
|
logger.error(e);
|
2017-06-12 12:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2017-08-18 14:08:28 +00:00
|
|
|
balanceQuery(a => {
|
|
|
|
logger.info('Got balance data. Storing it');
|
|
|
|
balanceCache = a;
|
2017-08-07 14:14:32 +00:00
|
|
|
|
2017-08-18 14:08:28 +00:00
|
|
|
});
|
2017-08-07 14:14:32 +00:00
|
|
|
|
2017-08-18 14:08:28 +00:00
|
|
|
}
|
2017-08-07 14:14:32 +00:00
|
|
|
|
2017-08-18 14:08:28 +00:00
|
|
|
|
|
|
|
exports.doBTC = (req, res) => {
|
|
|
|
logger.info('Bitcoin request');
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.end(JSON.stringify(btcCache));
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.doBalance = (req, res) => {
|
|
|
|
logger.info('Balance request');
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.end(JSON.stringify(balanceCache));
|
2017-08-07 14:14:32 +00:00
|
|
|
|
|
|
|
};
|
2017-08-18 14:08:28 +00:00
|
|
|
|
|
|
|
function updateBitcoin() {
|
|
|
|
|
|
|
|
logger.warn('updateBitcoin');
|
|
|
|
getBitcoin();
|
|
|
|
const now = new Date();
|
|
|
|
const mod = 300000 - (now.getTime() % 300000);
|
|
|
|
|
|
|
|
let btcupdateFn = () => {
|
|
|
|
updateBitcoin();
|
|
|
|
};
|
|
|
|
setTimeout(btcupdateFn.bind(this), mod + 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateBalance() {
|
|
|
|
|
|
|
|
logger.warn('updateBalance');
|
|
|
|
getBalance();
|
|
|
|
const now = new Date();
|
|
|
|
const mod = 3.6e+6 - (now.getTime() % 3.6e+6);
|
|
|
|
|
|
|
|
let balanceUpdateFn = () => {
|
|
|
|
updateBalance();
|
|
|
|
};
|
|
|
|
setTimeout(balanceUpdateFn.bind(this), mod + 10);
|
|
|
|
}
|
|
|
|
updateBitcoin();
|
|
|
|
updateBalance();
|