فهرست منبع

Preserve the last bolus if it falls past DIA

Fixes: https://github.com/nightscout/Trio-dev/issues/609
Sam King 5 ماه پیش
والد
کامیت
a44df0bb76
1فایلهای تغییر یافته به همراه13 افزوده شده و 3 حذف شده
  1. 13 3
      Trio/Sources/APS/OpenAPSSwift/Iob/IobGenerator.swift

+ 13 - 3
Trio/Sources/APS/OpenAPSSwift/Iob/IobGenerator.swift

@@ -17,9 +17,10 @@ struct IobGenerator {
         // DIA value in dealing with suspended pumps in JS
         var pumpHistory = history.filter({ $0.timestamp >= lastDia || $0.isSuspendOrResume() }).map({ $0.computedEvent() })
 
-        // To make sure that lastTemp is filled in correctly, we need to check
-        // if there aren't any tempBasal events in the DIA-filtered list.
-        // If not, find the most recent one from the full history and add it.
+        // To make sure that lastTemp and lastBolusTime are filled in
+        // correctly, we need to check if there aren't any tempBasal or bolus
+        // events in the DIA-filtered list. If not, find the most recent one
+        // from the full history and add it.
         if pumpHistory.filter({ $0.type == .tempBasal }).isEmpty {
             // Find the most recent TempBasal event from before the DIA cutoff
             let olderTempBasals = history.filter({ $0.type == .tempBasal && $0.timestamp < lastDia })
@@ -34,6 +35,15 @@ struct IobGenerator {
             }
         }
 
+        // we need to check for amount != 0 to match the lastBolusTime logic
+        if pumpHistory.filter({ $0.type == .bolus && $0.amount != 0 }).isEmpty {
+            // Find the most recent Bolus event from before the DIA cutoff
+            let olderBoluses = history.filter({ $0.type == .bolus && $0.amount != 0 && $0.timestamp < lastDia })
+            if let lastBolus = olderBoluses.max(by: { $0.timestamp < $1.timestamp }) {
+                pumpHistory.append(lastBolus.computedEvent())
+            }
+        }
+
         let treatments = try IobHistory.calcTempTreatments(
             history: pumpHistory,
             profile: profile,