Skip to content

Commit

Permalink
Async rework 2.0 (nightscout#50)
Browse files Browse the repository at this point in the history
* make apsManager and openAPS async....highly wip

* cleanup

* clean up

* test

* cleanup

* small fixes for LA, refactoring

* refactoring of async functions

* cleanup

* async save function for Determination

* address PR feedback (better error handling)

---------

Co-authored-by: Marvin Polscheit <[email protected]>
  • Loading branch information
marv-out and Marvin Polscheit authored Jun 15, 2024
1 parent 0b95b9d commit f18f79d
Show file tree
Hide file tree
Showing 19 changed files with 869 additions and 886 deletions.
4 changes: 2 additions & 2 deletions FreeAPS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,6 @@
3811DEE725CA063400A708ED /* PersistedProperty.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersistedProperty.swift; sourceTree = "<group>"; };
3811DF0125CA9FEA00A708ED /* Credentials.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Credentials.swift; sourceTree = "<group>"; };
3811DF0F25CAAAE200A708ED /* APSManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APSManager.swift; sourceTree = "<group>"; };
3818AA42274BBC1100843DB3 /* ConfigOverride.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigOverride.xcconfig; sourceTree = "<group>"; };
3818AA45274C229000843DB3 /* LibreTransmitter */ = {isa = PBXFileReference; lastKnownFileType = folder; name = LibreTransmitter; path = Dependencies/LibreTransmitter; sourceTree = "<group>"; };
3818AA49274C267000843DB3 /* CGMBLEKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = CGMBLEKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3818AA4C274C26A300843DB3 /* LoopKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = LoopKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -953,6 +952,7 @@
BA49538D56989D8DA6FCF538 /* TargetsEditorDataFlow.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = TargetsEditorDataFlow.swift; sourceTree = "<group>"; };
BC210C0F3CB6D3C86E5DED4E /* LibreConfigRootView.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = LibreConfigRootView.swift; sourceTree = "<group>"; };
BD1661302B82ADAB00256551 /* CustomProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomProgressView.swift; sourceTree = "<group>"; };
BD1CF8B72C1A4A8400CB930A /* ConfigOverride.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigOverride.xcconfig; sourceTree = "<group>"; };
BD2FF19F2AE29D43005D1C5D /* CheckboxToggleStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckboxToggleStyle.swift; sourceTree = "<group>"; };
BD3CC0712B0B89D50013189E /* MainChartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainChartView.swift; sourceTree = "<group>"; };
BD7DA9A42AE06DFC00601B20 /* BolusCalculatorConfigDataFlow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BolusCalculatorConfigDataFlow.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1725,8 +1725,8 @@
isa = PBXGroup;
children = (
587A54C82BCDCE0F009D38E2 /* Model */,
BD1CF8B72C1A4A8400CB930A /* ConfigOverride.xcconfig */,
38F3783A2613555C009DB701 /* Config.xcconfig */,
3818AA42274BBC1100843DB3 /* ConfigOverride.xcconfig */,
388E595A25AD948C0019842D /* FreeAPS */,
38FCF3EE25E9028E0078B0D1 /* FreeAPSTests */,
3818AA44274C229000843DB3 /* Packages */,
Expand Down
1,074 changes: 584 additions & 490 deletions FreeAPS/Sources/APS/APSManager.swift

Large diffs are not rendered by default.

47 changes: 0 additions & 47 deletions FreeAPS/Sources/APS/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ private let staticPumpManagersByIdentifier: [String: PumpManagerUI.Type] = [
MockPumpManager.managerIdentifier: MockPumpManager.self
]

// private let staticPumpManagersByIdentifier: [String: PumpManagerUI.Type] = staticPumpManagers.reduce(into: [:]) { map, Type in
// map[Type.managerIdentifier] = Type
// }

private let accessLock = NSRecursiveLock(label: "BaseDeviceDataManager.accessLock")

final class BaseDeviceDataManager: DeviceDataManager, Injectable {
Expand Down Expand Up @@ -164,20 +160,6 @@ final class BaseDeviceDataManager: DeviceDataManager, Injectable {
self.updateUpdateFinished(true)
}
}

// pumpUpdateCancellable = Future<Bool, Never> { [unowned self] promise in
// pumpUpdatePromise = promise
// debug(.deviceManager, "Waiting for pump update and loop recommendation")
// processQueue.safeSync {
// pumpManager.ensureCurrentPumpData { _ in
// debug(.deviceManager, "Pump data updated.")
// }
// }
// }
// .timeout(30, scheduler: processQueue)
// .replaceError(with: false)
// .replaceEmpty(with: false)
// .sink(receiveValue: updateUpdateFinished)
}

private func updateUpdateFinished(_ recommendsLoop: Bool) {
Expand All @@ -186,12 +168,6 @@ final class BaseDeviceDataManager: DeviceDataManager, Injectable {
if !recommendsLoop {
warning(.deviceManager, "Loop recommendation time out or got error. Trying to loop right now.")
}

// directly in loop() function
// guard !loopInProgress else {
// warning(.deviceManager, "Loop already in progress. Skip recommendation.")
// return
// }
self.recommendsLoop.send()
}

Expand Down Expand Up @@ -340,29 +316,6 @@ extension BaseDeviceDataManager: PumpManagerDelegate {
}

let batteryPercent = Int((status.pumpBatteryChargeRemaining ?? 1) * 100)
let battery = Battery(
percent: batteryPercent,
voltage: nil,
string: batteryPercent >= 10 ? .normal : .low,
display: pumpManager.status.pumpBatteryChargeRemaining != nil
)

privateContext.perform {
let batteryToStore = OpenAPS_Battery(context: self.privateContext)
batteryToStore.id = UUID()
batteryToStore.date = Date()
batteryToStore.percent = Int16(batteryPercent)
batteryToStore.voltage = nil
batteryToStore.status = batteryPercent > 10 ? "normal" : "low"
batteryToStore.display = status.pumpBatteryChargeRemaining != nil

do {
guard self.privateContext.hasChanges else { return }
try self.privateContext.save()
} catch {
print(error.localizedDescription)
}
}
broadcaster.notify(PumpTimeZoneObserver.self, on: processQueue) {
$0.pumpTimeZoneDidChange(status.timeZone)
}
Expand Down
Loading

0 comments on commit f18f79d

Please sign in to comment.