Sam King 11 месяцев назад
Родитель
Сommit
b1833847d1

+ 11 - 0
Trio/Sources/APS/OpenAPS/OpenAPS.swift

@@ -661,6 +661,17 @@ final class OpenAPS {
         carbs: JSON,
         glucose: JSON
     ) async throws -> RawJSON {
+        
+    }
+    
+    private func mealJavascript(
+        pumphistory: JSON,
+        profile: JSON,
+        basalProfile: JSON,
+        clock: JSON,
+        carbs: JSON,
+        glucose: JSON
+    ) async throws -> RawJSON {
         try await withCheckedThrowingContinuation { continuation in
             jsWorker.inCommonContext { worker in
                 worker.evaluateBatch(scripts: [

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

@@ -44,6 +44,10 @@ enum JSONBridge {
         try JSONBridge.from(string: from.rawJSON)
     }
 
+    static func carbs(from: JSON) throws -> [CarbsEntry] {
+        try JSONBridge.from(string: from.rawJSON)
+    }
+    
     static func pumpHistory(from: JSON) throws -> [PumpHistoryEvent] {
         do {
             return try JSONBridge.from(string: from.rawJSON)

+ 10 - 0
Trio/Sources/APS/OpenAPSSwift/Logging/AlgorithmComparison.swift

@@ -71,6 +71,16 @@ struct IobInputs: Codable {
     let autosens: Autosens?
 }
 
+/// For tracking inputs to `meal` when there is a mismatch
+struct MealInputs: Codable {
+    let pumpHistory: [PumpHistoryEvent]
+    let profile: Profile
+    let basalProfile: [BasalProfileEntry]
+    let clock: Date
+    let carbs: [CarbsEntry]
+    let glucose: [BloodGlucose]
+}
+
 /// Represents a complete comparison between JS and Swift implementations
 struct AlgorithmComparison: Codable {
     let id: UUID

+ 33 - 0
Trio/Sources/APS/OpenAPSSwift/OpenAPSSwift.swift

@@ -41,6 +41,39 @@ struct OpenAPSSwift {
         }
     }
 
+    static func meal(
+        pumphistory: JSON,
+        profile: JSON,
+        basalProfile: JSON,
+        clock: JSON,
+        carbs: JSON,
+        glucose: JSON
+    ) async throws -> (OrefFunctionResult, MealInputs?) {
+        var mealInputs: MealInputs?
+
+        do {
+            let pumpHistory = try JSONBridge.pumpHistory(from: pumphistory)
+            let profile = try JSONBridge.profile(from: profile)
+            let basalprofile = try JSONBridge.basalProfile(from: basalProfile)
+            let clock = try JSONBridge.clock(from: clock)
+            let carbs = try JSONBridge.carbs(from: carbs)
+            let glucose = try JSONBridge.gluc
+            
+            iobInputs = IobInputs(history: pumpHistory, profile: profile, clock: clock, autosens: autosens)
+
+            let iobResult = try IobGenerator.generate(
+                history: pumpHistory,
+                profile: profile,
+                clock: clock,
+                autosens: autosens
+            )
+
+            return try (.success(JSONBridge.to(iobResult)), iobInputs)
+        } catch {
+            return (.failure(error), iobInputs)
+        }
+    }
+    
     static func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON) -> (OrefFunctionResult, IobInputs?) {
         var iobInputs: IobInputs?