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

Remove JSON marshalling for makeProfile

This commit removes the JSON marshalling for makeProfile. Note however
that most of the data that makeProfile consumes is stored in JSON and
its result is also stored in JSON, so this commit is largely about
separation of responsibilities and making our algorithm functions use
native types even though the JSON encoding / decoding is still
happening.

We did get a minor win by removing double encoding / decoding for the
Preferences struct and we will get big benefits later when we take on
glucose / pump events carbs. But for now this change is mostly a
refactor.
Sam King 1 день назад
Родитель
Сommit
3ee632e8e6

+ 16 - 40
Trio/Sources/APS/OpenAPS/OpenAPS.swift

@@ -625,17 +625,15 @@ final class OpenAPS {
         async let getCR = loadFileFromStorageAsync(name: Settings.carbRatios)
         async let getTempTargets = loadFileFromStorageAsync(name: Settings.tempTargets)
         async let getModel = loadFileFromStorageAsync(name: Settings.model)
-        async let getTrioSettingDefaults = loadFileFromStorageAsync(name: Trio.settings)
 
-        let (pumpSettings, bgTargets, basalProfile, isf, cr, tempTargets, model, trioSettings) = await (
+        let (pumpSettings, bgTargets, basalProfile, isf, cr, tempTargets, model) = await (
             getPumpSettings,
             getBGTargets,
             getBasalProfile,
             getISF,
             getCR,
             getTempTargets,
-            getModel,
-            getTrioSettingDefaults
+            getModel
         )
 
         // Retrieve user preferences, or set defaults if not available
@@ -680,31 +678,37 @@ final class OpenAPS {
 
         let clock = Date()
         do {
-            let pumpProfile = try makeProfile(
+            // Decode the raw settings into native models. The bundled-defaults
+            // fallback still happens in loadFileFromStorageAsync above, so decoding
+            // here preserves the same behavior it previously had inside makeProfile.
+            let pumpSettings = try JSONBridge.pumpSettings(from: pumpSettings)
+            let bgTargets = try JSONBridge.bgTargets(from: bgTargets)
+            let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
+            let isf = try JSONBridge.insulinSensitivities(from: isf)
+            let carbRatio = try JSONBridge.carbRatios(from: cr)
+            let tempTargets = try JSONBridge.tempTargets(from: tempTargets)
+
+            let pumpProfile = try OpenAPSSwift.makeProfile(
                 preferences: adjustedPreferences,
                 pumpSettings: pumpSettings,
                 bgTargets: bgTargets,
                 basalProfile: basalProfile,
                 isf: isf,
-                carbRatio: cr,
+                carbRatio: carbRatio,
                 tempTargets: tempTargets,
                 model: model,
-                autotune: RawJSON.null,
-                trioSettings: trioSettings,
                 clock: clock
             )
 
-            let profile = try makeProfile(
+            let profile = try OpenAPSSwift.makeProfile(
                 preferences: adjustedPreferences,
                 pumpSettings: pumpSettings,
                 bgTargets: bgTargets,
                 basalProfile: basalProfile,
                 isf: isf,
-                carbRatio: cr,
+                carbRatio: carbRatio,
                 tempTargets: tempTargets,
                 model: model,
-                autotune: RawJSON.null,
-                trioSettings: trioSettings,
                 clock: clock
             )
 
@@ -811,34 +815,6 @@ final class OpenAPS {
         return try swiftResult.returnOrThrow()
     }
 
-    private func makeProfile(
-        preferences: JSON,
-        pumpSettings: JSON,
-        bgTargets: JSON,
-        basalProfile: JSON,
-        isf: JSON,
-        carbRatio: JSON,
-        tempTargets: JSON,
-        model: JSON,
-        autotune _: JSON,
-        trioSettings: JSON,
-        clock: Date
-    ) throws -> RawJSON {
-        let swiftResult = OpenAPSSwift.makeProfile(
-            preferences: preferences,
-            pumpSettings: pumpSettings,
-            bgTargets: bgTargets,
-            basalProfile: basalProfile,
-            isf: isf,
-            carbRatio: carbRatio,
-            tempTargets: tempTargets,
-            model: model,
-            trioSettings: trioSettings,
-            clock: clock
-        )
-        return try swiftResult.returnOrThrow()
-    }
-
     private func loadJSON(name: String) -> String {
         try! String(contentsOf: Foundation.Bundle.main.url(forResource: "json/\(name)", withExtension: "json")!)
     }

+ 0 - 8
Trio/Sources/APS/OpenAPSSwift/JSONBridge.swift

@@ -40,14 +40,6 @@ enum JSONBridge {
         try JSONBridge.from(string: from.rawJSON)
     }
 
-    static func model(from: JSON) -> String {
-        from.rawJSON
-    }
-
-    static func trioSettings(from: JSON) throws -> TrioSettings {
-        try JSONBridge.from(string: from.rawJSON)
-    }
-
     static func glucose(from: JSON) throws -> [BloodGlucose] {
         try JSONBridge.from(string: from.rawJSON)
     }

+ 1 - 1
Trio/Sources/APS/OpenAPSSwift/Models/Profile.swift

@@ -1,6 +1,6 @@
 import Foundation
 
-struct Profile: Codable {
+struct Profile: JSON {
     // Kotlin-defined properties from AndroidAPS OapsProfile.kt
     // with defaults pulled from profile.js
     var dia: Decimal?

+ 20 - 37
Trio/Sources/APS/OpenAPSSwift/OpenAPSSwift.swift

@@ -2,44 +2,27 @@ import Foundation
 
 struct OpenAPSSwift {
     static func makeProfile(
-        preferences: JSON,
-        pumpSettings: JSON,
-        bgTargets: JSON,
-        basalProfile: JSON,
-        isf: JSON,
-        carbRatio: JSON,
-        tempTargets: JSON,
-        model: JSON,
-        trioSettings: JSON,
+        preferences: Preferences,
+        pumpSettings: PumpSettings,
+        bgTargets: BGTargets,
+        basalProfile: [BasalProfileEntry],
+        isf: InsulinSensitivities,
+        carbRatio: CarbRatios,
+        tempTargets: [TempTarget],
+        model: String,
         clock: Date
-    ) -> (OrefFunctionResult) {
-        do {
-            let preferences = try JSONBridge.preferences(from: preferences)
-            let pumpSettings = try JSONBridge.pumpSettings(from: pumpSettings)
-            let bgTargets = try JSONBridge.bgTargets(from: bgTargets)
-            let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
-            let isf = try JSONBridge.insulinSensitivities(from: isf)
-            let carbRatio = try JSONBridge.carbRatios(from: carbRatio)
-            let tempTargets = try JSONBridge.tempTargets(from: tempTargets)
-            let model = JSONBridge.model(from: model)
-            let trioSettings = try JSONBridge.trioSettings(from: trioSettings)
-
-            let profile = try ProfileGenerator.generate(
-                pumpSettings: pumpSettings,
-                bgTargets: bgTargets,
-                basalProfile: basalProfile,
-                isf: isf,
-                preferences: preferences,
-                carbRatios: carbRatio,
-                tempTargets: tempTargets,
-                model: model,
-                clock: clock
-            )
-
-            return (try .success(JSONBridge.to(profile)))
-        } catch {
-            return (.failure(error))
-        }
+    ) throws -> Profile {
+        try ProfileGenerator.generate(
+            pumpSettings: pumpSettings,
+            bgTargets: bgTargets,
+            basalProfile: basalProfile,
+            isf: isf,
+            preferences: preferences,
+            carbRatios: carbRatio,
+            tempTargets: tempTargets,
+            model: model,
+            clock: clock
+        )
     }
 
     static func determineBasal(