@@ -964,7 +964,7 @@ extension LoopDataManager {
964
964
let updateGroup = DispatchGroup ( )
965
965
966
966
let historicalGlucoseStartDate = Date ( timeInterval: - LoopCoreConstants. dosingDecisionHistoricalGlucoseInterval, since: now ( ) )
967
- let inputDataRecencyStartDate = Date ( timeInterval: - LoopCoreConstants . inputDataRecencyInterval, since: now ( ) )
967
+ let inputDataRecencyStartDate = Date ( timeInterval: - LoopAlgorithm . inputDataRecencyInterval, since: now ( ) )
968
968
969
969
// Fetch glucose effects as far back as we want to make retroactive analysis and historical glucose for dosing decision
970
970
var historicalGlucose : [ HistoricalGlucoseValue ] ?
@@ -1227,15 +1227,15 @@ extension LoopDataManager {
1227
1227
let pumpStatusDate = doseStore. lastAddedPumpData
1228
1228
let lastGlucoseDate = glucose. startDate
1229
1229
1230
- guard now ( ) . timeIntervalSince ( lastGlucoseDate) <= LoopCoreConstants . inputDataRecencyInterval else {
1230
+ guard now ( ) . timeIntervalSince ( lastGlucoseDate) <= LoopAlgorithm . inputDataRecencyInterval else {
1231
1231
throw LoopError . glucoseTooOld ( date: glucose. startDate)
1232
1232
}
1233
1233
1234
1234
guard lastGlucoseDate. timeIntervalSince ( now ( ) ) <= LoopCoreConstants . futureGlucoseDataInterval else {
1235
1235
throw LoopError . invalidFutureGlucose ( date: lastGlucoseDate)
1236
1236
}
1237
1237
1238
- guard now ( ) . timeIntervalSince ( pumpStatusDate) <= LoopCoreConstants . inputDataRecencyInterval else {
1238
+ guard now ( ) . timeIntervalSince ( pumpStatusDate) <= LoopAlgorithm . inputDataRecencyInterval else {
1239
1239
throw LoopError . pumpDataTooOld ( date: pumpStatusDate)
1240
1240
}
1241
1241
@@ -1487,15 +1487,15 @@ extension LoopDataManager {
1487
1487
let pumpStatusDate = doseStore. lastAddedPumpData
1488
1488
let lastGlucoseDate = glucose. startDate
1489
1489
1490
- guard now ( ) . timeIntervalSince ( lastGlucoseDate) <= LoopCoreConstants . inputDataRecencyInterval else {
1490
+ guard now ( ) . timeIntervalSince ( lastGlucoseDate) <= LoopAlgorithm . inputDataRecencyInterval else {
1491
1491
throw LoopError . glucoseTooOld ( date: glucose. startDate)
1492
1492
}
1493
1493
1494
- guard lastGlucoseDate. timeIntervalSince ( now ( ) ) <= LoopCoreConstants . inputDataRecencyInterval else {
1494
+ guard lastGlucoseDate. timeIntervalSince ( now ( ) ) <= LoopAlgorithm . inputDataRecencyInterval else {
1495
1495
throw LoopError . invalidFutureGlucose ( date: lastGlucoseDate)
1496
1496
}
1497
1497
1498
- guard now ( ) . timeIntervalSince ( pumpStatusDate) <= LoopCoreConstants . inputDataRecencyInterval else {
1498
+ guard now ( ) . timeIntervalSince ( pumpStatusDate) <= LoopAlgorithm . inputDataRecencyInterval else {
1499
1499
throw LoopError . pumpDataTooOld ( date: pumpStatusDate)
1500
1500
}
1501
1501
@@ -1541,16 +1541,18 @@ extension LoopDataManager {
1541
1541
1542
1542
let model = doseStore. insulinModelProvider. model ( for: pumpInsulinType)
1543
1543
1544
- return predictedGlucose. recommendedManualBolus (
1544
+ var recommendation = predictedGlucose. recommendedManualBolus (
1545
1545
to: glucoseTargetRange,
1546
1546
at: now ( ) ,
1547
1547
suspendThreshold: settings. suspendThreshold? . quantity,
1548
1548
sensitivity: insulinSensitivity,
1549
1549
model: model,
1550
- pendingInsulin: 0 , // Pending insulin is already reflected in the prediction
1551
- maxBolus: maxBolus,
1552
- volumeRounder: volumeRounder
1550
+ maxBolus: maxBolus
1553
1551
)
1552
+
1553
+ // Round to pump precision
1554
+ recommendation. amount = volumeRounder ( recommendation. amount)
1555
+ return recommendation
1554
1556
}
1555
1557
1556
1558
/// Generates a correction effect based on how large the discrepancy is between the current glucose and its model predicted value.
@@ -1575,37 +1577,22 @@ extension LoopDataManager {
1575
1577
// Get timeline of glucose discrepancies
1576
1578
retrospectiveGlucoseDiscrepancies = insulinCounteractionEffects. subtracting ( carbEffects, withUniformInterval: carbStore. delta)
1577
1579
1578
- // Calculate retrospective correction
1579
- let insulinSensitivity = settings. insulinSensitivitySchedule!. quantity ( at: glucose. startDate)
1580
- let basalRate = settings. basalRateSchedule!. value ( at: glucose. startDate)
1581
- let correctionRange = settings. glucoseTargetRangeSchedule!. quantityRange ( at: glucose. startDate)
1582
-
1583
1580
retrospectiveGlucoseEffect = retrospectiveCorrection. computeEffect (
1584
1581
startingAt: glucose,
1585
1582
retrospectiveGlucoseDiscrepanciesSummed: retrospectiveGlucoseDiscrepanciesSummed,
1586
- recencyInterval: LoopCoreConstants . inputDataRecencyInterval,
1587
- insulinSensitivity: insulinSensitivity,
1588
- basalRate: basalRate,
1589
- correctionRange: correctionRange,
1583
+ recencyInterval: LoopAlgorithm . inputDataRecencyInterval,
1590
1584
retrospectiveCorrectionGroupingInterval: LoopMath . retrospectiveCorrectionGroupingInterval
1591
1585
)
1592
1586
}
1593
1587
1594
1588
private func computeRetrospectiveGlucoseEffect( startingAt glucose: GlucoseValue , carbEffects: [ GlucoseEffect ] ) -> [ GlucoseEffect ] {
1595
1589
1596
- let insulinSensitivity = settings. insulinSensitivitySchedule!. quantity ( at: glucose. startDate)
1597
- let basalRate = settings. basalRateSchedule!. value ( at: glucose. startDate)
1598
- let correctionRange = settings. glucoseTargetRangeSchedule!. quantityRange ( at: glucose. startDate)
1599
-
1600
1590
let retrospectiveGlucoseDiscrepancies = insulinCounteractionEffects. subtracting ( carbEffects, withUniformInterval: carbStore. delta)
1601
1591
let retrospectiveGlucoseDiscrepanciesSummed = retrospectiveGlucoseDiscrepancies. combinedSums ( of: LoopMath . retrospectiveCorrectionGroupingInterval * retrospectiveCorrectionGroupingIntervalMultiplier)
1602
1592
return retrospectiveCorrection. computeEffect (
1603
1593
startingAt: glucose,
1604
1594
retrospectiveGlucoseDiscrepanciesSummed: retrospectiveGlucoseDiscrepanciesSummed,
1605
- recencyInterval: LoopCoreConstants . inputDataRecencyInterval,
1606
- insulinSensitivity: insulinSensitivity,
1607
- basalRate: basalRate,
1608
- correctionRange: correctionRange,
1595
+ recencyInterval: LoopAlgorithm . inputDataRecencyInterval,
1609
1596
retrospectiveCorrectionGroupingInterval: LoopMath . retrospectiveCorrectionGroupingInterval
1610
1597
)
1611
1598
}
@@ -1690,17 +1677,17 @@ extension LoopDataManager {
1690
1677
1691
1678
var errors = [ LoopError] ( )
1692
1679
1693
- if startDate. timeIntervalSince ( glucose. startDate) > LoopCoreConstants . inputDataRecencyInterval {
1680
+ if startDate. timeIntervalSince ( glucose. startDate) > LoopAlgorithm . inputDataRecencyInterval {
1694
1681
errors. append ( . glucoseTooOld( date: glucose. startDate) )
1695
1682
}
1696
1683
1697
- if glucose. startDate. timeIntervalSince ( startDate) > LoopCoreConstants . inputDataRecencyInterval {
1684
+ if glucose. startDate. timeIntervalSince ( startDate) > LoopAlgorithm . inputDataRecencyInterval {
1698
1685
errors. append ( . invalidFutureGlucose( date: glucose. startDate) )
1699
1686
}
1700
1687
1701
1688
let pumpStatusDate = doseStore. lastAddedPumpData
1702
1689
1703
- if startDate. timeIntervalSince ( pumpStatusDate) > LoopCoreConstants . inputDataRecencyInterval {
1690
+ if startDate. timeIntervalSince ( pumpStatusDate) > LoopAlgorithm . inputDataRecencyInterval {
1704
1691
errors. append ( . pumpDataTooOld( date: pumpStatusDate) )
1705
1692
}
1706
1693
@@ -2176,7 +2163,7 @@ extension LoopDataManager {
2176
2163
sensitivitySchedule: sensitivitySchedule,
2177
2164
at: date)
2178
2165
2179
- dosingDecision. manualBolusRecommendation = ManualBolusRecommendationWithDate ( recommendation: ManualBolusRecommendation ( amount: bolusAmount. doubleValue ( for: . internationalUnit( ) ) , pendingInsulin : 0 , notice: notice) ,
2166
+ dosingDecision. manualBolusRecommendation = ManualBolusRecommendationWithDate ( recommendation: ManualBolusRecommendation ( amount: bolusAmount. doubleValue ( for: . internationalUnit( ) ) , notice: notice) ,
2180
2167
date: Date ( ) )
2181
2168
2182
2169
return dosingDecision
0 commit comments