Skip to content

Commit

Permalink
Only trigger when new CGM data received > 4.2 minutes ago. (#2039)
Browse files Browse the repository at this point in the history
* Only trigger when new CGM data received > 4.2 minutes ago.

* Change errors to prints, still for testing

* Allow pump driven loop at < 5 minutes intervals
  • Loading branch information
ps2 authored Aug 9, 2023
1 parent c1e5330 commit bdf9ca3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 11 additions & 5 deletions Loop/Managers/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ final class DeviceDataManager {

@Published var pumpIsAllowingAutomation: Bool

private var lastCGMLoopTrigger: Date = .distantPast

private let automaticDosingStatus: AutomaticDosingStatus

var closedLoopDisallowedLocalizedDescription: String? {
Expand Down Expand Up @@ -557,7 +559,6 @@ final class DeviceDataManager {
private func processCGMReadingResult(_ manager: CGMManager, readingResult: CGMReadingResult, completion: @escaping () -> Void) {
switch readingResult {
case .newData(let values):
log.default("CGMManager:%{public}@ did update with %d values", String(describing: type(of: manager)), values.count)
loopManager.addGlucoseSamples(values) { result in
if !values.isEmpty {
DispatchQueue.main.async {
Expand All @@ -570,10 +571,8 @@ final class DeviceDataManager {
loopManager.receivedUnreliableCGMReading()
completion()
case .noData:
log.default("CGMManager:%{public}@ did update with no data", String(describing: type(of: manager)))
completion()
case .error(let error):
log.default("CGMManager:%{public}@ did update with error: %{public}@", String(describing: type(of: manager)), String(describing: error))
self.setLastError(error: error)
completion()
}
Expand Down Expand Up @@ -924,8 +923,14 @@ extension DeviceDataManager: CGMManagerDelegate {

func cgmManager(_ manager: CGMManager, hasNew readingResult: CGMReadingResult) {
dispatchPrecondition(condition: .onQueue(queue))
log.default("CGMManager:%{public}@ did update with %{public}@", String(describing: type(of: manager)), String(describing: readingResult))
processCGMReadingResult(manager, readingResult: readingResult) {
self.checkPumpDataAndLoop()
let now = Date()
if case .newData = readingResult, now.timeIntervalSince(self.lastCGMLoopTrigger) > .minutes(4.2) {
self.log.default("Triggering loop from new CGM data at %{public}@", String(describing: now))
self.lastCGMLoopTrigger = now
self.checkPumpDataAndLoop()
}
}
}

Expand Down Expand Up @@ -1012,7 +1017,8 @@ extension DeviceDataManager: PumpManagerDelegate {

self.queue.async {
self.processCGMReadingResult(cgmManager, readingResult: result) {
if self.loopManager.lastLoopCompleted == nil || self.loopManager.lastLoopCompleted!.timeIntervalSinceNow < -.minutes(6) {
if self.loopManager.lastLoopCompleted == nil || self.loopManager.lastLoopCompleted!.timeIntervalSinceNow < -.minutes(4.2) {
self.log.default("Triggering Loop from refreshCGM()")
self.checkPumpDataAndLoop()
}
completion?()
Expand Down
18 changes: 18 additions & 0 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class LoopDataManager {
case loopFinished
}

let loopLock = UnfairLock()

static let LoopUpdateContextKey = "com.loudnate.Loop.LoopDataManager.LoopUpdateContext"

private let carbStore: CarbStoreProtocol
Expand Down Expand Up @@ -840,7 +842,23 @@ extension LoopDataManager {
///
/// Executes an analysis of the current data, and recommends an adjustment to the current
/// temporary basal rate.
///
func loop() {

if let lastLoopCompleted, Date().timeIntervalSince(lastLoopCompleted) < .minutes(2) {
print("Looping too fast!")
}

let available = loopLock.withLockIfAvailable {
loopInternal()
return true
}
if available == nil {
print("Loop attempted while already looping!")
}
}

func loopInternal() {

dataAccessQueue.async {

Expand Down

0 comments on commit bdf9ca3

Please sign in to comment.