我正在尝试使用IoT Foundation's rest API在特定时间范围内检索历史事件。
但是,如果我指定API调用的可选开始/结束参数,IoT Foundation不会按预期识别它们并在意外时间范围内返回事件。
我想知道我是否错误地使用了API,或者它是IoT Foundation的错误。
以下是在过去24小时内检索事件的代码(test_hist.js)
(我的凭证参数替换为* s。)
/* IBM Internet of Things Foundation instance specific parameters */
var httpHost = '******.internetofthings.ibmcloud.com';
var authOptions= {
'user': '*******************', // apikey
'password': '******************' // apiToken
};
var deviceId = '******************';
var Client = require('node-rest-client').Client;
var restClient = new Client(authOptions);
var endMs = new Date().valueOf();
var startMs = endMs - 24 * 60 * 60 * 1000;
console.error('DEBUG: start=' + startMs + ' : ' + new Date(startMs));
console.error('DEBUG: end =' + endMs + ' : ' + new Date(endMs));
var params = {};
if (process.argv.indexOf('-s') === -1) params.start = startMs; // -s option to omit start parameter
if (process.argv.indexOf('-e') === -1) params.end = endMs; // -e option to omit end parameter
var verbose = (process.argv.indexOf('-v') >= 0); // -v option to output events to stdout.
doGet(params);
function doGet(params, allEvents) {
var url = 'https://' + httpHost + '/api/v0002/historian/types/HARemoteControl/devices/' + deviceId;
var args = { parameters: params };
var req = restClient.get(url, args, function(data, response) {
if (response.statusCode != 200) {
console.error(response.statusMessage);
} else {
var events = data.events;
var eventSize = events.length;
allEvents = !allEvents ? events : allEvents.concat(events);
var getMore = false;
if ('bookmark' in data) {
if (!('_bookmark' in params)) console.error('DEBUG: bookmark=' + data.bookmark);
params._bookmark = data.bookmark;
if (0 < eventSize && startMs < events[eventSize-1].timestamp.$date) {
doGet(params, allEvents);
getMore = true;
}
}
if (eventSize > 0) {
var lastTime = new Date(events[0].timestamp.$date);
var firstTime = new Date(events[eventSize-1].timestamp.$date);
console.error('DEBUG: ' + firstTime + ' ... ' + lastTime + ' events.length=' + eventSize + ('bookmark' in data ? ' more...' : ''));
} else {
console.error('DEBUG: events.length=0' + ('bookmark' in data ? ' more...' : ''));
}
if (verbose && !getMore) console.log( allEvents );
}
});
if (!allEvents) console.error('DEBUG: path=' + req.options.path);
}
结果:
当我第一次运行此代码时,API会按预期返回24小时的事件。
$ node test_hist.js
DEBUG: start=1445993284029 : Wed Oct 28 2015 09:48:04 GMT+0900 (JST)
DEBUG: end =1446079684029 : Thu Oct 29 2015 09:48:04 GMT+0900 (JST)
DEBUG: path=/api/v0002/historian/types/HARemoteControl/devices/HARemoteControl001?start=1445993284029&end=1446079684029
DEBUG: bookmark=1:iotHistorianSessionId=AnLL7ocgqFvkmyqAKXERIiNCBPvZttRNy7%2bIP7Vi8xJJCQq56QVBE0cSkQMI1W1L; HttpOnly
DEBUG: Thu Oct 29 2015 08:09:03 GMT+0900 (JST) ... Thu Oct 29 2015 09:48:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Thu Oct 29 2015 06:29:03 GMT+0900 (JST) ... Thu Oct 29 2015 08:08:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Thu Oct 29 2015 04:49:03 GMT+0900 (JST) ... Thu Oct 29 2015 06:28:08 GMT+0900 (JST) events.length=100 more...
DEBUG: Thu Oct 29 2015 03:09:03 GMT+0900 (JST) ... Thu Oct 29 2015 04:48:05 GMT+0900 (JST) events.length=100 more...
DEBUG: Thu Oct 29 2015 01:29:03 GMT+0900 (JST) ... Thu Oct 29 2015 03:08:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 23:49:03 GMT+0900 (JST) ... Thu Oct 29 2015 01:28:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 22:09:08 GMT+0900 (JST) ... Wed Oct 28 2015 23:48:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 20:39:03 GMT+0900 (JST) ... Wed Oct 28 2015 22:08:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 18:59:03 GMT+0900 (JST) ... Wed Oct 28 2015 20:38:05 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 17:19:02 GMT+0900 (JST) ... Wed Oct 28 2015 18:58:05 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 15:39:02 GMT+0900 (JST) ... Wed Oct 28 2015 17:18:08 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 14:02:12 GMT+0900 (JST) ... Wed Oct 28 2015 15:38:02 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 12:22:02 GMT+0900 (JST) ... Wed Oct 28 2015 14:01:02 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 10:44:02 GMT+0900 (JST) ... Wed Oct 28 2015 12:21:05 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 09:49:02 GMT+0900 (JST) ... Wed Oct 28 2015 10:43:02 GMT+0900 (JST) events.length=55
$
但是当我稍后运行相同的代码时,API会返回意外的事件时间段。
如下。
$ node test_hist.js
DEBUG: start=1446000327271 : Wed Oct 28 2015 11:45:27 GMT+0900 (JST)
DEBUG: end =1446086727271 : Thu Oct 29 2015 11:45:27 GMT+0900 (JST)
DEBUG: path=/api/v0002/historian/types/HARemoteControl/devices/HARemoteControl001?start=1446000327271&end=1446086727271
DEBUG: bookmark=1:iotHistorianSessionId=vbazIN0SDWrJTaep06zTipWS1TqbPMv/9m8EEumw5uuL6Gj6FiWl5z9POLeB5xLv; HttpOnly
DEBUG: Thu Oct 29 2015 08:43:03 GMT+0900 (JST) ... Thu Oct 29 2015 10:22:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Thu Oct 29 2015 07:03:03 GMT+0900 (JST) ... Thu Oct 29 2015 08:42:13 GMT+0900 (JST) events.length=100 more...
DEBUG: Thu Oct 29 2015 05:23:05 GMT+0900 (JST) ... Thu Oct 29 2015 07:02:06 GMT+0900 (JST) events.length=100 more...
DEBUG: Thu Oct 29 2015 03:43:08 GMT+0900 (JST) ... Thu Oct 29 2015 05:22:05 GMT+0900 (JST) events.length=100 more...
DEBUG: Thu Oct 29 2015 02:03:08 GMT+0900 (JST) ... Thu Oct 29 2015 03:42:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Thu Oct 29 2015 00:23:03 GMT+0900 (JST) ... Thu Oct 29 2015 02:02:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 22:43:03 GMT+0900 (JST) ... Thu Oct 29 2015 00:22:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 21:13:08 GMT+0900 (JST) ... Wed Oct 28 2015 22:42:13 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 19:33:03 GMT+0900 (JST) ... Wed Oct 28 2015 21:12:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 17:53:03 GMT+0900 (JST) ... Wed Oct 28 2015 19:32:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 16:13:02 GMT+0900 (JST) ... Wed Oct 28 2015 17:52:03 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 14:33:02 GMT+0900 (JST) ... Wed Oct 28 2015 16:12:05 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 12:56:02 GMT+0900 (JST) ... Wed Oct 28 2015 14:32:02 GMT+0900 (JST) events.length=100 more...
DEBUG: Wed Oct 28 2015 11:16:02 GMT+0900 (JST) ... Wed Oct 28 2015 12:55:02 GMT+0900 (JST) events.length=100 more...
$
节点:
发生此症状后,如果省略了start和end参数,则API会按预期返回最后100个事件。这意味着IoT Foundation在10:23和11:45之间有事件,但在指定end参数时没有返回它们。
$ node test_hist.js -s -e
DEBUG: start=1446000368159 : Wed Oct 28 2015 11:46:08 GMT+0900 (JST)
DEBUG: end =1446086768159 : Thu Oct 29 2015 11:46:08 GMT+0900 (JST)
DEBUG: path=/api/v0002/historian/types/HARemoteControl/devices/HARemoteControl001
DEBUG: Thu Oct 29 2015 10:07:06 GMT+0900 (JST) ... Thu Oct 29 2015 11:46:03 GMT+0900 (JST) events.length=100
$
以上3个执行结果是Bluemix VM(Ubuntu 14.4),在正常和异常结果之间,我在上午10:22在本地Windows PC上运行相同的代码(test_hist.js)。所以在我看来,这个API在某些情况下记住了开始/结束参数,并且用这些记忆值继续替换以下API调用的开始/结束参数。
答案 0 :(得分:0)
我遇到了类似的问题,数据被截断&amp;超时/时间顺序,也可能缺少数据。您可以按照IoTF从以下AJAX查询提供到Google表格中的顺序,查看我在2015-01-01至2015-12-31之间转储数据的页面:
/api/v0002/historian/types/6c84fb90-12c4-11e1-840d-7b25c5ee775a/devices/SX.084.045.224.012?evt_type=report:1&start=1420070400000&end=1451520000000