Skip to content

Commit

Permalink
Merge pull request tidepool-org#608 from tidepool-org/pazaan/medtroni…
Browse files Browse the repository at this point in the history
…c600-time-change-bugs

Fix for basal event errors caused by time change scenarios
  • Loading branch information
pazaan authored May 3, 2018
2 parents 12aae8d + dec4203 commit 081caca
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tidepool-uploader",
"productName": "tidepool-uploader",
"version": "2.2.5-600series-qa.19",
"version": "2.2.5-600series-qa.20",
"description": "Tidepool Project Universal Uploader",
"main": "./main.js",
"author": {
Expand Down
6 changes: 4 additions & 2 deletions lib/drivers/medtronic600/NGPHistoryParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,9 @@ class NGPHistoryParser {

const duration = resumeEvent == null ? 0 : resumeEvent.timestamp.toDate().valueOf() -
event.timestamp.toDate().valueOf();
if (duration < 0) {
throw new Error('Suspend event duration cannot be less than zero');
}

const suspendResumeEvent = this.cfg.builder.makeDeviceEventSuspendResume()
.with_reason(reason)
Expand Down Expand Up @@ -1632,7 +1635,7 @@ class NGPHistoryParser {
const wizard = this.cfg.builder.makeWizard()
.with_recommended({
carb: wizardEvent.foodEstimate,
correction: wizardEvent.iobAdjustment,
correction: wizardEvent.correctionEstimate,
net: wizardEvent.wizardEstimate,
})
.with_bolus(wizardBolus)
Expand All @@ -1649,7 +1652,6 @@ class NGPHistoryParser {
});
}


if (wizardEvent.bgInput > 0) {
wizard.with_bgInput(wizardEvent.bgInput);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/drivers/medtronic600/NGPUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class NGPTimestamp {
const offset = buffer.readUInt32BE(0x04) - 0x100000000;
return new NGPTimestamp(rtc, offset);
}

static fromDateAndRtc(jsDate, rtc) {
const offset = (jsDate.getTime() - NGPTimestamp.pumpBaseTimeMS - (rtc * 1000)) / 1000;
return new NGPTimestamp(rtc, offset);
}
}

class NGPConstants {
Expand Down
39 changes: 20 additions & 19 deletions lib/drivers/medtronic600/medtronic600Simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class Medtronic600Simulator {
this.currSMBG = null;
this.currTimestamp = null;
this.currPumpSettings = null;
this.currStatus = null;
}

static get TWENTY_FOUR_HOURS() {
Expand Down Expand Up @@ -129,11 +128,6 @@ class Medtronic600Simulator {
}

if (this.currBasal != null) {
const currentNgpTimestamp = new NGPUtil.NGPTimestamp(
this.config.settings.currentNgpTimestamp.rtc,
this.config.settings.currentNgpTimestamp.offset,
);

// Back-fill Auto-Basal gaps
if (this.currBasal.deliveryType === 'automated') {
const autoBasalDifference = Date.parse(event.time).valueOf() -
Expand All @@ -147,14 +141,11 @@ class Medtronic600Simulator {
.set('basalEndTime', sundial.parseFromFormat(Date.parse(this.currBasal.time).valueOf() + 300000).toISOString());
this.events.push(this.currBasal.done());

/* eslint-disable function-paren-newline */
const currBasalTimestamp = this.getCurrentBasalTimestamp();
const newDeviceTimestamp = new NGPUtil.NGPTimestamp(
currentNgpTimestamp.rtcFromDate(
sundial.parseFromFormat(
sundial.parseFromFormat(this.currBasal.deviceTime).valueOf() + 300000)),
currentNgpTimestamp.offset,
currBasalTimestamp.rtc + 300,
currBasalTimestamp.offset,
);
/* eslint-enable function-paren-newline */

const insertedBasal = this.config.builder.makeAutomatedBasal()
.with_deviceTime(sundial.formatDeviceTime(newDeviceTimestamp.toDate()))
Expand Down Expand Up @@ -220,7 +211,8 @@ class Medtronic600Simulator {
deliveryType: suppressedBasal.deliveryType,
rate: suppressedBasal.rate,
scheduleName: suppressedBasal.scheduleName,
});
})
.set('index', suppressedBasal.index);
if (!Number.isNaN(Number(this.currBasal.percent))) {
event.set('rate', suppressedBasal.rate * this.currBasal.percent);
}
Expand All @@ -234,12 +226,11 @@ class Medtronic600Simulator {
// It's also feasible that we won't get a basal segment start event at all if the user
// has cancelled a temp basal, and followed it up immediately with another temp basal.

/* eslint-disable function-paren-newline */
const currBasalTimestamp = this.getCurrentBasalTimestamp();
const newDeviceTimestamp = new NGPUtil.NGPTimestamp(
currentNgpTimestamp.rtcFromDate(
sundial.parseFromFormat(sundial.parseFromFormat(this.currBasal.deviceTime).getTime() +
this.currBasal.duration)), currentNgpTimestamp.offset);
/* eslint-enable function-paren-newline */
currBasalTimestamp.rtc + (this.currBasal.duration / 1000),
currBasalTimestamp.offset,
);

if (this.currBasal.suppressed.type !== event.type ||
this.currBasal.suppressed.deliveryType !== event.deliveryType ||
Expand Down Expand Up @@ -313,7 +304,6 @@ class Medtronic600Simulator {

suspendResume(event) {
this.simpleSimulate(event);
this.currStatus = event;
}

cbg(event) {
Expand Down Expand Up @@ -435,6 +425,17 @@ class Medtronic600Simulator {
_.remove(this.events, event => event === eventToRemove);
}

getCurrentBasalTimestamp() {
if (_.isUndefined(this.currBasal.index)) {
throw new TypeError(`Expected valid index for basal: ${JSON.stringify(this.currBasal)}`);
}

return NGPUtil.NGPTimestamp.fromDateAndRtc(
sundial.parseFromFormat(this.currBasal.deviceTime),
this.currBasal.index,
);
}

applySuspendedBasals() {
const orderedBasals = _.sortBy(
_.filter(this.events, event => event.type === 'basal'),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tidepool-uploader",
"version": "2.2.5-600series-qa.19",
"version": "2.2.5-600series-qa.20",
"description": "Tidepool Project Universal Uploader",
"private": true,
"main": "main.js",
Expand Down
Loading

0 comments on commit 081caca

Please sign in to comment.