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

retrieve current basal from settings (not suggestion, because this value is 0 most of the time and therefore not useful for a superbolus)

polscm32 2 лет назад
Родитель
Сommit
bddaa53bcd

+ 6 - 0
FreeAPS/Sources/Modules/Bolus/BolusProvider.swift

@@ -14,6 +14,12 @@ extension Bolus {
                 ?? PumpSettings(insulinActionCurve: 6, maxBolus: 10, maxBasal: 2)
         }
 
+        func getProfile() -> [BasalProfileEntry] {
+            storage.retrieve(OpenAPS.Settings.basalProfile, as: [BasalProfileEntry].self)
+                ?? [BasalProfileEntry](from: OpenAPS.defaults(for: OpenAPS.Settings.basalProfile))
+                ?? []
+        }
+
         func fetchGlucose() -> [Readings] {
             var fetchGlucose = [Readings]()
             coredataContext.performAndWait {

+ 39 - 0
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -62,6 +62,8 @@ extension Bolus {
         @Published var useFattyMealCorrectionFactor: Bool = false
         @Published var eventualBG: Int = 0
 
+        @Published var currentBasal: Decimal = 0
+
         @Published var meal: [CarbsEntry]?
         @Published var carbs: Decimal = 0
         @Published var fat: Decimal = 0
@@ -95,6 +97,42 @@ extension Bolus {
             }
         }
 
+        func getCurrentBasal() {
+            let basalEntries = provider.getProfile()
+
+            let dateFormatter = DateFormatter()
+            dateFormatter.dateFormat = "HH:mm:ss"
+            let currentTime = dateFormatter.string(from: Date())
+
+            // loop throug entries and get current basal entry
+            for (index, entry) in basalEntries.enumerated() {
+                if let entryStartTimeDate = dateFormatter.date(from: entry.start) {
+                    var entryEndTimeDate: Date
+
+                    if index < basalEntries.count - 1 {
+                        let nextEntry = basalEntries[index + 1]
+                        if let nextEntryStartTimeDate = dateFormatter.date(from: nextEntry.start) {
+                            let timeDifference = nextEntryStartTimeDate.timeIntervalSince(entryStartTimeDate)
+                            entryEndTimeDate = entryStartTimeDate.addingTimeInterval(timeDifference)
+                        } else {
+                            continue
+                        }
+                    } else {
+                        entryEndTimeDate = Date()
+                    }
+                    // if currenTime is between start and end of basal entry -> basal = currentBasal
+                    if let currentTimeDate = dateFormatter.date(from: currentTime) {
+                        if currentTimeDate >= entryStartTimeDate, currentTimeDate <= entryEndTimeDate {
+                            if let basal = entry.rate as? Decimal {
+                                currentBasal = basal
+                                break
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
         func getDeltaBG() {
             let glucose = provider.fetchGlucose()
             guard glucose.count >= 3 else { return }
@@ -214,6 +252,7 @@ extension Bolus {
                     .roundBolus(amount: max(self.insulinRecommended, 0))
 
                 if self.useCalc {
+                    self.getCurrentBasal()
                     self.getDeltaBG()
                     self.insulinCalculated = self.calculateInsulin()
                 }