Jon Mårtensson 4 лет назад
Родитель
Сommit
80162c8f0b
1 измененных файлов с 37 добавлено и 38 удалено
  1. 37 38
      FreeAPS/Resources/javascript/middleware/determine_basal.js

+ 37 - 38
FreeAPS/Resources/javascript/middleware/determine_basal.js

@@ -7,7 +7,6 @@ function middleware(iob, currenttemp, glucose, profile, autosens, meal, reservoi
     const minLimitChris = profile.autosens_min;
     const maxLimitChris = profile.autosens_max;
     const adjustmentFactor = 1;
-    // Your current target, lower limit
     const currentMinTarget = profile.min_bg;
     var exerciseSetting = false;
     var log = "";
@@ -114,20 +113,21 @@ function middleware(iob, currenttemp, glucose, profile, autosens, meal, reservoi
                 let minutes = time1.getMinutes();
                 let seconds = "00";
                 let string = "" + hour + ":" + minutes + ":" + seconds;
-                let baseRate = new Date(string);
+                let baseTime = new Date(string);
                 let basalScheduledRate = 0;
                 for (let k = 0; k < profile.basalprofile.length; k++) {
-                    if (profile.basalprofile[k].start == baseRate) {
+                    if (profile.basalprofile[k].start == baseTime) {
                         basalScheduledRate = profile.basalprofile[k].rate;
                         insulin = basalScheduledRate * basDuration;
                         break;
-                    } else if (k + 1 < profile.basalprofile.length) {
-                            if (profile.basalprofile[k].start < baseRate && profile.basalprofile[k+1].start > baseRate){
-                                basalScheduledRate = profile.basalprofile[k].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;
+                            insulin = basalScheduledRate * basDuration;
+                            break;
+                        }
+                    }
                 }
                 // Account for smallest possible pump dosage
                 incrementsRaw = insulin / minimalDose;
@@ -142,54 +142,53 @@ function middleware(iob, currenttemp, glucose, profile, autosens, meal, reservoi
     
     // Check for temp basals that completes
     for (let i = 2; i < pumphistory.length; i++) {
-        if (pumphistory[i]._type == "TempBasal") {
-            let time1 = new Date(pumphistory[i].timestamp);
-            let time2 = time1;
+        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;
             do {
                 --m;
                 if (pumphistory[m]._type == "TempBasal" && m >= 0) {
                     // next (newer) temp basal
-                    let time2 = new Date(pumphistory[m].timestamp);
+                    let time1 = new Date(pumphistory[m].timestamp);
                     break;
                 }
             } while (m >= 0);
             
-            let basDuration = (time2 - time1) / 36e5;
+            let tempBasalTimeDifference = (time2 - time1) / 36e5;
             
-            if ((pumphistory[i-1]['duration (min)'] / 60 ) < basDuration) {
+            if (time2Duration < tempBasalTimeDifference) {
                 
-                let timeOfbasal = basDuration - (pumphistory[i-1]['duration (min)'] / 60); //
-                console.log('timeOfBasal: ' + timeOfbasal);
+                let timeOfbasal = tempBasalTimeDifference - time2Duration; //
                 
-                let hour = time1.getHours();
-                let minutes = time1.getMinutes();
+                let hour = time2.getHours();
+                let minutes = time2.getMinutes();
                 let seconds = "00";
                 let string = "" + hour + ":" + minutes + ":" + seconds;
                 let baseTime = new Date(string);
                 
-                console.log('baseTime:  ' + baseTime + ' and time dirrence:' + (time2 - time1) / 36e5);
-                
-                for (let k = 0; k < profile.basalprofile.length; k++) {
-                    if (profile.basalprofile[k].start == baseTime) {
-                        let basalScheduledRate = profile.basalprofile[k].rate;
-                        insulin = basalScheduledRate * timeOfbasal;
+                // 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 || profile.basalprofile[k+1].start > baseTime) {
+                        basalScheduledRate = profile.basalprofile[k].rate;
+                        insulin = basalScheduledRate * basDuration;
                         break;
                     }
-                    else if (k+1 <= (profile.basalprofile.length - 1)) {
-                            if (profile.basalprofile[k].start < baseTime &&  profile.basalprofile[k+1].start > baseTime) {
-                                basalScheduledRate = profile.basalprofile[k].rate;
-                                insulin = basalScheduledRate * timeOfbasal;
-                                break;
-                            }
-                            else if (k == (profile.basalprofile.length - 1) && baseTime >= profile.basalprofile[k].start) {
-                                    basalScheduledRate = profile.basalprofile[k].rate;
-                                    insulin = basalScheduledRate * timeOfbasal;
-                                    break;
-                            }
+                    else if (profile.basalprofile[k].start < baseTime && profile.basalprofile[k+1].start > baseTime) {
+                            basalScheduledRate = profile.basalprofile[k].rate;
+                            insulin = basalScheduledRate * basDuration;
+                            break;
+                    }
+                    else if (k = profile.basalprofile.length) {
+                        basalScheduledRate = profile.basalprofile[k-1].rate;
+                        insulin = basalScheduledRate * basDuration;
+                        break;
                     }
                 }
-                
+            
                 // Account for smallest possible pump dosage
                 incrementsRaw = insulin / minimalDose;
                 if (incrementsRaw >= 1) {