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

Merge pull request #1287 from nightscout/remove-profile-json

[Part 2 of 6] Remove JSON marshalling for makeProfile
Sam King 5 часов назад
Родитель
Сommit
a2aec04fdf

+ 22 - 46
Trio/Sources/APS/OpenAPS/OpenAPS.swift

@@ -621,21 +621,19 @@ final class OpenAPS {
         async let getPumpSettings = loadFileFromStorageAsync(name: Settings.settings)
         async let getBGTargets = loadFileFromStorageAsync(name: Settings.bgTargets)
         async let getBasalProfile = loadFileFromStorageAsync(name: Settings.basalProfile)
-        async let getISF = loadFileFromStorageAsync(name: Settings.insulinSensitivities)
-        async let getCR = loadFileFromStorageAsync(name: Settings.carbRatios)
+        async let getInsulinSensitivities = loadFileFromStorageAsync(name: Settings.insulinSensitivities)
+        async let getCarbRatios = 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, insulinSensitivities, carbRatios, tempTargets, model) = await (
             getPumpSettings,
             getBGTargets,
             getBasalProfile,
-            getISF,
-            getCR,
+            getInsulinSensitivities,
+            getCarbRatios,
             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 insulinSensitivities = try JSONBridge.insulinSensitivities(from: insulinSensitivities)
+            let carbRatios = try JSONBridge.carbRatios(from: carbRatios)
+            let tempTargets = try JSONBridge.tempTargets(from: tempTargets)
+
+            let pumpProfile = try OpenAPSSwift.makeProfile(
                 preferences: adjustedPreferences,
                 pumpSettings: pumpSettings,
                 bgTargets: bgTargets,
                 basalProfile: basalProfile,
-                isf: isf,
-                carbRatio: cr,
+                insulinSensitivities: insulinSensitivities,
+                carbRatios: carbRatios,
                 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,
+                insulinSensitivities: insulinSensitivities,
+                carbRatios: carbRatios,
                 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],
+        insulinSensitivities: InsulinSensitivities,
+        carbRatios: 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: insulinSensitivities,
+            preferences: preferences,
+            carbRatios: carbRatios,
+            tempTargets: tempTargets,
+            model: model,
+            clock: clock
+        )
     }
 
     static func determineBasal(