|
|
@@ -109,20 +109,20 @@ function middleware(iob, currenttemp, glucose, profile, autosens, meal, reservoi
|
|
|
// 2. Check for temp basals that completes. This is for when disconected from link/iphone, or when in open loop.
|
|
|
// To do: need to check for more circumstances when scheduled basal rates are used.
|
|
|
//
|
|
|
- for (let i = 0; i < pumphistory.length; i++) {
|
|
|
+ for (let k = 0; k < pumphistory.length; k++) {
|
|
|
// Check for 0 temp basals with 0 min duration.
|
|
|
insulin = 0;
|
|
|
- if (pumphistory[i]['duration (min)'] == 0) {
|
|
|
- let time1 = new Date(pumphistory[i].timestamp);
|
|
|
+ if (pumphistory[k]['duration (min)'] == 0) {
|
|
|
+ let time1 = new Date(pumphistory[k].timestamp);
|
|
|
let time2 = time1;
|
|
|
- let j = i;
|
|
|
+ let l = k;
|
|
|
do {
|
|
|
- --j;
|
|
|
- if (pumphistory[j]._type == "TempBasal" && j >= 0) {
|
|
|
- time2 = new Date(pumphistory[j].timestamp);
|
|
|
+ --l;
|
|
|
+ if (pumphistory[l]._type == "TempBasal" && l >= 0) {
|
|
|
+ time2 = new Date(pumphistory[l].timestamp);
|
|
|
break;
|
|
|
}
|
|
|
- } while (j > 0);
|
|
|
+ } while (l > 0);
|
|
|
// duration of current scheduled basal in h
|
|
|
let basDuration = (time2 - time1) / 36e5;
|
|
|
if (basDuration > 0) {
|
|
|
@@ -132,21 +132,21 @@ function middleware(iob, currenttemp, glucose, profile, autosens, meal, reservoi
|
|
|
let string = "" + hour + ":" + minutes + ":" + seconds;
|
|
|
let baseTime = new Date(string);
|
|
|
let basalScheduledRate = profile.basalprofile[0].start;
|
|
|
- for (let k = 0; k < profile.basalprofile.length; k++) {
|
|
|
- if (profile.basalprofile[k].start == baseTime) {
|
|
|
- basalScheduledRate = profile.basalprofile[k].rate;
|
|
|
+ for (let m = 0; m < profile.basalprofile.length; m++) {
|
|
|
+ if (profile.basalprofile[m].start == baseTime) {
|
|
|
+ basalScheduledRate = profile.basalprofile[m].rate;
|
|
|
insulin = basalScheduledRate * basDuration;
|
|
|
break;
|
|
|
}
|
|
|
- else if (k + 1 < profile.basalprofile.length) {
|
|
|
- if (profile.basalprofile[k].start < baseTime && profile.basalprofile[k+1].start > baseTime) {
|
|
|
- basalScheduledRate = profile.basalprofile[k].rate;
|
|
|
+ else if (m + 1 < profile.basalprofile.length) {
|
|
|
+ if (profile.basalprofile[m].start < baseTime && profile.basalprofile[m+1].start > baseTime) {
|
|
|
+ basalScheduledRate = profile.basalprofile[m].rate;
|
|
|
insulin = basalScheduledRate * basDuration;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- else if (k == profile.basalprofile.length - 1) {
|
|
|
- basalScheduledRate = profile.basalprofile[k].rate;
|
|
|
+ else if (m == profile.basalprofile.length - 1) {
|
|
|
+ basalScheduledRate = profile.basalprofile[m].rate;
|
|
|
insulin = basalScheduledRate * basDuration;
|
|
|
break;
|
|
|
}
|
|
|
@@ -157,58 +157,64 @@ function middleware(iob, currenttemp, glucose, profile, autosens, meal, reservoi
|
|
|
incrementsRounded = Math.floor(incrementsRaw);
|
|
|
insulin = incrementsRounded * minimalDose;
|
|
|
scheduledBasalInsulin += insulin;
|
|
|
- } else { insulin = 0}
|
|
|
+ } else { insulin = 0;
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Check for temp basals that completes
|
|
|
- for (let i = pumphistory.length -1; i > 0; i--) {
|
|
|
- if (pumphistory[i]._type == "TempBasalDuration" && pumphistory[i]['duration (min)'] > 0) {
|
|
|
- let time2Duration = pumphistory[i]['duration (min)'] / 60;
|
|
|
- let time2 = new Date(pumphistory[i].timestamp);;
|
|
|
- let time1 = time2;
|
|
|
- let m = i;
|
|
|
+ for (let n = pumphistory.length -1; n > 0; n--) {
|
|
|
+ if (pumphistory[n]._type == "TempBasalDuration") {
|
|
|
+ // duration in hours
|
|
|
+ let oldBasalDuration = pumphistory[n]['duration (min)'] / 60;
|
|
|
+ // time of old temp basal
|
|
|
+ let oldTime = new Date(pumphistory[n].timestamp);
|
|
|
+ let newTime = oldTime;
|
|
|
+ let o = n;
|
|
|
do {
|
|
|
- --m;
|
|
|
- if (m >= 0) {
|
|
|
- if (pumphistory[m]._type == "TempBasal" && m >= 0) {
|
|
|
- // next (newer) temp basal
|
|
|
- let time1 = new Date(pumphistory[m].timestamp);
|
|
|
+ --o;
|
|
|
+ if (o >= 0) {
|
|
|
+ if (pumphistory[o]._type == "TempBasal") {
|
|
|
+ // time of next (new) temp basal
|
|
|
+ newTime = new Date(pumphistory[o].timestamp);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- } while (m > 0);
|
|
|
+ } while (o > 0);
|
|
|
|
|
|
- // Time difference in hours
|
|
|
- let tempBasalTimeDifference = (time1 - time2) / 36e5;
|
|
|
+ // Time difference in hours, new - old
|
|
|
+ let tempBasalTimeDifference = (newTime - oldTime) / 36e5;
|
|
|
+ let timeOfbasal = tempBasalTimeDifference - oldBasalDuration;
|
|
|
|
|
|
- if (time2Duration < tempBasalTimeDifference) {
|
|
|
-
|
|
|
- let timeOfbasal = tempBasalTimeDifference - time2Duration; //
|
|
|
-
|
|
|
- let hour = time2.getHours();
|
|
|
- let minutes = time2.getMinutes();
|
|
|
+ // if duration of scheduled basal is more than 0
|
|
|
+ if (timeOfbasal > 0) {
|
|
|
+ let hour = oldTime.getHours();
|
|
|
+ let minutes = oldTime.getMinutes();
|
|
|
let seconds = "00";
|
|
|
- let string = "" + hour + ":" + minutes + ":" + seconds;
|
|
|
- let baseTime = new Date(string);
|
|
|
+ // "hour:minutes:00"
|
|
|
+ let timeString = "" + hour + ":" + minutes + ":" + seconds;
|
|
|
+ let baseTime = new Date(timeString);
|
|
|
|
|
|
// Default if correct basal schedule rate not found
|
|
|
let basalScheduledRate = profile.basalprofile[0].rate;
|
|
|
|
|
|
- for (let k = 0; k < profile.basalprofile.length; ++k) {
|
|
|
- if (profile.basalprofile[k].start == baseTime) {
|
|
|
- basalScheduledRate = profile.basalprofile[k].rate;
|
|
|
+ for (let p = 0; p < profile.basalprofile.length; ++p) {
|
|
|
+ let basalRateTime = new Date(profile.basalprofile[p].start);
|
|
|
+ if (basalRateTime == baseTime) {
|
|
|
+ basalScheduledRate = profile.basalprofile[p].rate;
|
|
|
break;
|
|
|
}
|
|
|
- else if (k+1 < profile.basalprofile.length) {
|
|
|
- if (profile.basalprofile[k].start < baseTime && profile.basalprofile[k+1].start > baseTime) {
|
|
|
- basalScheduledRate = profile.basalprofile[k].rate;
|
|
|
+ else if (p+1 < profile.basalprofile.length) {
|
|
|
+ let nextBasalRateTime = new Date(profile.basalprofile[p+1].start);
|
|
|
+ if (basalRateTime < baseTime && nextBasalRateTime > baseTime) {
|
|
|
+ basalScheduledRate = profile.basalprofile[p].rate;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- else if (k == (profile.basalprofile.length - 1)) {
|
|
|
- basalScheduledRate = profile.basalprofile[k].rate;
|
|
|
+ else if (p == (profile.basalprofile.length - 1)) {
|
|
|
+ basalScheduledRate = profile.basalprofile[p].rate;
|
|
|
break;
|
|
|
}
|
|
|
}
|