forked from xdrip-js/Lookout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncNS.js
666 lines (519 loc) · 17.9 KB
/
syncNS.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
const moment = require('moment');
const Debug = require('debug');
const log = Debug('syncNS:log');
const error = Debug('syncNS:error');
const debug = Debug('syncNS:debug');
const _ = require('lodash');
const TimeLimitedPromise = require('./timeLimitedPromise');
const xDripAPS = require('./xDripAPS')();
const RIG_SOURCE = 0;
const NS_SOURCE = 1;
let options = null;
let storage = null;
let transmitter = null;
const syncCal = async (sensorStart) => {
let rigCal = null;
let NSCal = null;
let nsQueryError = false;
let rigCalStr = null;
log('syncCal started');
NSCal = await xDripAPS.latestCal()
.catch((err) => {
error(`Error getting NS calibration: ${err}`);
nsQueryError = true;
});
if (nsQueryError) {
return;
}
if (NSCal) {
debug(`SyncNS NS Cal - date: ${moment(NSCal.date).format()} slope: ${Math.round(NSCal.slope * 100) / 100} intercept: ${Math.round(NSCal.intercept * 10) / 10}`);
}
await storage.lock();
// Always synchronize only the transmitter calibration
// The expired cal is always able to be calculated
// form the BG Checks and glucose values in NS
rigCalStr = 'g5Calibration';
rigCal = await storage.getItem(rigCalStr)
.catch((err) => {
error(`Error getting rig calibration: ${err}`);
});
if (rigCal) {
debug(`SyncNS Rig Cal - date: ${moment(rigCal.date).format()} slope: ${Math.round(rigCal.slope * 100) / 100} intercept: ${Math.round(rigCal.intercept * 10) / 10}`);
}
if (NSCal) {
if (!rigCal) {
debug('No rig calibration, storing NS calibration');
if (sensorStart && sensorStart.diff(moment(NSCal.date)) > 0) {
debug('Found sensor start after latest NS calibration. Not updating local rig calibration');
} else {
await storage.setItem(rigCalStr, NSCal)
.catch(() => {
error('Unable to store NS Calibration');
});
}
} else if (rigCal && (rigCal.date < NSCal.date)) {
debug(`NS calibration more recent than rig calibration NS Cal Date: ${NSCal.date} Rig Cal Date: ${rigCal.date}`);
storage.setItem(rigCalStr, NSCal)
.catch(() => {
error('Unable to store NS Calibration');
});
} else if (rigCal && (rigCal.date > NSCal.date)) {
debug(`Rig calibration more recent than NS calibration NS Cal Date: ${NSCal.date} Rig Cal Date: ${rigCal.date}`);
debug('Upoading rig calibration');
xDripAPS.postCalibration(rigCal);
} else {
debug('Rig and NS calibration dates match - no sync needed');
}
} else if (rigCal) {
debug('No NS calibration - uploading rig calibration');
xDripAPS.postCalibration(rigCal);
} else {
debug('No rig or NS calibration');
}
storage.unlock();
log('syncCal complete');
};
const syncEvent = async (itemName, eventType) => {
let rigItem = null;
let nsEvent = null;
let nsQueryError = null;
log(`Syncing rig ${itemName} and NS ${eventType} started`);
nsEvent = await xDripAPS.latestEvent(eventType)
.catch((err) => {
nsQueryError = err;
});
if (nsQueryError) {
throw new Error(`NS Query Error syncing ${eventType}: ${nsQueryError}`);
}
if (nsEvent) {
debug(`SyncNS NS ${eventType}- date: ${nsEvent.date.format()}`);
}
await storage.lock();
rigItem = await storage.getEvent(itemName)
.catch((err) => {
error(`Error getting rig ${itemName}: ${err}`);
});
if (rigItem) {
debug(`SyncNS Rig ${itemName}- date: ${rigItem.date.format()}`);
}
const latestEvent = {
event: rigItem,
source: RIG_SOURCE,
};
if (nsEvent) {
if (!rigItem) {
debug(`No rig ${itemName}, storing NS ${eventType}`);
latestEvent.event = nsEvent;
latestEvent.source = NS_SOURCE;
await storage.setEvent(itemName, nsEvent)
.catch(() => {
error(`Unable to store ${itemName}`);
});
} else if (rigItem && ((nsEvent.date - rigItem.date) > 1000)) {
debug(`NS ${eventType} more recent than rig ${itemName} NS date: ${nsEvent.date.format()} Rig date: ${rigItem.date.format()}`);
latestEvent.event = nsEvent;
latestEvent.source = NS_SOURCE;
storage.setEvent(itemName, nsEvent)
.catch(() => {
error(`Unable to store ${itemName}`);
});
} else if (rigItem && ((rigItem.date - nsEvent.date) > 1000)) {
debug(`Rig ${itemName} more recent than NS ${eventType} NS date: ${nsEvent.date.format()} Rig date: ${rigItem.date.format()}`);
debug(`Uploading rig ${itemName}`);
latestEvent.event = rigItem;
latestEvent.source = RIG_SOURCE;
xDripAPS.postEvent(eventType, rigItem.date, rigItem.notes);
} else {
debug(`Rig and NS ${eventType} dates match - no sync needed`);
}
} else if (rigItem) {
debug(`No NS ${eventType} - uploading rig ${itemName}`);
latestEvent.event = rigItem;
latestEvent.source = RIG_SOURCE;
xDripAPS.postEvent(eventType, rigItem.date, rigItem.notes);
} else {
debug(`No rig ${itemName} or NS ${eventType}`);
}
storage.unlock();
log(`Syncing rig ${itemName} and NS ${eventType} complete`);
if (latestEvent) {
latestEvent.date = moment(latestEvent.event.date);
}
return latestEvent;
};
const syncSGVs = async () => {
let rigSGVs = null;
let nsSGVs = null;
log('syncSGVs started');
await storage.lock();
rigSGVs = await storage.getArray('glucoseHist')
.catch((err) => {
error(`Error getting rig SGVs: ${err}`);
});
// make sure they all have readDateMills for easy math
for (let i = 0; i < rigSGVs; i += 1) {
rigSGVs[i].readDateMills = moment(rigSGVs[i].readDate).valueOf();
}
const minDate = moment().subtract(24, 'hours').valueOf();
// remote items older than 24 hours
rigSGVs = rigSGVs.filter(sgv => sgv.readDateMills >= minDate);
// get the list of which SGVs we have
// that haven't been verified to be in NS
const nsMisses = rigSGVs.filter(sgv => !sgv.inNS);
const nsGaps = [];
// Assemble the list of overall gaps that account
// for consecutive misses as one gap to minimize
// the number of NS queries
if (nsMisses.length > 0) {
let gapStart = nsMisses[0].readDateMills;
let prevTime = nsMisses[0].readDateMills;
let gapSGVs = [nsMisses[0]];
for (let i = 1; i < nsMisses.length; i += 1) {
const gap = { gapStart: moment(gapStart), gapEnd: moment(prevTime), gapSGVs };
if ((nsMisses[i].readDateMills - prevTime) > 6 * 60000) {
nsGaps.push(gap);
gapStart = nsMisses[i].readDateMills;
gapSGVs = [nsMisses[i]];
} else {
gapSGVs.push(nsMisses[i]);
}
prevTime = nsMisses[i].readDateMills;
}
if (gapSGVs.length > 0) {
nsGaps.push({ gapStart: moment(gapStart), gapEnd: moment(prevTime), gapSGVs });
}
}
debug('nsGaps: ');
_.each(nsGaps, (gap) => {
debug(` gapStart: ${moment(gap.gapStart).format()} gapEnd: ${moment(gap.gapEnd).format()}`);
});
await Promise.all(_.map(nsGaps, async (nsGap) => {
let nsQueryError = false;
// get the NS entries that are in the gap
nsSGVs = await xDripAPS.SGVsBetween(
nsGap.gapStart, nsGap.gapEnd,
Math.round((nsGap.gapEnd.valueOf() - nsGap.gapStart.valueOf()) * 2 / 5 * 60000) + 1,
).catch((err) => {
error(`Unable to get NS SGVs to match unfiltered with BG Check: ${err}`);
nsQueryError = true;
});
if (!nsSGVs) {
nsSGVs = [];
}
// if the ns query failed, just bail out of this gap
if (nsQueryError) {
return;
}
// give them all a dateMills to make comparison's easier
for (let i = 0; i < nsSGVs.length; i += 1) {
nsSGVs[i].dateMills = moment(nsSGVs[i].date).valueOf();
}
nsSGVs = _.sortBy(nsSGVs, ['dateMills']);
// mark any matches we have so we don't re-upload them
_.each(nsSGVs, (nsSGV) => {
const matches = nsGap.gapSGVs.filter(
sgv => Math.abs(sgv.readDateMills - nsSGV.dateMills) < 60000,
);
if (matches.length > 0) {
matches[0].inNS = true;
}
});
// upload any gapSGVs to NS that we haven't found a NS match
_.each(nsGap.gapSGVs, (gapSGV) => {
if (gapSGV.glucose && !gapSGV.inNS) {
xDripAPS.post(gapSGV, false, true);
}
});
}));
let rigGaps = null;
if (transmitter) {
rigGaps = transmitter.sgvGaps(rigSGVs);
}
debug('rigGaps:\n%O', rigGaps);
await Promise.all(_.map(rigGaps, async (gap) => {
nsSGVs = await xDripAPS.SGVsBetween(
gap.gapStart, gap.gapEnd,
Math.round((gap.gapEnd.valueOf() - gap.gapStart.valueOf()) / 5 * 60000) + 1,
).catch((err) => {
error(`Unable to get NS SGVs to match unfiltered with BG Check: ${err}`);
});
if (!nsSGVs) {
nsSGVs = [];
}
for (let i = 0; i < nsSGVs.length; i += 1) {
nsSGVs[i].dateMills = moment(nsSGVs[i].date).valueOf();
}
nsSGVs = _.sortBy(nsSGVs, ['dateMills']);
_.each(nsSGVs, (nsSGV) => {
const rigSGV = {
readDate: nsSGV.dateString,
readDateMills: nsSGV.dateMills,
filtered: nsSGV.filtered,
unfiltered: nsSGV.unfiltered,
glucose: nsSGV.sgv,
nsNoise: nsSGV.noise,
trend: nsSGV.trend,
state: 0x00, // Set state to None
g5calibrated: false,
inNS: true,
};
rigSGVs.push(rigSGV);
});
}));
rigSGVs = _.sortBy(rigSGVs, ['readDateMills']);
await storage.setItem('glucoseHist', rigSGVs)
.catch((err) => {
error(`Unable to store glucoseHist: ${err}`);
});
storage.unlock();
log('syncSGVs complete');
return ((rigSGVs.length > 0) && rigSGVs[rigSGVs.length - 1]) || null;
};
const syncBGChecks = async (sensorStart, sensorStop) => {
let NSBGChecks = null;
let nsQueryError = false;
const bgChecksFromNS = [];
let sliceStart = 0;
let validBGCheckStartTime = sensorStart;
log('syncBGChecks started');
if (!sensorStart || (sensorStop && sensorStop.valueOf() > sensorStart.valueOf())) {
validBGCheckStartTime = sensorStop;
}
debug(`NS Query for BG Checks since: ${validBGCheckStartTime}`);
NSBGChecks = await xDripAPS.BGChecksSince(validBGCheckStartTime)
.catch((err) => {
// Bail out since we can't sync if we don't have NS access
error(`Error getting NS BG Checks: ${err}`);
nsQueryError = true;
});
if (nsQueryError) {
return null;
}
if (!NSBGChecks) {
NSBGChecks = [];
}
debug(`SyncNS NS BG Checks: ${NSBGChecks.length}`);
for (let i = 0; i < NSBGChecks.length; i += 1) {
const timeVal = moment(NSBGChecks[i].created_at);
NSBGChecks[i].created_at = timeVal.format();
NSBGChecks[i].dateMills = timeVal.valueOf();
}
NSBGChecks = _.sortBy(NSBGChecks, ['dateMills']);
sliceStart = 0;
for (let i = 0; i < NSBGChecks.length; i += 1) {
if (moment(NSBGChecks[i].created_at).diff(validBGCheckStartTime) < 0) {
sliceStart = i + 1;
}
}
NSBGChecks = NSBGChecks.slice(sliceStart);
if (NSBGChecks.length > 0) {
const bgCheck = NSBGChecks[NSBGChecks.length - 1];
debug(`Most recent NS BG Check - date: ${bgCheck.created_at} type: ${bgCheck.glucoseType} glucose: ${bgCheck.glucose}`);
}
await storage.lock();
let rigBGChecks = await storage.getArray('bgChecks')
.catch((err) => {
error(`Error getting bgChecks: ${err}`);
});
for (let i = 0; i < rigBGChecks.length; i += 1) {
rigBGChecks[i].dateMills = moment(rigBGChecks[i].date).valueOf();
}
const rigDataLength = rigBGChecks.length;
if (rigDataLength > 0) {
const bgCheck = rigBGChecks[rigDataLength - 1];
debug(`Most recent Rig BG Check - date: ${moment(bgCheck.date).format()} glucose: ${bgCheck.glucose} unfiltered: ${bgCheck.unfiltered}`);
}
for (let i = 0; i < NSBGChecks.length; i += 1) {
const nsValue = NSBGChecks[i];
let rigValue = null;
let rigIndex = 0;
for (; rigIndex < rigDataLength; rigIndex += 1) {
const timeDiff = nsValue.dateMills - rigBGChecks[rigIndex].dateMills;
if (Math.abs(timeDiff) < 10 * 1000) {
rigValue = rigBGChecks[rigIndex];
break;
} else if (timeDiff < 0) {
// Bail if rigBGChecks time is later than NS BG time
break;
}
}
if (!rigValue) {
rigValue = {
date: moment(nsValue.created_at).valueOf(),
dateMills: nsValue.dateMills,
glucose: nsValue.glucose,
type: 'NS',
};
rigBGChecks.push(rigValue);
// we found a new BG check
bgChecksFromNS.push(rigValue);
}
}
rigBGChecks = _.sortBy(rigBGChecks, ['dateMills']);
sliceStart = 0;
// Remove any cal data we have
// that predates the last sensor start
for (let i = 0; i < rigBGChecks.length; i += 1) {
if (rigBGChecks[i].dateMills < validBGCheckStartTime.valueOf()) {
sliceStart = i + 1;
}
}
rigBGChecks = rigBGChecks.slice(sliceStart);
const bgIndexes = [];
const promises = [];
// try to fill in any missing unfiltered values
for (let i = 0; i < rigBGChecks.length; i += 1) {
if (transmitter
&& (!('unfiltered' in rigBGChecks[i])
|| !rigBGChecks[i].unfiltered
|| !('filtered' in rigBGChecks[i])
|| !rigBGChecks[i].filtered)) {
bgIndexes.push(i);
promises.push(transmitter.getUnfiltered(moment(rigBGChecks[i].dateMills)));
}
}
const results = await Promise.all(promises);
for (let i = 0; i < results.length; i += 1) {
if (results[i]) {
rigBGChecks[bgIndexes[i]].unfiltered = results[i].unfiltered;
rigBGChecks[bgIndexes[i]].filtered = results[i].filtered;
}
}
await storage.setItem('bgChecks', rigBGChecks)
.catch((err) => {
debug(`Unable to store bgChecks: ${err}`);
});
storage.unlock();
let nsIndex = 0;
for (let rigIndex = 0; rigIndex < rigBGChecks.length; rigIndex += 1) {
const rigValue = rigBGChecks[rigIndex];
let nsValue = null;
for (; nsIndex < NSBGChecks.length; nsIndex += 1) {
const timeDiff = NSBGChecks[nsIndex].dateMills - rigValue.dateMills;
if (Math.abs(timeDiff) < 10 * 1000) {
nsValue = NSBGChecks[nsIndex];
break;
} else if (timeDiff > 0) {
// bail out if NS BG Check is later in time than rig value
break;
}
}
if (!nsValue) {
xDripAPS.postBGCheck(rigValue);
}
}
if (transmitter) {
transmitter.sendBgChecksToTxmitter(bgChecksFromNS);
}
log('syncBGChecks complete');
return rigBGChecks;
};
const calcNextSyncTimeDelay = (sgv) => {
if (!sgv) {
// If we don't have a glucose value, just return 5 minutes
return 5 * 60000;
}
let sgvTime = sgv.readDateMills;
const now = moment().valueOf();
// If lazy upload is enabled, sync 30 seconds earlier
// so the prumary rig and the delayed upload rig
// don't try to upload the same missing data
const preDelta = options.read_only ? 60000 : 30000;
// Find the next point in time where
// 30 seconds less than the next possible
// transmitter wake up time is later than now
while ((sgvTime - preDelta) < now) {
sgvTime += 5 * 60000;
}
// Return the amount of time in milliseconds between
// now and 30 seconds before the next wake up time
return (sgvTime - preDelta - now);
};
const syncNS = async (options_, storage_, transmitter_) => {
let sensorInsert = null;
let sensorStart = null;
let sensorStop = null;
let latestSGV = null;
let bgChecks = null;
let nsQueryError = false;
log(
'\n====================================\n'
+ 'syncNS started'
+ '\n====================================',
);
storage = storage_;
transmitter = transmitter_;
options = options_;
sensorInsert = await syncEvent('sensorInsert', 'Sensor Change')
.catch((err) => {
log(err);
});
sensorStart = await syncEvent('sensorStart', 'Sensor Start')
.catch((err) => {
log(err);
nsQueryError = true;
});
if (!sensorInsert || (sensorStart && (sensorStart.event.date > sensorInsert.event.date))) {
sensorInsert = sensorStart;
}
sensorStop = await syncEvent('sensorStop', 'Sensor Stop')
.catch((err) => {
log(err);
nsQueryError = true;
});
if (nsQueryError) {
log(
'\n====================================\n'
+ 'syncNS - Error querying NS - Setting 5 minute timer to try again'
+ '\n====================================',
);
setTimeout(() => {
// Restart the syncNS after 5 minute
syncNS(options, storage, transmitter);
}, 5 * 60000);
return;
}
if (sensorStart
&& sensorStart.source === NS_SOURCE
&& (Date.now() - sensorStart.event.date) < 130 * 60000) {
// if we just received a sensor start from Nightscout,
// go ahead and see if we need to start a sensor session
if (transmitter && !(await transmitter.inSensorSession())) {
transmitter.startSensorTime(sensorStart.date, sensorStart.event.notes);
}
}
// For each of these, we catch any errors and then
// call resolve so the Promise.all works as it
// should and doesn't trigger early because of an error
const syncCalPromise = new TimeLimitedPromise(4 * 60 * 1000, async (resolve) => {
await syncCal(sensorInsert.date);
resolve();
});
const syncSGVsPromise = new TimeLimitedPromise(4 * 60 * 1000, async (resolve) => {
latestSGV = await syncSGVs();
resolve();
});
const syncBGChecksPromise = new TimeLimitedPromise(4 * 60 * 1000, async (resolve) => {
bgChecks = await syncBGChecks(sensorInsert.date, sensorStop.date);
resolve();
});
await Promise.all([syncCalPromise, syncSGVsPromise, syncBGChecksPromise])
.catch((err) => {
error(`syncNS error: ${err}`);
});
// have transmitterIO check if the sensor session should be ended.
if (transmitter) {
transmitter.checkSensorSession(sensorInsert.date, sensorStop.date, bgChecks, latestSGV);
}
const timeDelay = calcNextSyncTimeDelay(latestSGV);
log(
'\n====================================\n'
+ `syncNS complete - setting ${Math.round(timeDelay / 6000) / 10} minute timer`
+ '\n====================================',
);
setTimeout(() => {
// Restart the syncNS after 5 minute
syncNS(options, storage, transmitter);
}, timeDelay);
};
module.exports = syncNS;