-
Notifications
You must be signed in to change notification settings - Fork 0
/
opensensors.js
187 lines (166 loc) · 6.99 KB
/
opensensors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var Promise = require("bluebird");
var bhttp = Promise.promisifyAll(require("bhttp"));
var fs = Promise.promisifyAll(require("fs"));
var extend = require('xtend');
// config encapsulates opensensors-api-key
// valid keys for config are: api-key (required)
module.exports = function(config) {
var API_POST_OPTIONS = {
headers: {
Accept: "application/json",
Authorization: "api-key " + config["api-key"]
}
};
var API_BASE_URL = "https://api.opensensors.io";
// helper (actually workhorse) method that does a GET to a URL
// it appends the augmented payloads in the response to the second argument that gets passed to it
// if the response body JSON contains a next element it recursively calls itself
var getUntilNot400 = function(url){
var theUrl = url;
return Promise.try(function(){
return bhttp.get(theUrl, API_POST_OPTIONS);
}).then(function(response){
if(response.statusCode == 400){
console.log("Got 400, waiting 30 seconds before trying again");
return Promise.delay(30000).then(function(){
return getUntilNot400(theUrl);
});
}
else{
return response;
}
});
};
var recursiveGET = function(url, results, status, followNext){
console.log("Current Num Results: " + results.length + " -> URL: " + url);
return Promise.try(function(){
return getUntilNot400(url);
}).catch(function(err){
console.error(err);
}).then(function(response){
// if there's a non-null status object provided
// lets reach into the status.filename
// and modify the entry for status.serialnumber
if(status && status.filename) {
return Promise.try(function () {
return fs.readFileAsync(status.filename, 'utf8');
}).then(function(content) {
return Promise.try(function () {
if(content == ""){
content = "{}";
}
var json = JSON.parse(content);
if (!json[status.serialNumber]) {
json[status.serialNumber] = {};
}
if(response.body.messages) {
json[status.serialNumber].numResults = results.length + response.body.messages.length;
}
else{
json[status.serialNumber].complete = true;
json[status.serialNumber].error = true;
json[status.serialNumber].errorMessage = "No messages found.";
}
if(results.length > 0) {
json[status.serialNumber].timestamp = results[results.length - 1].timestamp;
}
if(!response.body.next){
json[status.serialNumber].complete = true;
}
else{
json[status.serialNumber].complete = false;
}
return json;
}).catch(function () {
return null;
}).then(function (json) {
if(json) {
return fs.writeFileAsync(status.filename, JSON.stringify(json));
}
else{
return null;
}
});
}).then(function(){
return response;
});
}
else {
return response; // pass it through
}
}).then(function(response){
var augmentedPayloads = [];
if(response.body.messages){
augmentedPayloads = response.body.messages.map(function(msg){
// as it turns out nan is not valid JSON
var body = msg.payload.text.replace(/nan/g, 'null');
var datum = JSON.parse(body);
datum.timestamp = msg.date;
datum.topic = msg.topic;
return datum;
});
}
return Promise.try(function(){
return results.concat(augmentedPayloads);
}).then(function(newResults){
if(followNext && response.body.next){
return recursiveGET(API_BASE_URL + response.body.next, newResults, status, followNext);
}
else{
return newResults;
}
});
});
};
// this function returns a string to append to a url path
// to add the [flat] params object as a querystring
function urlParams(params){
var ret = "";
if(Object.keys(params).length > 0){ // if there are any optional params
ret += '?';
var encodeParams = Object.keys(params).map(function(key){
if(key != "status") { // special case, not an OpenSensors parameter
return key + '=' + params[key];
}
});
ret += encodeParams.join('&');
}
return ret;
}
// this function returns a string to append to a url path
// to add the [flat] params object as a querystring
function collectMessagesBy(x, val, params){
var API_MESSAGES_BY_PATH = "/v1/messages/" + x;
var url = API_BASE_URL + API_MESSAGES_BY_PATH;
if(!val){
console.error(x + "is required");
return Promise.resolve({});
}
url += "/" + val+ urlParams(params);
var status = params ? extend(params.status) : null;
return recursiveGET(url, [], status);
}
// returns an array of message payloads from the API, augmented with timestamp
// valid optional param keys are "start-date", "end-date", and "dur"
function collectMessagesByDevice(device, params){
return collectMessagesBy("device", device, params);
}
// returns an array of message payloads from the API, augmented with timestamp
// valid optional param keys are "start-date", "end-date", and "dur"
function collectMessagesByTopic(topic, params){
return collectMessagesBy("topic", topic, params);
}
// returns an array of message payloads from the API, augmented with timestamp
// valid optional param keys are "start-date", "end-date", and "dur"
function collectMessagesByUser(user, params){
return collectMessagesBy("user", user, params);
}
// this is what require(opensensors)(config) actually will return
return {
messages: {
byDevice: collectMessagesByDevice,
byTopic: collectMessagesByTopic,
byUser: collectMessagesByUser
}
};
};