|
| 1 | +Submodule Loop contains modified content |
| 2 | +diff --git a/Loop/Loop/Managers/DeviceDataManager.swift b/Loop/Loop/Managers/DeviceDataManager.swift |
| 3 | +index 96452b2e..40938be0 100644 |
| 4 | +--- a/Loop/Loop/Managers/DeviceDataManager.swift |
| 5 | ++++ b/Loop/Loop/Managers/DeviceDataManager.swift |
| 6 | +@@ -47,6 +47,8 @@ final class DeviceDataManager { |
| 7 | + |
| 8 | + @Published var pumpIsAllowingAutomation: Bool |
| 9 | + |
| 10 | ++ private var lastCGMLoopTrigger: Date = .distantPast |
| 11 | ++ |
| 12 | + private let closedLoopStatus: ClosedLoopStatus |
| 13 | + |
| 14 | + var closedLoopDisallowedLocalizedDescription: String? { |
| 15 | +@@ -949,8 +951,14 @@ extension DeviceDataManager: CGMManagerDelegate { |
| 16 | + |
| 17 | + func cgmManager(_ manager: CGMManager, hasNew readingResult: CGMReadingResult) { |
| 18 | + dispatchPrecondition(condition: .onQueue(queue)) |
| 19 | ++ log.default("CGMManager:%{public}@ did update with %{public}@", String(describing: type(of: manager)), String(describing: readingResult)) |
| 20 | + processCGMReadingResult(manager, readingResult: readingResult) { |
| 21 | +- self.checkPumpDataAndLoop() |
| 22 | ++ let now = Date() |
| 23 | ++ if case .newData = readingResult, now.timeIntervalSince(self.lastCGMLoopTrigger) > .minutes(4.2) { |
| 24 | ++ self.log.default("Triggering loop from new CGM data at %{public}@", String(describing: now)) |
| 25 | ++ self.lastCGMLoopTrigger = now |
| 26 | ++ self.checkPumpDataAndLoop() |
| 27 | ++ } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | +@@ -1032,7 +1040,8 @@ extension DeviceDataManager: PumpManagerDelegate { |
| 32 | + |
| 33 | + self.queue.async { |
| 34 | + self.processCGMReadingResult(cgmManager, readingResult: result) { |
| 35 | +- if self.loopManager.lastLoopCompleted == nil || self.loopManager.lastLoopCompleted!.timeIntervalSinceNow < -.minutes(6) { |
| 36 | ++ if self.loopManager.lastLoopCompleted == nil || self.loopManager.lastLoopCompleted!.timeIntervalSinceNow < -.minutes(4.2) { |
| 37 | ++ self.log.default("Triggering Loop from refreshCGM()") |
| 38 | + self.checkPumpDataAndLoop() |
| 39 | + } |
| 40 | + completion?() |
| 41 | +diff --git a/Loop/Loop/Managers/LoopDataManager.swift b/Loop/Loop/Managers/LoopDataManager.swift |
| 42 | +index 18a08166..5da70aff 100644 |
| 43 | +--- a/Loop/Loop/Managers/LoopDataManager.swift |
| 44 | ++++ b/Loop/Loop/Managers/LoopDataManager.swift |
| 45 | +@@ -27,6 +27,8 @@ final class LoopDataManager { |
| 46 | + case loopFinished |
| 47 | + } |
| 48 | + |
| 49 | ++ let loopLock = UnfairLock() |
| 50 | ++ |
| 51 | + static let LoopUpdateContextKey = "com.loudnate.Loop.LoopDataManager.LoopUpdateContext" |
| 52 | + |
| 53 | + private let carbStore: CarbStoreProtocol |
| 54 | +@@ -824,8 +826,24 @@ extension LoopDataManager { |
| 55 | + /// |
| 56 | + /// Executes an analysis of the current data, and recommends an adjustment to the current |
| 57 | + /// temporary basal rate. |
| 58 | ++ /// |
| 59 | + func loop() { |
| 60 | +- |
| 61 | ++ |
| 62 | ++ if let lastLoopCompleted, Date().timeIntervalSince(lastLoopCompleted) < .minutes(2) { |
| 63 | ++ print("Looping too fast!") |
| 64 | ++ } |
| 65 | ++ |
| 66 | ++ let available = loopLock.withLockIfAvailable { |
| 67 | ++ loopInternal() |
| 68 | ++ return true |
| 69 | ++ } |
| 70 | ++ if available == nil { |
| 71 | ++ print("Loop attempted while already looping!") |
| 72 | ++ } |
| 73 | ++ } |
| 74 | ++ |
| 75 | ++ func loopInternal() { |
| 76 | ++ |
| 77 | + dataAccessQueue.async { |
| 78 | + |
| 79 | + // If time was changed to future time, and a loop completed, then time was fixed, lastLoopCompleted will prevent looping |
| 80 | +Submodule LoopKit contains modified content |
| 81 | +diff --git a/LoopKit/LoopKit/UnfairLock.swift b/LoopKit/LoopKit/UnfairLock.swift |
| 82 | +index 9741920..7ceae42 100644 |
| 83 | +--- a/LoopKit/LoopKit/UnfairLock.swift |
| 84 | ++++ b/LoopKit/LoopKit/UnfairLock.swift |
| 85 | +@@ -28,6 +28,15 @@ public class UnfairLock { |
| 86 | + return try f() |
| 87 | + } |
| 88 | + |
| 89 | ++ public func withLockIfAvailable<ReturnValue>(_ f: () throws -> ReturnValue) rethrows -> ReturnValue? { |
| 90 | ++ if os_unfair_lock_trylock(_lock) { |
| 91 | ++ defer { os_unfair_lock_unlock(_lock) } |
| 92 | ++ return try f() |
| 93 | ++ } else { |
| 94 | ++ return nil |
| 95 | ++ } |
| 96 | ++ } |
| 97 | ++ |
| 98 | + public func assertOwned() { |
| 99 | + os_unfair_lock_assert_owner(_lock) |
| 100 | + } |
0 commit comments