@@ -45,6 +45,8 @@ final class DeviceDataManager {
45
45
46
46
@Published var pumpIsAllowingAutomation : Bool
47
47
48
+ private var lastCGMLoopTrigger : Date = . distantPast
49
+
48
50
private let automaticDosingStatus : AutomaticDosingStatus
49
51
50
52
var closedLoopDisallowedLocalizedDescription : String ? {
@@ -557,7 +559,6 @@ final class DeviceDataManager {
557
559
private func processCGMReadingResult( _ manager: CGMManager , readingResult: CGMReadingResult , completion: @escaping ( ) -> Void ) {
558
560
switch readingResult {
559
561
case . newData( let values) :
560
- log. default ( " CGMManager:%{public}@ did update with %d values " , String ( describing: type ( of: manager) ) , values. count)
561
562
loopManager. addGlucoseSamples ( values) { result in
562
563
if !values. isEmpty {
563
564
DispatchQueue . main. async {
@@ -570,10 +571,8 @@ final class DeviceDataManager {
570
571
loopManager. receivedUnreliableCGMReading ( )
571
572
completion ( )
572
573
case . noData:
573
- log. default ( " CGMManager:%{public}@ did update with no data " , String ( describing: type ( of: manager) ) )
574
574
completion ( )
575
575
case . error( let error) :
576
- log. default ( " CGMManager:%{public}@ did update with error: %{public}@ " , String ( describing: type ( of: manager) ) , String ( describing: error) )
577
576
self . setLastError ( error: error)
578
577
completion ( )
579
578
}
@@ -924,8 +923,14 @@ extension DeviceDataManager: CGMManagerDelegate {
924
923
925
924
func cgmManager( _ manager: CGMManager , hasNew readingResult: CGMReadingResult ) {
926
925
dispatchPrecondition ( condition: . onQueue( queue) )
926
+ log. default ( " CGMManager:%{public}@ did update with %{public}@ " , String ( describing: type ( of: manager) ) , String ( describing: readingResult) )
927
927
processCGMReadingResult ( manager, readingResult: readingResult) {
928
- self . checkPumpDataAndLoop ( )
928
+ let now = Date ( )
929
+ if case . newData = readingResult, now. timeIntervalSince ( self . lastCGMLoopTrigger) > . minutes( 4.2 ) {
930
+ self . log. default ( " Triggering loop from new CGM data at %{public}@ " , String ( describing: now) )
931
+ self . lastCGMLoopTrigger = now
932
+ self . checkPumpDataAndLoop ( )
933
+ }
929
934
}
930
935
}
931
936
@@ -1012,7 +1017,8 @@ extension DeviceDataManager: PumpManagerDelegate {
1012
1017
1013
1018
self . queue. async {
1014
1019
self . processCGMReadingResult ( cgmManager, readingResult: result) {
1015
- if self . loopManager. lastLoopCompleted == nil || self . loopManager. lastLoopCompleted!. timeIntervalSinceNow < - . minutes ( 6 ) {
1020
+ if self . loopManager. lastLoopCompleted == nil || self . loopManager. lastLoopCompleted!. timeIntervalSinceNow < - . minutes ( 4.2 ) {
1021
+ self . log. default ( " Triggering Loop from refreshCGM() " )
1016
1022
self . checkPumpDataAndLoop ( )
1017
1023
}
1018
1024
completion ? ( )
0 commit comments