@@ -36,7 +36,7 @@ final class DeviceDataManager {
3636
3737 private var nightscoutDataManager : NightscoutDataManager !
3838
39- var latestPumpStatus : RileyLinkKit . PumpStatus ?
39+ fileprivate var latestPumpStatus : RileyLinkKit . PumpStatus ?
4040
4141 private( set) var lastError : ( date: Date , error: Error ) ?
4242
@@ -61,7 +61,7 @@ final class DeviceDataManager {
6161 }
6262
6363 // Battery monitor
64- func observeBatteryDuring( _ block: ( ) -> Void ) {
64+ fileprivate func observeBatteryDuring( _ block: ( ) -> Void ) {
6565 let oldVal = pumpBatteryChargeRemaining
6666 block ( )
6767 if let newVal = pumpBatteryChargeRemaining {
@@ -79,6 +79,17 @@ final class DeviceDataManager {
7979
8080 @objc private func receivedRileyLinkManagerNotification( _ note: Notification ) {
8181 NotificationCenter . default. post ( name: note. name, object: self , userInfo: note. userInfo)
82+
83+ switch note. name {
84+ case Notification . Name. DeviceConnectionStateDidChange,
85+ Notification . Name. DeviceNameDidChange:
86+ // Update the HKDevice to include the name or connection status change
87+ if let device = rileyLinkManager. firstConnectedDevice? . device {
88+ loopManager. doseStore. setDevice ( device)
89+ }
90+ default :
91+ break
92+ }
8293 }
8394
8495 /**
@@ -136,9 +147,45 @@ final class DeviceDataManager {
136147 }
137148 }
138149
150+ fileprivate func updateTimerTickPreference( ) {
151+ /// Controls the management of the RileyLink timer tick, which is a reliably-changing BLE
152+ /// characteristic which can cause the app to wake. For most users, the G5 Transmitter and
153+ /// G4 Receiver are reliable as hearbeats, but users who find their resources extremely constrained
154+ /// due to greedy apps or older devices may choose to always enable the timer by always setting `true`
155+ rileyLinkManager. timerTickEnabled = pumpDataIsStale ( ) || !( cgmManager? . providesBLEHeartbeat == true )
156+ }
157+
158+ /**
159+ Attempts to fix an extended communication failure between a RileyLink device and the pump
160+
161+ - parameter device: The RileyLink device
162+ */
163+ private func troubleshootPumpComms( using device: RileyLinkDevice ) {
164+ // Ensuring timer tick is enabled will allow more tries to bring the pump data up-to-date.
165+ updateTimerTickPreference ( )
166+
167+ // How long we should wait before we re-tune the RileyLink
168+ let tuneTolerance = TimeInterval ( minutes: 14 )
169+
170+ if device. lastTuned == nil || device. lastTuned!. timeIntervalSinceNow <= - tuneTolerance {
171+ device. tunePump { ( result) in
172+ switch result {
173+ case . success( let scanResult) :
174+ self . logger. addError ( " Device \( device. name ?? " " ) auto-tuned to \( scanResult. bestFrequency) MHz " , fromSource: " RileyLink " )
175+ case . failure( let error) :
176+ self . logger. addError ( " Device \( device. name ?? " " ) auto-tune failed with error: \( error) " , fromSource: " RileyLink " )
177+ self . rileyLinkManager. deprioritizeDevice ( device: device)
178+ self . setLastError ( error: error)
179+ }
180+ }
181+ } else {
182+ rileyLinkManager. deprioritizeDevice ( device: device)
183+ }
184+ }
185+
139186 // MARK: Pump data
140187
141- var latestPumpStatusFromMySentry : MySentryPumpStatusMessageBody ?
188+ fileprivate var latestPumpStatusFromMySentry : MySentryPumpStatusMessageBody ?
142189
143190 /**
144191 Handles receiving a MySentry status message, which are only posted by MM x23 pumps.
@@ -266,6 +313,9 @@ final class DeviceDataManager {
266313 }
267314 }
268315 }
316+
317+ // New reservoir data means we may want to adjust our timer tick requirements
318+ self . updateTimerTickPreference ( )
269319 }
270320 }
271321
@@ -303,7 +353,7 @@ final class DeviceDataManager {
303353
304354 private func pumpDataIsStale( ) -> Bool {
305355 // How long should we wait before we poll for new pump data?
306- let pumpStatusAgeTolerance = rileyLinkManager. idleListeningEnabled ? TimeInterval ( minutes: 11 ) : TimeInterval ( minutes: 4 )
356+ let pumpStatusAgeTolerance = rileyLinkManager. idleListeningEnabled ? TimeInterval ( minutes: 9 ) : TimeInterval ( minutes: 4 )
307357
308358 return loopManager. doseStore. lastReservoirValue == nil
309359 || loopManager. doseStore. lastReservoirValue!. startDate. timeIntervalSinceNow <= - pumpStatusAgeTolerance
@@ -421,31 +471,6 @@ final class DeviceDataManager {
421471 }
422472 }
423473
424- /**
425- Attempts to fix an extended communication failure between a RileyLink device and the pump
426-
427- - parameter device: The RileyLink device
428- */
429- private func troubleshootPumpComms( using device: RileyLinkDevice ) {
430- // How long we should wait before we re-tune the RileyLink
431- let tuneTolerance = TimeInterval ( minutes: 14 )
432-
433- if device. lastTuned == nil || device. lastTuned!. timeIntervalSinceNow <= - tuneTolerance {
434- device. tunePump { ( result) in
435- switch result {
436- case . success( let scanResult) :
437- self . logger. addError ( " Device \( device. name ?? " " ) auto-tuned to \( scanResult. bestFrequency) MHz " , fromSource: " RileyLink " )
438- case . failure( let error) :
439- self . logger. addError ( " Device \( device. name ?? " " ) auto-tune failed with error: \( error) " , fromSource: " RileyLink " )
440- self . rileyLinkManager. deprioritizeDevice ( device: device)
441- self . setLastError ( error: error)
442- }
443- }
444- } else {
445- rileyLinkManager. deprioritizeDevice ( device: device)
446- }
447- }
448-
449474 // MARK: - CGM
450475
451476 var cgm : CGM ? = UserDefaults . standard. cgm {
@@ -465,11 +490,7 @@ final class DeviceDataManager {
465490 cgmManager? . delegate = self
466491 loopManager. glucoseStore. managedDataInterval = cgmManager? . managedDataInterval
467492
468- /// Controls the management of the RileyLink timer tick, which is a reliably-changing BLE
469- /// characteristic which can cause the app to wake. For most users, the G5 Transmitter and
470- /// G4 Receiver are reliable as hearbeats, but users who find their resources extremely constrained
471- /// due to greedy apps or older devices may choose to always enable the timer by always setting `true`
472- rileyLinkManager. timerTickEnabled = !( cgmManager? . providesBLEHeartbeat == true )
493+ updateTimerTickPreference ( )
473494 }
474495
475496 var sensorInfo : SensorDisplayable ? {
@@ -545,6 +566,16 @@ final class DeviceDataManager {
545566 rileyLinkManager. idleListeningEnabled = false
546567 }
547568
569+ // Update the HKDevice to include the model change
570+ if let device = rileyLinkManager. firstConnectedDevice? . device {
571+ loopManager. doseStore. setDevice ( device)
572+ }
573+
574+ // Update the preference for basal profile start events
575+ if let recordsBasalProfileStartEvents = pumpState? . pumpModel? . recordsBasalProfileStartEvents {
576+ loopManager. doseStore. pumpRecordsBasalProfileStartEvents = recordsBasalProfileStartEvents
577+ }
578+
548579 UserDefaults . standard. pumpModelNumber = pumpState? . pumpModel? . rawValue
549580 case " pumpRegion " ? :
550581 UserDefaults . standard. pumpRegion = pumpState? . pumpRegion
@@ -630,6 +661,10 @@ final class DeviceDataManager {
630661
631662 loopManager. carbStore. syncDelegate = remoteDataManager. nightscoutService. uploader
632663 loopManager. doseStore. delegate = self
664+ // Proliferate PumpModel preferences to DoseStore
665+ if let pumpModel = pumpState? . pumpModel {
666+ loopManager. doseStore. pumpRecordsBasalProfileStartEvents = pumpModel. recordsBasalProfileStartEvents
667+ }
633668
634669 setupCGM ( )
635670 }
0 commit comments