silvrgit/lib/btc.js
2022-10-17 17:26:51 +01:00

154 lines
3.7 KiB
JavaScript

const http = require('http');
const https = require('https');
const LimitedArray = require('@rakh/limitedarray');
const trend = require('trend');
const logger = require('log4js').getLogger('btc');
let btcCache = {};
let balanceCache = {};
let eventEmitter;
let lastUpdated;
const history = new LimitedArray(288); // one days worth in 5 minute chunks
function getBitcoin () {
logger.info('>> getting bitcoin');
function btcQuery (callback) {
const options = {
'host': 'api.coindesk.com',
// port: 80,
'path': '/v1/bpi/currentprice.json',
// method: 'GET',
'headers': {
/* 'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)*/
}
};
try {
http.request(options).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{
callback(cData);
}
});
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');
logger.debug('>> btc', a);
// console.log(a.time.updatedISO);
btcCache = a;
history.push(a.bpi.USD.rate_float);
btcCache.history = history.get();
btcCache.trend = trend(btcCache.history, { 'avgPoints': 12 });
global.lastcheck = new Date();
btcCache.lastcheck = global.lastcheck.toISOString();
eventEmitter.emit('sendSocket', { 'id': 'btc', 'data': btcCache });
});
}
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{
callback(cData, r);
}
});
response.on('error', e => {
logger.error(e);
});
}).end();
}
catch (e) {
logger.error(e);
}
}
balanceQuery(a => {
logger.info('Got balance data. Storing it');
balanceCache = a;
eventEmitter.emit('sendSocket', { 'id': 'balance', 'data': balanceCache });
});
}
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));
};
function updateBitcoin() {
logger.warn('updateBitcoin');
getBitcoin();
const now = new Date();
const mod = 300000 - (now.getTime() % 300000);
const 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);
const balanceUpdateFn = () => {
updateBalance();
};
setTimeout(balanceUpdateFn.bind(this), mod + 10);
}
exports.setEmitter = (newEmitter) => {
logger.debug('Setting events', newEmitter);
eventEmitter = newEmitter;
};
setTimeout(function() {
updateBitcoin();
updateBalance();
}, 25000);