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

Merge pull request #275 from kingst/setting-for-oref-swift

[Part 2 of 3] Add setting for using the Swift Oref algorithm
marv-out 1 год назад
Родитель
Сommit
347b068ded

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

@@ -130,7 +130,7 @@ final class BaseAPSManager: APSManager, Injectable {
             let wasParsed = storage.parseOnFileSettingsToMgdL()
             if wasParsed {
                 Task {
-                    await openAPS.createProfiles()
+                    await openAPS.createProfiles(useSwiftOref: settings.useSwiftOref)
                 }
             }
         }
@@ -390,7 +390,7 @@ final class BaseAPSManager: APSManager, Injectable {
             async let autosenseResult = autosense()
 
             _ = try await autosenseResult
-            await openAPS.createProfiles()
+            await openAPS.createProfiles(useSwiftOref: settings.useSwiftOref)
             let determination = try await openAPS.determineBasal(currentTemp: await currentTemp, clock: now)
 
             if let determination = determination {

+ 8 - 7
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -13,8 +13,6 @@ final class OpenAPS {
 
     let jsonConverter = JSONConverter()
 
-    private let enableNativeOref = false // TODO: Replace with a default-on setting
-
     init(storage: FileStorage) {
         self.storage = storage
     }
@@ -464,7 +462,7 @@ final class OpenAPS {
         }
     }
 
-    func createProfiles() async {
+    func createProfiles(useSwiftOref: Bool) async {
         debug(.openAPS, "Start creating pump profile and user profile")
 
         // Load required settings and profiles asynchronously
@@ -518,7 +516,8 @@ final class OpenAPS {
                 tempTargets: tempTargets,
                 model: model,
                 autotune: RawJSON.null,
-                freeaps: freeaps
+                freeaps: freeaps,
+                useSwiftOref: useSwiftOref
             )
 
             let profile = try await makeProfile(
@@ -531,7 +530,8 @@ final class OpenAPS {
                 tempTargets: tempTargets,
                 model: model,
                 autotune: RawJSON.null,
-                freeaps: freeaps
+                freeaps: freeaps,
+                useSwiftOref: useSwiftOref
             )
 
             // Save the profiles
@@ -728,7 +728,8 @@ final class OpenAPS {
         tempTargets: JSON,
         model: JSON,
         autotune: JSON,
-        freeaps: JSON
+        freeaps: JSON,
+        useSwiftOref: Bool
     ) async throws -> RawJSON {
         // TODO: Compare exceptions as well
         let startJavascriptAt = Date()
@@ -748,7 +749,7 @@ final class OpenAPS {
 
         // Important: we want to make sure that this flag ensures that none
         // of the native code runs
-        guard enableNativeOref else {
+        guard useSwiftOref else {
             return jsJson
         }
 

+ 5 - 0
FreeAPS/Sources/Models/FreeAPSSettings.swift

@@ -75,6 +75,7 @@ struct FreeAPSSettings: JSON, Equatable {
     var useLiveActivity: Bool = false
     var lockScreenView: LockScreenView = .simple
     var bolusShortcut: BolusShortcutLimit = .notAllowed
+    var useSwiftOref: Bool = false
 }
 
 extension FreeAPSSettings: Decodable {
@@ -327,6 +328,10 @@ extension FreeAPSSettings: Decodable {
             settings.bolusShortcut = bolusShortcut
         }
 
+        if let useSwiftOref = try? container.decode(Bool.self, forKey: .useSwiftOref) {
+            settings.useSwiftOref = useSwiftOref
+        }
+
         self = settings
     }
 }

+ 3 - 0
FreeAPS/Sources/Modules/AlgorithmAdvancedSettings/AlgorithmAdvancedSettingsStateModel.swift

@@ -21,6 +21,7 @@ extension AlgorithmAdvancedSettings {
         @Published var remainingCarbsFraction: Decimal = 1.0
         @Published var remainingCarbsCap: Decimal = 90
         @Published var noisyCGMTargetMultiplier: Decimal = 1.3
+        @Published var useSwiftOref: Bool = false
 
         var insulinActionCurve: Decimal = 10
 
@@ -45,6 +46,8 @@ extension AlgorithmAdvancedSettings {
             subscribePreferencesSetting(\.remainingCarbsCap, on: $remainingCarbsCap) { remainingCarbsCap = $0 }
             subscribePreferencesSetting(\.noisyCGMTargetMultiplier, on: $noisyCGMTargetMultiplier) {
                 noisyCGMTargetMultiplier = $0 }
+            subscribeSetting(\.useSwiftOref, on: $useSwiftOref) {
+                useSwiftOref = $0 }
 
             insulinActionCurve = pumpSettings.insulinActionCurve
         }

+ 26 - 0
FreeAPS/Sources/Modules/AlgorithmAdvancedSettings/View/AlgorithmAdvancedSettingsRootView.swift

@@ -338,6 +338,32 @@ extension AlgorithmAdvancedSettings {
                         Text("Note: A CGM is considered noisy when it provides inconsistent readings.")
                     }
                 )
+                SettingInputSection(
+                    decimalValue: $decimalPlaceholder,
+                    booleanValue: $state.useSwiftOref,
+                    shouldDisplayHint: $shouldDisplayHint,
+                    selectedVerboseHint: Binding(
+                        get: { selectedVerboseHint },
+                        set: {
+                            selectedVerboseHint = $0.map { AnyView($0) }
+                            hintLabel = NSLocalizedString("Use Swift Oref", comment: "Use Swift Oref")
+                        }
+                    ),
+                    units: state.units,
+                    type: .boolean,
+                    label: NSLocalizedString("Use Swift Oref", comment: "Use Swift Oref"),
+                    miniHint: "Use new Swift OpenAPS Oref algorithm",
+                    verboseHint:
+                    VStack(alignment: .leading, spacing: 10) {
+                        Text("Default: OFF").bold()
+                        Text(
+                            "This experimental option uses a new version of the OpenAPS Oref algorithm written directly in Swift programming language, replacing the current JavaScript version."
+                        )
+                        Text(
+                            "When enabled, we'll securely log anonymous technical data comparing the Swift and existing JavaScript algorithms to improve calculation accuracy."
+                        )
+                    }
+                )
             }
             .listSectionSpacing(sectionSpacing)
             .sheet(isPresented: $shouldDisplayHint) {