Skip to content

Commit

Permalink
Process the Meal marker event from medtronic pump (#832)
Browse files Browse the repository at this point in the history
* Process the Meal marker event from medtronic pump (#810)

* Changes

* Process Meal Markers from pump

* Undo changes

* tabs to spaces to fix indentation

* return journalCarbs

* copy-pasta fix

* indentation
  • Loading branch information
scottleibrand authored Nov 28, 2017
1 parent 7dfd55b commit 217e820
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
18 changes: 11 additions & 7 deletions lib/bolus.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function reduce (treatments) {

var state = { };
var previous = [ ];

function in_previous (ev) {
var found = false;
previous.forEach(function (elem) {
Expand All @@ -23,7 +23,6 @@ function reduce (treatments) {
var candidates = tail.slice( ).filter(function (elem) {
var dt = Date.parse(elem.timestamp);
return ts - dt <= ms;

});
return candidates;
}
Expand Down Expand Up @@ -62,7 +61,7 @@ function reduce (treatments) {
if (remaining && remaining.length > 0) {
if (state.bolus && state.wizard) {
// skip to end
return bolus({}, []);
return bolus({}, []);
}
// keep recursing
return bolus(remaining[0], remaining.slice(1));
Expand Down Expand Up @@ -94,10 +93,10 @@ function reduce (treatments) {
var iobFile = "./monitor/iob.json";
var fs = require('fs');
if (fs.existsSync(iobFile)) {
var iob = JSON.parse(fs.readFileSync(iobFile));
if (iob && Array.isArray(iob) && iob.length) {
annotate("Calculated IOB:", iob[0].iob);
}
var iob = JSON.parse(fs.readFileSync(iobFile));
if (iob && Array.isArray(iob) && iob.length) {
annotate("Calculated IOB:", iob[0].iob);
}
}
}

Expand Down Expand Up @@ -167,6 +166,11 @@ function reduce (treatments) {
var tail = within_minutes_from(current, treatments.slice(index+1), 2);
bolus(current, tail);
break;
case 'JournalEntryMealMarker':
current.carbs = current.carb_input;
current.eventType = 'Carb Correction';
results.push(current);
break;
default:
results.push(current);
break;
Expand Down
21 changes: 15 additions & 6 deletions lib/meal/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ function findMealInputs (inputs) {
var profile_data = inputs.profile;
var mealInputs = [];
var duplicates = 0;

for (var i=0; i < carbHistory.length; i++) {
var current = carbHistory[i];
if (current.carbs && current.created_at) {
var temp = {};
temp.timestamp = current.created_at;
temp.carbs = current.carbs;
temp.nsCarbs = current.carbs;

if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.created_at,"carbs")) {
mealInputs.push(temp);
} else {
Expand All @@ -46,7 +45,7 @@ function findMealInputs (inputs) {
var temp = {};
temp.timestamp = current.timestamp;
temp.bolus = current.amount;

if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.timestamp,"bolus")) {
mealInputs.push(temp);
} else {
Expand All @@ -58,7 +57,7 @@ function findMealInputs (inputs) {
temp.timestamp = current.timestamp;
temp.carbs = current.carb_input;
temp.bwCarbs = current.carb_input;

// don't enter the treatment if there's another treatment with the same exact timestamp
// to prevent duped carb entries from multiple sources
if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.timestamp,"carbs")) {
Expand Down Expand Up @@ -106,11 +105,21 @@ function findMealInputs (inputs) {
} else {
duplicates += 1;
}
} else if (current._type == "JournalEntryMealMarker" && current.carb_input > 0 && current.timestamp) {
var temp = {};
temp.timestamp = current.timestamp;
temp.carbs = current.carb_input;
temp.journalCarbs = current.carb_input;
if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.timestamp,"carbs")) {
mealInputs.push(temp);
} else {
duplicates += 1;
}
}
}

//if (duplicates > 0) console.error("Removed duplicate bolus/carb entries:" + duplicates);

return mealInputs;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/meal/total.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function recentCarbs(opts, time) {
var carbs = 0;
var nsCarbs = 0;
var bwCarbs = 0;
var journalCarbs = 0;
var bwFound = false;
var carbDelay = 20 * 60 * 1000;
var maxCarbs = 0;
Expand Down Expand Up @@ -41,6 +42,7 @@ function recentCarbs(opts, time) {
var carbsToRemove = 0;
var nsCarbsToRemove = 0;
var bwCarbsToRemove = 0;
var journalCarbsToRemove = 0;
treatments.forEach(function(treatment) {
var now = time.getTime();
// consider carbs from up to 6 hours ago in calculating COB
Expand All @@ -54,6 +56,8 @@ function recentCarbs(opts, time) {
} else if (treatment.bwCarbs >= 1) {
bwCarbs += parseFloat(treatment.bwCarbs);
bwFound = true;
} else if (treatment.journalCarbs >= 1) {
journalCarbs += parseFloat(treatment.journalCarbs);
} else {
console.error("Treatment carbs unclassified:",treatment);
}
Expand All @@ -71,6 +75,8 @@ function recentCarbs(opts, time) {
nsCarbsToRemove += parseFloat(treatment.nsCarbs);
} else if (treatment.bwCarbs >= 1) {
bwCarbsToRemove += parseFloat(treatment.bwCarbs);
} else if (treatment.journalCarbs >= 1) {
journalCarbsToRemove += parseFloat(treatment.journalCarbs);
}
} else {
carbsToRemove = 0;
Expand All @@ -86,6 +92,7 @@ function recentCarbs(opts, time) {
carbs -= carbsToRemove;
nsCarbs -= nsCarbsToRemove;
bwCarbs -= bwCarbsToRemove;
journalCarbs -= journalCarbsToRemove;

// calculate the current deviation and steepest deviation downslope over the last hour
COB_inputs.ciTime = time.getTime();
Expand Down Expand Up @@ -113,6 +120,7 @@ function recentCarbs(opts, time) {
carbs: Math.round( carbs * 1000 ) / 1000
, nsCarbs: Math.round( nsCarbs * 1000 ) / 1000
, bwCarbs: Math.round( bwCarbs * 1000 ) / 1000
, journalCarbs: Math.round( journalCarbs * 1000 ) / 1000
, mealCOB: Math.round( mealCOB )
, currentDeviation: Math.round( c.currentDeviation * 100 ) / 100
, maxDeviation: Math.round( c.maxDeviation * 100 ) / 100
Expand Down

0 comments on commit 217e820

Please sign in to comment.