-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
443 lines (385 loc) · 13.8 KB
/
index.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
* Author: Jeremy Pollock
* https://github.com/jpollock
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the MIT License
* along with this program. .
*
*
* @description: Allows user to store their Glooko data in their own
* Nightscout server by facilitating the transfer of latest records
* from Glooko's server into theirs.
*/
var request = require('request');
var qs = require('querystring');
var crypto = require('crypto');
var PubNub = require('pubnub');
var moment = require('moment-timezone');
// Defaults
var server = "api.glooko.com"
var bridge = readENV('GLOOKO_SERVER')
if (bridge && bridge.indexOf(".") > 1) {
server = bridge;
}
else if (bridge && bridge === 'EU') {
server = "api.glooko.com";
} else {
server = "api.glooko.com";
}
var Defaults = {
"applicationId":"d89443d2-327c-4a6f-89e5-496bbb0317db"
, "lastGuid":"1e0c094e-1e54-4a4f-8e6a-f94484b53789" // hardcoded, random guid; no Glooko docs to explain need for param or why bad data works
, login: 'https://' + server + '/api/v2/users/sign_in'
, accept: 'application/json'
, 'content-type': 'application/json'
, LatestFoods: 'https://' + server + '/api/v2/foods'
, LatestInsulins: 'https://' + server + '/api/v2/insulins'
// ?sessionID=e59c836f-5aeb-4b95-afa2-39cf2769fede&minutes=1440&maxCount=1"
, nightscout_upload: '/api/v1/treatments.json'
, MIN_PASSPHRASE_LENGTH: 12
};
// assemble the POST body for the login endpoint
function login_payload (opts) {
var body = {
"userLogin": {
"email": opts.accountName,
"password": opts.password
},
"deviceInformation": {
"deviceModel": "iPhone"
}
};
return body;
}
// Login to Glooko's server.
function authorize (opts, then) {
var url = Defaults.login;
var body = login_payload(opts);
var headers = { 'User-Agent': Defaults.agent
, 'Content-Type': Defaults['content-type']
, 'Accept': Defaults.accept };
var req ={ uri: url, body: body, json: true, headers: headers, method: 'POST'
, rejectUnauthorized: false };
// Asynchronously calls the `then` function when the request's I/O
// is done.
return request(req, then);
}
// Assemble query string for fetching data.
function fetch_query (url, opts) {
// ?sessionID=e59c836f-5aeb-4b95-afa2-39cf2769fede&minutes=1440&maxCount=1"
var q = {
//lastUpdatedAt: opts.lastUpdatedAt
//,
lastGuid: Defaults.lastGuid
, sendSoftDeleted: opts.sendSoftDeleted || false
, limit: opts.maxCount || 1000
};
url += '?lastUpdatedAt=' + opts.lastUpdatedAt + '&' + qs.stringify(q);
console.log(url);
return url;
}
// Asynchronously fetch data from Dexcom's server.
// Will fetch `minutes` and `maxCount` records.
function fetch (url, opts, then) {
var url = fetch_query(url, opts);
var headers = { 'User-Agent': Defaults.agent
, 'Content-Type': Defaults['content-type']
, 'Content-Length': 0
, 'Accept': Defaults.accept
, 'Cookie': opts.sessionID };
var req ={ uri: url, json: true, headers: headers, method: 'GET'
, rejectUnauthorized: false };
return request(req, then);
}
// Authenticate and fetch data from Dexcom.
function do_everything (opts, then) {
var login_opts = opts.login;
var fetch_opts = opts.fetch;
authorize(login_opts, function (err, res, body) {
var arr = {};
fetch_opts.sessionID = res.headers['set-cookie'][0];
var d_now = Date.now();
var d_then = new Date(d_now - 600*60000)
//console.log(d_then);
//var fetch_opts = Object.create(opts.fetch);
fetch_opts.lastUpdatedAt = d_then.toISOString();
fetch(Defaults.LatestFoods, fetch_opts, function (err, res, foods) {
fetch(Defaults.LatestInsulins, fetch_opts, function (err, res, insulins) {
arr['foods'] = foods;
arr['insulins'] = insulins;
console.log("Foods: " + foods.length + " Insulins:" + insulins.length);
then(err, arr);
});
});
});
}
function generate_nightscout_treatments(entries, then) {
// Snack Bolus
// Meal Bolus
// BG Check
// Correction Bolus
// Carb Correction
var foods = entries['foods']['foods']; //ugh
var insulins = entries['insulins']['insulins'];
var treatments = []
if (foods) {
foods.forEach(function(element) {
var treatment = {};
//console.log(element);
var f_date = new Date(element.timestamp);
var f_s_date = new Date(f_date.getTime() - 45*60000);
var f_e_date = new Date(f_date.getTime() + 45*60000);
var now = moment(f_date); //todays date
var end = moment(f_s_date); // another date
var duration = moment.duration(now.diff(end));
var minutes = duration.asMinutes();
var i_date = new Date();
var result = insulins.filter(function(el) {
i_date = new Date(el.timestamp);
var i_moment = moment(i_date);
var duration = moment.duration(now.diff(i_moment));
var minutes = duration.asMinutes();
return Math.abs(minutes) < 46;
})
insulin = result[0];
if (insulin != undefined) {
var i_date = moment(insulin.timestamp);
treatment.eventType = 'Meal Bolus';
treatment.eventTime = new Date(i_date + 420*60000).toISOString( );
//treatment.eventTime = new Date(i_date).toISOString( );
//treatment.eventTime = i_date.toISOString( );
treatment.insulin = insulin.value;
treatment.preBolus = moment.duration(moment(f_date).diff(moment(i_date))).asMinutes();
} else {
var f_date = moment(element.timestamp);
treatment.eventType = 'Carb Correction';
treatment.eventTime = new Date(f_date + 420*60000).toISOString( );
//treatment.eventTime = new Date(f_date).toISOString( );
//treatment.eventTime = f_date.toISOString( );
}
treatment.carbs = element.carbs;
treatment.notes = JSON.stringify(element);
treatments.push(treatment);
});
}
if (insulins) {
insulins.forEach(function(element) {
var treatment = {};
//console.log(element);
var f_date = new Date(element.timestamp);
var f_s_date = new Date(f_date.getTime() - 45*60000);
var f_e_date = new Date(f_date.getTime() + 45*60000);
var now = moment(f_date); //todays date
var end = moment(f_s_date); // another date
var duration = moment.duration(now.diff(end));
var minutes = duration.asMinutes();
var i_date = new Date();
var result = foods.filter(function(el) {
i_date = new Date(el.timestamp);
var i_moment = moment(i_date);
var duration = moment.duration(now.diff(i_moment));
var minutes = duration.asMinutes();
return Math.abs(minutes) < 46;
})
//console.log(result);
if (result[0] == undefined) {
var f_date = moment(element.timestamp);
treatment.eventType = 'Correction Bolus';
treatment.eventTime = new Date(f_date + 420*60000).toISOString( );
treatment.insulin = element.value;
//treatment.eventTime = f_date.toISOString( );
treatments.push(treatment);
}
});
}
then(err, treatments);
}
// Record data into Nightscout.
function report_to_nightscout (opts, then) {
var shasum = crypto.createHash('sha1');
var hash = shasum.update(opts.API_SECRET);
//console.log(shasum.digest('hex'));
var headers = { 'api-secret': shasum.digest('hex')
, 'Content-Type': Defaults['content-type']
, 'Accept': Defaults.accept };
var url = opts.endpoint + Defaults.nightscout_upload;
console.log(url);
var req = { uri: url, body: opts.treatments, json: true, headers: headers, method: 'POST'
, rejectUnauthorized: false };
return request(req, then);
}
function engine (opts) {
var runs = 0;
var failures = 0;
function my ( ) {
console.log('RUNNING', runs, 'failures', failures);
if (my.sessionID) {
var fetch_opts = Object.create(opts.fetch);
if (runs === 0) {
console.log('First run, fetching', opts.firstFetchCount);
fetch_opts.maxCount = opts.firstFetchCount;
}
fetch_opts.sessionID = my.sessionID;
var now = Date.now();
var then = new Date(now - 600*60000)
console.log(then);
//var fetch_opts = Object.create(opts.fetch);
fetch_opts.lastUpdatedAt = then.toISOString();
var arr = {};
fetch(Defaults.LatestFoods, fetch_opts, function (err, res, foods) {
fetch(Defaults.LatestInsulins, fetch_opts, function (err, res, insulins) {
arr['foods'] = foods;
arr['insulins'] = insulins;
//console.log(arr);
to_nightscout(arr);
});
});
} else {
failures++;
refresh_token( );
}
}
function refresh_token ( ) {
console.log('Fetching new token');
authorize(opts.login, function (err, res, body) {
if (!err && body && res.statusCode == 200) {
my.sessionID = res.headers['set-cookie'][0];
failures = 0;
my( );
} else {
failures++;
console.log("Error refreshing token", err, res.statusCode, body);
if (failures >= opts.maxFailures) {
throw "Too many login failures, check GLOOKO_ACCOUNT_NAME and GLOOKO_PASSWORD";
}
}
});
}
function to_nightscout (entries) {
var ns_config = Object.create(opts.nightscout);
if (entries) {
generate_nightscout_treatments(entries, function(err, treatments) {
//var entries = glucose.map(dex_to_entry);
console.log('TREATMENTS='+treatments);
//console.log(opts.callback);
if (opts && opts.callback && opts.callback.call) {
opts.callback(null, treatments);
}
if (ns_config.endpoint) {
ns_config.treatments = treatments;
// Send data to Nightscout.
report_to_nightscout(ns_config, function (err, response, body) {
console.log("Nightscout Glooko upload", 'error', err, 'status', response.statusCode, body);
});
}
});
}
}
my( );
return my;
}
// Provide public, testable API
engine.fetch = fetch;
engine.authorize = authorize;
engine.authorize_fetch = do_everything;
module.exports = engine;
function readENV(varName, defaultValue) {
//for some reason Azure uses this prefix, maybe there is a good reason
var value = process.env['CUSTOMCONNSTR_' + varName]
|| process.env['CUSTOMCONNSTR_' + varName.toLowerCase()]
|| process.env[varName]
|| process.env[varName.toLowerCase()];
return value || defaultValue;
}
// If run from commandline, run the whole program.
if (!module.parent) {
if (readENV('API_SECRET').length < Defaults.MIN_PASSPHRASE_LENGTH) {
var msg = [ "API_SECRET environment variable should be at least"
, Defaults.MIN_PASSPHRASE_LENGTH, "characters" ];
var err = new Error(msg.join(' '));
throw err;
process.exit(1);
}
var args = process.argv.slice(2);
var config = {
accountName: readENV('GLOOKO_ACCOUNT_NAME')
, password: readENV('GLOOKO_PASSWORD')
};
var ns_config = {
API_SECRET: readENV('API_SECRET')
, endpoint: readENV('NS', 'http://' + readENV('WEBSITE_HOSTNAME'))
};
var interval = readENV('SHARE_INTERVAL', 60000 * 2.5);
var fetch_config = { maxCount: readENV('maxCount', 1)
, minutes: readENV('minutes', 1440)
};
var meta = {
login: config
, fetch: fetch_config
, nightscout: ns_config
, maxFailures: readENV('maxFailures', 3)
, firstFetchCount: readENV('firstFetchCount', 3)
};
switch (args[0]) {
case 'login':
//authorize(config, console.log.bind(console, 'login'));
authorize(config, function (err, res, body) {
if (!err && body && res.statusCode == 200) {
console.log(res.headers['set-cookie'][0]);
//console.log("Success refreshing token", err, res.statusCode, body);
} else {
console.log("Error refreshing token", err, res.statusCode, body);
}
});
/*authorize(config, function (err, res) {
console.log(err);
});*/
break;
case 'fetch':
config = { sessionID: args[1] };
fetch(config, console.log.bind(console, 'fetched'));
break;
case 'testdaemon':
setInterval(engine(meta), 2500);
break;
case 'run':
// Authorize and fetch from Glooko
var now = Date.now();
var then = new Date(now - 7200*60000)
//var fetch_opts = Object.create(opts.fetch);
meta.fetch.lastUpdatedAt = then.toISOString();
do_everything(meta, function (err, entries) {
//console.log('Entries', entries['foods']);
if (entries) {
// Translate to Nightscout data.
generate_nightscout_treatments(entries, function(err, treatments) {
//var entries = glucose.map(dex_to_entry);
//console.log(ns_config);
if (ns_config.endpoint) {
ns_config.treatments = treatments;
console.log(treatments);
// Send data to Nightscout.
report_to_nightscout(ns_config, function (err, response, body) {
console.log("Nightscout upload", 'error', err, 'status', response.statusCode, body);
});
}
});
}
});
break;
default:
setInterval(engine(meta), interval);
break;
break;
}
}