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

Update the calculation of basal insulin for Apple Health with the same formula as TDD (#615)

* Allow to delete Carbs when connexion with NS is impossible - Alert the user of the issue.

* synchronise upload readings toggle in dexcom settings with FAX settings

* Update the calculation of basal insulin for Apple Health with the same formula as TDD
Pierre L 3 лет назад
Родитель
Сommit
8d4fc17e5a
1 измененных файлов с 19 добавлено и 2 удалено
  1. 19 2
      FreeAPS/Sources/Services/HealthKit/HealthKitManager.swift

+ 19 - 2
FreeAPS/Sources/Services/HealthKit/HealthKitManager.swift

@@ -280,16 +280,33 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver {
                     .compactMap { item -> InsulinBasal? in
                     .compactMap { item -> InsulinBasal? in
                         let nextElementEventIndex = item.offset + 1
                         let nextElementEventIndex = item.offset + 1
                         guard basalEvents.count > nextElementEventIndex else { return nil }
                         guard basalEvents.count > nextElementEventIndex else { return nil }
+
+                        var minimalDose = self.settingsManager.preferences.bolusIncrement
+                        if (minimalDose != 0.05) || (minimalDose != 0.025) {
+                            minimalDose = Decimal(0.1)
+                        }
+
                         let nextBasalEvent = basalEvents[nextElementEventIndex]
                         let nextBasalEvent = basalEvents[nextElementEventIndex]
                         let secondsOfCurrentBasal = nextBasalEvent.timestamp.timeIntervalSince(item.element.timestamp)
                         let secondsOfCurrentBasal = nextBasalEvent.timestamp.timeIntervalSince(item.element.timestamp)
                         let amount = Decimal(secondsOfCurrentBasal / 3600) * (item.element.rate ?? 0)
                         let amount = Decimal(secondsOfCurrentBasal / 3600) * (item.element.rate ?? 0)
+                        let incrementsRaw = amount / minimalDose
+
+                        var amountRounded: Decimal
+                        if incrementsRaw >= 1 {
+                            let incrementsRounded = floor(Double(incrementsRaw))
+                            amountRounded = Decimal(round(incrementsRounded * Double(minimalDose) * 100_000.0) / 100_000.0)
+                        } else {
+                            amountRounded = 0
+                        }
+
                         let id = String(item.element.id.dropFirst())
                         let id = String(item.element.id.dropFirst())
-                        guard amount > 0,
+                        guard amountRounded >= 0,
                               id != ""
                               id != ""
                         else { return nil }
                         else { return nil }
+
                         return InsulinBasal(
                         return InsulinBasal(
                             id: id,
                             id: id,
-                            amount: amount,
+                            amount: amountRounded,
                             startDelivery: item.element.timestamp,
                             startDelivery: item.element.timestamp,
                             endDelivery: nextBasalEvent.timestamp
                             endDelivery: nextBasalEvent.timestamp
                         )
                         )