Explorar o código

show pump basal profile if no looping

Ivan Valkou %!s(int64=5) %!d(string=hai) anos
pai
achega
885e957e35

+ 2 - 2
FreeAPS/Sources/APS/APSManager.swift

@@ -463,7 +463,7 @@ final class BaseAPSManager: APSManager, Injectable {
         guard let pump = pumpManager, verifyStatus() else {
             isLooping.send(false)
             debug(.apsManager, "Invalid pump state")
-            processError(APSError.invalidPumpState(message: "Pump is bolusing or suspended"))
+            processError(APSError.invalidPumpState(message: "Pump is busy, suspended or not set"))
             return
         }
 
@@ -493,7 +493,7 @@ final class BaseAPSManager: APSManager, Injectable {
             .flatMap { bolusPublisher }
             .sink { [weak self] completion in
                 if case let .failure(error) = completion {
-                    debug(.apsManager, "Loop failed with error: \(error.localizedDescription)")
+                    warning(.apsManager, "Loop failed with error: \(error.localizedDescription)")
                     self?.reportEnacted(suggestion: suggested, received: false)
                     self?.processError(APSError.pumpError(error))
                 } else {

+ 1 - 0
FreeAPS/Sources/Modules/Home/HomeDataFlow.swift

@@ -12,6 +12,7 @@ protocol HomeProvider: Provider {
     func filteredGlucose(hours: Int) -> [BloodGlucose]
     func pumpHistory(hours: Int) -> [PumpHistoryEvent]
     func pumpSettings() -> PumpSettings
+    func autotunedBasalProfile() -> [BasalProfileEntry]
     func basalProfile() -> [BasalProfileEntry]
     func tempTargets(hours: Int) -> [TempTarget]
     func carbs(hours: Int) -> [CarbsEntry]

+ 6 - 1
FreeAPS/Sources/Modules/Home/HomeProvider.swift

@@ -64,10 +64,15 @@ extension Home {
             storage.retrieve(OpenAPS.Monitor.reservoir, as: Decimal.self)
         }
 
-        func basalProfile() -> [BasalProfileEntry] {
+        func autotunedBasalProfile() -> [BasalProfileEntry] {
             storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self)?.basalProfile
                 ?? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
                 ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
         }
+
+        func basalProfile() -> [BasalProfileEntry] {
+            storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
+                ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
+        }
     }
 }

+ 2 - 0
FreeAPS/Sources/Modules/Home/HomeViewModel.swift

@@ -20,6 +20,7 @@ extension Home {
         @Published var boluses: [PumpHistoryEvent] = []
         @Published var suspensions: [PumpHistoryEvent] = []
         @Published var maxBasal: Decimal = 2
+        @Published var autotunedBasalProfile: [BasalProfileEntry] = []
         @Published var basalProfile: [BasalProfileEntry] = []
         @Published var tempTargets: [TempTarget] = []
         @Published var carbs: [CarbsEntry] = []
@@ -193,6 +194,7 @@ extension Home {
 
         private func setupBasalProfile() {
             DispatchQueue.main.async {
+                self.autotunedBasalProfile = self.provider.autotunedBasalProfile()
                 self.basalProfile = self.provider.basalProfile()
             }
         }

+ 27 - 12
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -40,6 +40,7 @@ struct MainChartView: View {
     @Binding var suspensions: [PumpHistoryEvent]
     @Binding var hours: Int
     @Binding var maxBasal: Decimal
+    @Binding var autotunedBasalProfile: [BasalProfileEntry]
     @Binding var basalProfile: [BasalProfileEntry]
     @Binding var tempTargets: [TempTarget]
     @Binding var carbs: [CarbsEntry]
@@ -179,7 +180,7 @@ struct MainChartView: View {
         .onChange(of: maxBasal) { _ in
             calculateBasalPoints(fullSize: fullSize)
         }
-        .onChange(of: basalProfile) { _ in
+        .onChange(of: autotunedBasalProfile) { _ in
             calculateBasalPoints(fullSize: fullSize)
         }
         .onChange(of: didAppearTrigger) { _ in
@@ -465,7 +466,8 @@ extension MainChartView {
             let firstRegularBasalPoints = findRegularBasalPoints(
                 timeBegin: dayAgoTime,
                 timeEnd: firstTempTime,
-                fullSize: fullSize
+                fullSize: fullSize,
+                autotuned: false
             )
             let tempBasalPoints = firstRegularBasalPoints + tempBasals.chunks(ofCount: 2).map { chunk -> [CGPoint] in
                 let chunk = Array(chunk)
@@ -475,7 +477,12 @@ extension MainChartView {
                 let rateCost = Config.basalHeight / CGFloat(maxBasalRate())
                 let x0 = timeToXCoordinate(timeBegin, fullSize: fullSize)
                 let y0 = Config.basalHeight - CGFloat(chunk[0].rate ?? 0) * rateCost
-                let regularPoints = findRegularBasalPoints(timeBegin: lastTimeEnd, timeEnd: timeBegin, fullSize: fullSize)
+                let regularPoints = findRegularBasalPoints(
+                    timeBegin: lastTimeEnd,
+                    timeEnd: timeBegin,
+                    fullSize: fullSize,
+                    autotuned: false
+                )
                 lastTimeEnd = timeEnd
                 return regularPoints + [CGPoint(x: x0, y: y0)]
             }.flatMap { $0 }
@@ -495,17 +502,18 @@ extension MainChartView {
             }
 
             let endDateTime = dayAgoTime + 1.days.timeInterval + 6.hours.timeInterval
-            let regularBasalPoints = findRegularBasalPoints(
+            let autotunedBasalPoints = findRegularBasalPoints(
                 timeBegin: dayAgoTime,
                 timeEnd: endDateTime,
-                fullSize: fullSize
+                fullSize: fullSize,
+                autotuned: true
             )
 
-            let regularBasalPath = Path { path in
+            let autotunedBasalPath = Path { path in
                 var yPoint: CGFloat = Config.basalHeight
                 path.move(to: CGPoint(x: -50, y: yPoint))
 
-                for point in regularBasalPoints {
+                for point in autotunedBasalPoints {
                     path.addLine(to: CGPoint(x: point.x, y: yPoint))
                     path.addLine(to: point)
                     yPoint = point.y
@@ -515,7 +523,7 @@ extension MainChartView {
 
             DispatchQueue.main.async {
                 self.tempBasalPath = tempBasalPath
-                self.regularBasalPath = regularBasalPath
+                self.regularBasalPath = autotunedBasalPath
             }
         }
     }
@@ -602,7 +610,12 @@ extension MainChartView {
         }
     }
 
-    private func findRegularBasalPoints(timeBegin: TimeInterval, timeEnd: TimeInterval, fullSize: CGSize) -> [CGPoint] {
+    private func findRegularBasalPoints(
+        timeBegin: TimeInterval,
+        timeEnd: TimeInterval,
+        fullSize: CGSize,
+        autotuned: Bool
+    ) -> [CGPoint] {
         guard timeBegin < timeEnd else {
             return []
         }
@@ -610,17 +623,19 @@ extension MainChartView {
         let calendar = Calendar.current
         let startOfDay = calendar.startOfDay(for: beginDate)
 
-        let basalNormalized = basalProfile.map {
+        let profile = autotuned ? autotunedBasalProfile : basalProfile
+
+        let basalNormalized = profile.map {
             (
                 time: startOfDay.addingTimeInterval($0.minutes.minutes.timeInterval).timeIntervalSince1970,
                 rate: $0.rate
             )
-        } + basalProfile.map {
+        } + profile.map {
             (
                 time: startOfDay.addingTimeInterval($0.minutes.minutes.timeInterval + 1.days.timeInterval).timeIntervalSince1970,
                 rate: $0.rate
             )
-        } + basalProfile.map {
+        } + profile.map {
             (
                 time: startOfDay.addingTimeInterval($0.minutes.minutes.timeInterval + 2.days.timeInterval).timeIntervalSince1970,
                 rate: $0.rate

+ 1 - 0
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -197,6 +197,7 @@ extension Home {
                         suspensions: $viewModel.suspensions,
                         hours: .constant(viewModel.filteredHours),
                         maxBasal: $viewModel.maxBasal,
+                        autotunedBasalProfile: $viewModel.autotunedBasalProfile,
                         basalProfile: $viewModel.basalProfile,
                         tempTargets: $viewModel.tempTargets,
                         carbs: $viewModel.carbs,