Ivan Valkou 5 лет назад
Родитель
Сommit
068cf60052
3 измененных файлов с 46 добавлено и 20 удалено
  1. 10 3
      FreeAPS/Helpers/JSON.swift
  2. 12 5
      FreeAPS/OpenAPS/JavaScriptWorker.swift
  3. 24 12
      FreeAPS/OpenAPS/OpenAPS.swift

+ 10 - 3
FreeAPS/Helpers/JSON.swift

@@ -9,12 +9,12 @@ import Foundation
 
 
 protocol JSON: Codable {
-    func toString() -> String
+    var string: String { get }
     init?(from: String)
 }
 
 extension JSON {
-    func toString() -> String {
+    var string: String {
         String(data: try! JSONEncoder().encode(self), encoding: .utf8)!
     }
 
@@ -28,6 +28,13 @@ extension JSON {
 }
 
 extension String: JSON {
-    func toString() -> String { self }
+    var string: String { self }
     init?(from: String) { self = from }
 }
+
+extension Double: JSON {}
+
+extension Int: JSON {}
+
+extension Bool: JSON {}
+

+ 12 - 5
FreeAPS/OpenAPS/JavaScriptWorker.swift

@@ -16,8 +16,10 @@ final class JavaScriptWorker {
     init() {
         virtualMachine = processQueue.sync { JSVirtualMachine()! }
         context = JSContext(virtualMachine: virtualMachine)!
-        context.exceptionHandler = { context, exception in
-            print(exception!.toString()!)
+        context.exceptionHandler = { _, exception in
+            if let error = exception?.toString() {
+                print(error)
+            }
         }
     }
 
@@ -35,11 +37,16 @@ final class JavaScriptWorker {
         context.objectForKeyedSubscript(key)
     }
 
-    func stringify(_ string: String) -> JSON {
+    func json(for string: String) -> JSON {
         evaluate(string: "JSON.stringify(\(string));")!.toString()!
     }
 
-    func setValue(_ value: JSON, forEnvKey key: String) {
-        evaluate(string: "freeaps.\(key) = \(value.toString());")
+    func call(function: String, with arguments: [JSON]) -> JSON {
+        let joined = arguments.map(\.string).joined(separator: ",")
+        return json(for: "\(function)(\(joined))")
+    }
+
+    func setEnviromentValue(_ value: JSON, forKey key: String) {
+        evaluate(string: "freeaps.\(key) = \(value.string);")
     }
 }

+ 24 - 12
FreeAPS/OpenAPS/OpenAPS.swift

@@ -10,13 +10,10 @@ import JavaScriptCore
 
 final class OpenAPS {
     private let vmQueue = DispatchQueue(label: "DispatchQueue.JSVirtualMachine")
-    private let jsWorker = JavaScriptWorker()
 
-    init() {
-        loadScripts()
-    }
+    func determineBasal() {
+        let jsWorker = JavaScriptWorker()
 
-    private func loadScripts() {
         let scripts = [
             Script(name: "prepare"),
             Script(name: "basal-set-temp"),
@@ -25,19 +22,34 @@ final class OpenAPS {
         ]
 
         scripts.forEach { jsWorker.evaluate(script: $0) }
-    }
 
-    func determineBasal() {
         let glucose = loadJSON(name: "glucose")
         let currentTemp = loadJSON(name: "temp_basal")
         let iobData = loadJSON(name: "iob")
         let profile = loadJSON(name: "profile")
-        let autosensData = Autosens(ratio: 1.0).toString()
+        let autosensData = Autosens(ratio: 1.0)
         let mealData = loadJSON(name: "meal")
-
-        let glucoseStatus = jsWorker.stringify("getLastGlucose(\(glucose))")
-        jsWorker.setValue(glucoseStatus, forEnvKey: "glucoseStatus")
-        let result = jsWorker.stringify("determine_basal(freeaps.glucoseStatus, \(currentTemp), \(iobData), \(profile), \(autosensData), \(mealData), tempBasalFunctions, true, 100, 1527924300000)")
+        let tempBasalFunctions = "tempBasalFunctions"
+        let microBolusAllowed = true
+        let reservoir = 100
+        let tsMilliseconds = 1527924300000
+
+        let glucoseStatus = jsWorker.call(function: "getLastGlucose", with: [glucose])
+        let result = jsWorker.call(
+            function: "determine_basal",
+            with: [
+                glucoseStatus,
+                currentTemp,
+                iobData,
+                profile,
+                autosensData,
+                mealData,
+                tempBasalFunctions,
+                microBolusAllowed,
+                reservoir,
+                tsMilliseconds
+            ]
+        )
         print(result)
         print(jsWorker["logError"]!.toString()!)