forked from nightscout/share2nightscout-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
355 lines (327 loc) · 10.6 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
/**
* Author: Ben West
* https://github.com/bewest
* Advisor: Scott Hanselman
* http://www.hanselman.com/blog/BridgingDexcomShareCGMReceiversAndNightscout.aspx
*
* 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 GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* @description: Allows user to store their Dexcom data in their own
* Nightscout server by facilitating the transfer of latest records
* from Dexcom's server into theirs.
*/
var request = require('request');
var qs = require('querystring');
var crypto = require('crypto');
// Defaults
var Defaults = {
"applicationId":"d89443d2-327c-4a6f-89e5-496bbb0317db"
, "agent": "Dexcom Share/3.0.2.11 CFNetwork/711.2.23 Darwin/14.0.0"
, login: 'https://share1.dexcom.com/ShareWebServices/Services/General/LoginPublisherAccountByName'
, accept: 'application/json'
, 'content-type': 'application/json'
, LatestGlucose: "https://share1.dexcom.com/ShareWebServices/Services/Publisher/ReadPublisherLatestGlucoseValues"
// ?sessionID=e59c836f-5aeb-4b95-afa2-39cf2769fede&minutes=1440&maxCount=1"
, nightscout_upload: '/api/v1/entries.json'
, nightscout_battery: '/api/v1/devicestatus.json'
, MIN_PASSPHRASE_LENGTH: 12
};
var DIRECTIONS = {
NONE: 0
, DoubleUp: 1
, SingleUp: 2
, FortyFiveUp: 3
, Flat: 4
, FortyFiveDown: 5
, SingleDown: 6
, DoubleDown: 7
, 'NOT COMPUTABLE': 8
, 'RATE OUT OF RANGE': 9
};
var Trends = (function ( ) {
var keys = Object.keys(DIRECTIONS);
var trends = keys.sort(function (a, b) {
return DIRECTIONS[a] - DIRECTIONS[b];
});
return trends;
})( );
function directionToTrend (direction) {
var trend = 8;
if (direction in DIRECTIONS) {
trend = DIRECTIONS[direction];
}
return trend;
}
function trendToDirection (trend) {
return Trends[trend] || Trends[0];
}
// assemble the POST body for the login endpoint
function login_payload (opts) {
var body = {
"password": opts.password
, "applicationId" : opts.applicationId || Defaults.applicationId
, "accountName": opts.accountName
};
return body;
}
// Login to Dexcom'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 (opts) {
// ?sessionID=e59c836f-5aeb-4b95-afa2-39cf2769fede&minutes=1440&maxCount=1"
var q = {
sessionID: opts.sessionID
, minutes: opts.minutes || 1440
, maxCount: opts.maxCount || 1
};
var url = Defaults.LatestGlucose + '?' + qs.stringify(q);
return url;
}
// Asynchronously fetch data from Dexcom's server.
// Will fetch `minutes` and `maxCount` records.
function fetch (opts, then) {
var url = fetch_query(opts);
var body = "";
var headers = { 'User-Agent': Defaults.agent
, 'Content-Type': Defaults['content-type']
, 'Content-Length': 0
, 'Accept': Defaults.accept };
var req ={ uri: url, body: body, json: true, headers: headers, method: 'POST'
, 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) {
fetch_opts.sessionID = body;
fetch(fetch_opts, function (err, res, glucose) {
then(err, glucose);
});
});
}
// Map Dexcom's property values to Nightscout's.
function dex_to_entry (d) {
/*
[ { DT: '/Date(1426292016000-0700)/',
ST: '/Date(1426295616000)/',
Trend: 4,
Value: 101,
WT: '/Date(1426292039000)/' } ]
*/
var regex = /\((.*)\)/;
var wall = parseInt(d.WT.match(regex)[1]);
var date = new Date(wall);
var entry = {
sgv: d.Value
, date: wall
, dateString: date.toISOString( )
, trend: d.Trend
, direction: trendToDirection(d.Trend)
, device: 'share2'
, type: 'sgv'
};
return entry;
}
// Record data into Nightscout.
function report_to_nightscout (opts, then) {
var shasum = crypto.createHash('sha1');
var hash = shasum.update(opts.API_SECRET);
var headers = { 'api-secret': shasum.digest('hex')
, 'Content-Type': Defaults['content-type']
, 'Accept': Defaults.accept };
var url = opts.endpoint + Defaults.nightscout_upload;
var req = { uri: url, body: opts.entries, json: true, headers: headers, method: 'POST'
, rejectUnauthorized: false };
return request(req, then);
}
function nullify_battery_status (opts, then) {
var shasum = crypto.createHash('sha1');
var hash = shasum.update(opts.API_SECRET);
var headers = { 'api-secret': shasum.digest('hex')
, 'Content-Type': Defaults['content-type']
, 'Accept': Defaults.accept };
var url = opts.endpoint + Defaults.nightscout_battery;
var body = { uploaderBattery: false };
var req = { uri: url, body: body, 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;
fetch(fetch_opts, function (err, res, glucose) {
if (res.statusCode < 400) {
to_nightscout(glucose);
} else {
my.sessionID = null;
refresh_token( );
}
});
} 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 = body;
failures = 0;
my( );
} else {
failures++;
console.log("Error refreshing token", err, res.statusCode, body);
if (failures >= opts.maxFailures) {
throw "Too many login failures, check DEXCOM_ACCOUNT_NAME and DEXCOM_PASSWORD";
}
}
});
}
function to_nightscout (glucose) {
var ns_config = Object.create(opts.nightscout);
if (glucose) {
if (runs === 0) {
nullify_battery_status(ns_config, function (err, resp) {
if (err) {
console.warn('Problem reporting battery', arguments);
} else {
console.log('Battery status hidden');
}
});
}
runs++;
// Translate to Nightscout data.
var entries = glucose.map(dex_to_entry);
console.log('Entries', entries);
if (ns_config.endpoint) {
ns_config.entries = entries;
// Send data to Nightscout.
report_to_nightscout(ns_config, function (err, response, body) {
console.log("Nightscout 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);
}
if (readENV('DEXCOM_ACCOUNT_NAME', '@').match(/\@/)) {
var msg = [ "environment variable"
, "DEXCOM_ACCOUNT_NAME should be"
, "Dexcom Share user name, not an email address"];
var err = new Error(msg.join(' '));
throw err;
process.exit(1);
}
var args = process.argv.slice(2);
var config = {
accountName: readENV('DEXCOM_ACCOUNT_NAME')
, password: readENV('DEXCOM_PASSWORD')
};
var ns_config = {
API_SECRET: readENV('API_SECRET')
, endpoint: readENV('NS', 'https://' + 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'));
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 Dexcom.
do_everything(meta, function (err, glucose) {
console.log('From Dexcom', err, glucose);
if (glucose) {
// Translate to Nightscout data.
var entries = glucose.map(dex_to_entry);
console.log('Entries', entries);
if (ns_config.endpoint) {
ns_config.entries = entries;
// 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;
}
}