silvrgit/lib/btc.js

154 lines
3.7 KiB
JavaScript
Raw Normal View History

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 = {};
let eventEmitter;
let lastUpdated;
const history = new LimitedArray(288); // one days worth in 5 minute chunks
2017-08-18 14:08:28 +00:00
function getBitcoin () {
logger.info('>> getting bitcoin');
function btcQuery (callback) {
const options = {
'host': 'api.coindesk.com',
2017-08-18 14:08:28 +00:00
// port: 80,
'path': '/v1/bpi/currentprice.json',
2017-08-18 14:08:28 +00:00
// method: 'GET',
'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);
}
catch (e) {
2017-08-18 14:08:28 +00:00
logger.error(e);
}
finally{
2017-08-18 14:08:28 +00:00
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) {
2017-08-18 14:08:28 +00:00
logger.error(e);
}
}
btcQuery(a => {
// logger.info(a);
2017-08-18 14:08:28 +00:00
logger.info('Got btc data. Storing it');
2017-09-05 10:36:24 +00:00
logger.debug('>> btc', a);
console.log(a.time.updatedISO);
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 });
2017-08-18 14:08:28 +00:00
GLOBAL.lastcheck = new Date();
btcCache.lastcheck = GLOBAL.lastcheck.toISOString();
eventEmitter.emit('sendSocket', { 'id': 'btc', 'data': btcCache });
2017-08-18 14:08:28 +00:00
});
}
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) {
2017-08-18 14:08:28 +00:00
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;
eventEmitter.emit('sendSocket', { 'id': 'balance', 'data': balanceCache });
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);
const btcupdateFn = () => {
2017-08-18 14:08:28 +00:00
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);
const balanceUpdateFn = () => {
2017-08-18 14:08:28 +00:00
updateBalance();
};
setTimeout(balanceUpdateFn.bind(this), mod + 10);
}
exports.setEmitter = (newEmitter) => {
logger.debug('Setting events', newEmitter);
eventEmitter = newEmitter;
};
setTimeout(function() {
updateBitcoin();
updateBalance();
}, 25000);