@@ -76,6 +76,7 @@ final class LoopDataManager {
7676 center. addObserver ( forName: . CarbEntriesDidUpdate, object: nil , queue: nil ) { ( note) -> Void in
7777 self . dataAccessQueue. async {
7878 self . carbEffect = nil
79+ self . carbsOnBoardSeries = nil
7980 self . notify ( forChange: . carbs)
8081 }
8182 }
@@ -165,6 +166,18 @@ final class LoopDataManager {
165166 }
166167 }
167168
169+ if carbsOnBoardSeries == nil , let carbStore = deviceDataManager. carbStore {
170+ updateGroup. enter ( )
171+ carbStore. getCarbsOnBoardValues { ( values, error) in
172+ if let error = error {
173+ self . deviceDataManager. logger. addError ( error, fromSource: " CarbStore " )
174+ }
175+
176+ self . carbsOnBoardSeries = values
177+ updateGroup. leave ( )
178+ }
179+ }
180+
168181 if insulinEffect == nil {
169182 updateGroup. enter ( )
170183 updateInsulinEffect { ( effects, error) in
@@ -229,9 +242,10 @@ final class LoopDataManager {
229242 - lastTempBasal: The last set temp basal
230243 - lastLoopCompleted: The last date at which a loop completed, from prediction to dose (if dosing is enabled)
231244 - insulinOnBoard Current insulin on board
245+ - carbsOnBoard Current carbs on board
232246 - error: An error in the current state of the loop, or one that happened during the last attempt to loop.
233247 */
234- func getLoopStatus( _ resultsHandler: @escaping ( _ predictedGlucose: [ GlucoseValue ] ? , _ retrospectivePredictedGlucose: [ GlucoseValue ] ? , _ recommendedTempBasal: TempBasalRecommendation ? , _ lastTempBasal: DoseEntry ? , _ lastLoopCompleted: Date ? , _ insulinOnBoard: InsulinValue ? , _ error: Error ? ) -> Void ) {
248+ func getLoopStatus( _ resultsHandler: @escaping ( _ predictedGlucose: [ GlucoseValue ] ? , _ retrospectivePredictedGlucose: [ GlucoseValue ] ? , _ recommendedTempBasal: TempBasalRecommendation ? , _ lastTempBasal: DoseEntry ? , _ lastLoopCompleted: Date ? , _ insulinOnBoard: InsulinValue ? , _ carbsOnBoard : CarbValue ? , _ error: Error ? ) -> Void ) {
235249 dataAccessQueue. async {
236250 var error : Error ?
237251
@@ -241,7 +255,9 @@ final class LoopDataManager {
241255 error = updateError
242256 }
243257
244- resultsHandler ( self . predictedGlucose, self . retrospectivePredictedGlucose, self . recommendedTempBasal, self . lastTempBasal, self . lastLoopCompleted, self . insulinOnBoard, error ?? self . lastLoopError)
258+ let currentCOB = self . carbsOnBoardSeries? . closestPriorToDate ( Date ( ) )
259+
260+ resultsHandler ( self . predictedGlucose, self . retrospectivePredictedGlucose, self . recommendedTempBasal, self . lastTempBasal, self . lastLoopCompleted, self . insulinOnBoard, currentCOB, error ?? self . lastLoopError)
245261 }
246262 }
247263
@@ -294,6 +310,7 @@ final class LoopDataManager {
294310 retrospectivePredictedGlucose = nil
295311 }
296312 }
313+ private var carbsOnBoardSeries : [ CarbValue ] ?
297314 private var insulinEffect : [ GlucoseEffect ] ? {
298315 didSet {
299316 if let bolusDate = lastBolus? . date, bolusDate. timeIntervalSinceNow < TimeInterval ( minutes: - 5 ) {
@@ -544,6 +561,7 @@ final class LoopDataManager {
544561 self . dataAccessQueue. async {
545562 if success {
546563 self . carbEffect = nil
564+ self . carbsOnBoardSeries = nil
547565
548566 do {
549567 try self . update ( )
@@ -673,7 +691,7 @@ extension LoopDataManager {
673691 ///
674692 /// - parameter completionHandler: A closure called once the report has been generated. The closure takes a single argument of the report string.
675693 func generateDiagnosticReport( _ completionHandler: @escaping ( _ report: String ) -> Void ) {
676- getLoopStatus { ( predictedGlucose, retrospectivePredictedGlucose, recommendedTempBasal, lastTempBasal, lastLoopCompleted, insulinOnBoard, error) in
694+ getLoopStatus { ( predictedGlucose, retrospectivePredictedGlucose, recommendedTempBasal, lastTempBasal, lastLoopCompleted, insulinOnBoard, carbsOnBoard , error) in
677695 let report = [
678696 " ## LoopDataManager " ,
679697 " predictedGlucose: \( predictedGlucose ?? [ ] ) " ,
@@ -682,6 +700,7 @@ extension LoopDataManager {
682700 " lastTempBasal: \( lastTempBasal) " ,
683701 " lastLoopCompleted: \( lastLoopCompleted ?? . distantPast) " ,
684702 " insulinOnBoard: \( insulinOnBoard) " ,
703+ " carbsOnBoard: \( carbsOnBoard) " ,
685704 " error: \( error) "
686705 ]
687706 completionHandler ( report. joined ( separator: " \n " ) )
0 commit comments