2016-08-08 10:46:31 +00:00
|
|
|
'use strict';
|
2016-08-11 14:34:52 +00:00
|
|
|
var rp = require('request-promise');
|
2016-08-08 10:46:31 +00:00
|
|
|
var logger = require('log4js').getLogger();
|
2016-08-11 14:34:52 +00:00
|
|
|
var util = require('util');
|
|
|
|
var request = require('request');
|
|
|
|
var btoa = require('btoa');
|
2016-08-08 10:46:31 +00:00
|
|
|
|
2016-08-11 14:34:52 +00:00
|
|
|
var buildAuthentication = function() {
|
|
|
|
var user = 'a-qz0da4-dfwwdkmkzr'; // Your actual username
|
|
|
|
var pass = '9txJEf3Cjy7hkSOvkv'; // Your actual password
|
|
|
|
var token = user.concat(':', pass);
|
2016-08-08 10:46:31 +00:00
|
|
|
|
2016-08-11 14:34:52 +00:00
|
|
|
return 'Basic '.concat(btoa(token));
|
|
|
|
};
|
|
|
|
|
|
|
|
function getData(data, prev) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
logger.debug('Working...');
|
|
|
|
var range = '';
|
|
|
|
|
|
|
|
if (data.hasOwnProperty('start')) {
|
|
|
|
range = ['?start=', data.start, '&end=', data.end].join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.hasOwnProperty('bookmark')) {
|
|
|
|
|
|
|
|
range = range.concat(range.length > 0 ? '&' : '?');
|
|
|
|
range = range.concat('_bookmark=',encodeURIComponent(data.bookmark));
|
|
|
|
|
|
|
|
}
|
2016-08-08 10:46:31 +00:00
|
|
|
|
2016-08-11 14:34:52 +00:00
|
|
|
var options = {
|
|
|
|
uri: 'https://qz0da4.internetofthings.ibmcloud.com/api/v0002/historian/types/mDot/devices/' + data.id + range,
|
|
|
|
headers: {
|
|
|
|
Authorization: buildAuthentication()
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
};
|
2016-08-08 10:46:31 +00:00
|
|
|
|
2016-08-11 14:34:52 +00:00
|
|
|
logger.debug(options);
|
2016-08-08 10:46:31 +00:00
|
|
|
|
2016-08-11 14:34:52 +00:00
|
|
|
// This is put together to get around the Paging that bluemix uses.
|
|
|
|
// They limit the number of results to 100 per query
|
|
|
|
// Kind of sloppy using the promises like this I think.
|
|
|
|
|
|
|
|
rp(options)
|
|
|
|
.then(function(d) {
|
|
|
|
|
|
|
|
prev.events.concat(d.events);
|
|
|
|
|
|
|
|
Array.prototype.push.apply(prev.events, d.events);
|
|
|
|
|
|
|
|
if (d.hasOwnProperty('bookmark')) {
|
|
|
|
data.bookmark = d.bookmark;
|
|
|
|
getData(data, prev).then(function(d) {
|
|
|
|
resolve(d);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logger.debug('Final resolve...');
|
|
|
|
resolve(prev);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
logger.error(err);
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = module.exports = {
|
|
|
|
|
|
|
|
doGet: function(data) {
|
2016-08-08 10:46:31 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
2016-08-11 14:34:52 +00:00
|
|
|
logger.debug('Inside doGet Promise');
|
|
|
|
logger.debug('About to query');
|
|
|
|
|
|
|
|
getData(data,{events: []}).then(function(d) {
|
|
|
|
resolve(d);
|
|
|
|
}).catch(function(e) {
|
|
|
|
logger.error(e);
|
|
|
|
reject(e);
|
|
|
|
});
|
2016-08-08 10:46:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|