Просмотр исходного кода

TDD calculation fixes
* Prevent future insulin entries for scheduled basal calculation
* Do not skip 0 TBR rates

Co-Authored-By: polscm32<polscm32@users.noreply.github.com>

Deniz Cengiz 1 год назад
Родитель
Сommit
26d82a1869
1 измененных файлов с 8 добавлено и 2 удалено
  1. 8 2
      FreeAPS/Sources/APS/Storage/TDDStorage.swift

+ 8 - 2
FreeAPS/Sources/APS/Storage/TDDStorage.swift

@@ -173,7 +173,7 @@ final class BaseTDDStorage: TDDStorage, Injectable {
 
         // Add temp basal events to the timeline
         for event in tempBasalEvents {
-            guard let duration = event.duration, let rate = event.amount, rate > 0 else { continue }
+            guard let duration = event.duration, let rate = event.amount else { continue }
             let end = event.timestamp.addingTimeInterval(TimeInterval(duration * 60))
             timeline.append((start: event.timestamp, end: end, type: .tempBasal, rate: rate))
         }
@@ -261,6 +261,7 @@ final class BaseTDDStorage: TDDStorage, Injectable {
 
         return gaps.reduce(into: Decimal(0)) { totalInsulin, gap in
             var currentTime = gap.start
+            let now = Date()
 
             while currentTime < gap.end {
                 // Find applicable basal rate for current time
@@ -276,7 +277,12 @@ final class BaseTDDStorage: TDDStorage, Injectable {
                     calendar: Calendar.current
                 ) ?? gap.end
 
-                let endTime = min(nextSwitchTime, gap.end)
+                // Ensure endTime does not exceed current time or gap end
+                let endTime = min(min(nextSwitchTime, gap.end), now)
+
+                // Only proceed if we have a valid time interval
+                guard endTime > currentTime else { break }
+
                 let durationHours = (Decimal(endTime.timeIntervalSince(currentTime)) / 3600).truncated(toPlaces: 5)
                 let insulin = Decimal(roundToSupportedBasalRate(Double(rate * durationHours)))