Просмотр исходного кода

Add code to copy basal rates to pump.

Jan Dittmer 4 лет назад
Родитель
Сommit
cfc8a9c279

+ 43 - 0
FreeAPS/Sources/Modules/AutotuneConfig/AutotuneConfigStateModel.swift

@@ -1,4 +1,5 @@
 import Combine
+import LoopKit
 import SwiftUI
 
 extension AutotuneConfig {
@@ -57,5 +58,47 @@ extension AutotuneConfig {
                 .cancellable()
                 .store(in: &lifetime)
         }
+
+        func copyBasal() {
+            guard let autotuneProfile = autotune?.basalProfile else {
+                NSLog("copyBasal failure - no profile")
+                return
+            }
+            guard let pump = provider.deviceManager?.pumpManager else {
+                // storage.save(profile, as: OpenAPS.Settings.basalProfile)
+                NSLog("copyBasal failure - no pump")
+                return
+            }
+            let profile = autotuneProfile.map {
+                BasalProfileEntry(
+                    start: $0.start,
+                    minutes: $0.minutes,
+                    // Round to 0.05, ie. 1/20th
+                    rate: Decimal(round(Double($0.rate) * 20) / 20)
+                )
+            }
+            for item in profile {
+                NSLog("\(item.minutes) \(item.rate)")
+            }
+            let syncValues = profile.map {
+                RepeatingScheduleValue(
+                    startTime: TimeInterval($0.minutes * 60),
+                    value: Double($0.rate)
+                )
+            }
+
+            for item in syncValues {
+                NSLog("\(item.startTime) \(item.value)")
+            }
+            pump.syncBasalRateSchedule(items: syncValues) { result in
+                switch result {
+                case .success:
+                    NSLog("copyBasal success")
+                    self.provider.storage.save(profile, as: OpenAPS.Settings.basalProfile)
+                case let .failure(error):
+                    NSLog("copyBasal failed \(error)")
+                }
+            }
+        }
     }
 }

+ 6 - 0
FreeAPS/Sources/Modules/AutotuneConfig/View/AutotuneConfigRootView.swift

@@ -79,6 +79,12 @@ extension AutotuneConfig {
                         label: { Text("Delete autotune data") }
                             .foregroundColor(.red)
                     }
+
+                    Section {
+                        Button { state.copyBasal() }
+                        label: { Text("Copy basal rates to pump") }
+                            .foregroundColor(.red)
+                    }
                 }
             }
             .onAppear(perform: configureView)