Browse Source

Middleware support

Ivan Valkou 5 năm trước cách đây
mục cha
commit
54032e3b3d

+ 3 - 0
FreeAPS/Resources/javascript/middleware/determine_basal.js

@@ -0,0 +1,3 @@
+function middleware(iob, currenttemp, glucose, profile, autosens, meal, microbolusAllowed, reservoir, clock) {
+    // modify anything
+}

+ 0 - 6
FreeAPS/Resources/javascript/prepare/autosens.js

@@ -1,10 +1,4 @@
 // для settings/autosens.json параметры: monitor/glucose.json monitor/pumphistory-24h-zoned.json settings/basal_profile.json settings/profile.json monitor/carbhistory.json settings/temptargets.json
-var console = {
-    log: function(...args) { _consoleLog(args); },
-    error: function(...args) { _consoleLog(args); }
-};
-var printLog = function(...args) { console.log(args); };
-var process = { stderr: { write: printLog } };
 
 function generate(glucose_data, pumphistory_data, basalprofile, profile_data, carb_data = {}, temptarget_data = {}) {
     if (glucose_data.length < 72) {

+ 0 - 5
FreeAPS/Resources/javascript/prepare/autotune-core.js

@@ -1,8 +1,3 @@
-var console = {
-    log: function(...args) { _consoleLog(args); },
-    error: function(...args) { _consoleLog(args); }
-};
-
 function generate(prepped_glucose_data,previous_autotune_data,pumpprofile_data){
   if (!pumpprofile_data.useCustomPeakTime) {
       previous_autotune_data.dia = pumpprofile_data.dia;

+ 0 - 5
FreeAPS/Resources/javascript/prepare/autotune-prep.js

@@ -1,8 +1,3 @@
-var console = {
-    log: function(...args) { _consoleLog(args); },
-    error: function(...args) { _consoleLog(args); }
-};
-
 function generate(pumphistory_data,profile_data,glucose_data,pumpprofile_data,carb_data={ },categorize_uam_as_basal=false,tune_insulin_curve=false){
     if ( typeof(profile_data.carb_ratio) === 'undefined' || profile_data.carb_ratio < 2 ) {
         if ( typeof(pumpprofile_data.carb_ratio) === 'undefined' || pumpprofile_data.carb_ratio < 2 ) {

+ 16 - 16
FreeAPS/Resources/javascript/prepare/determine-basal.js

@@ -1,29 +1,29 @@
 //для enact/smb-suggested.json параметры: monitor/iob.json monitor/temp_basal.json monitor/glucose.json settings/profile.json settings/autosens.json --meal monitor/meal.json --microbolus --reservoir monitor/reservoir.json
-var console = {
-    log: function(...args) { _consoleLog(args); },
-    error: function(...args) { _consoleLog(args); }
-};
-var printLog = function(...args) { console.log(args); };
-var process = { stderr: { write: printLog } };
 
+function generate(iob, currenttemp, glucose, profile, autosens = null, meal = null, microbolusAllowed = false, reservoir = null, clock = new Date()) {
 
-function generate(iob_data, currenttemp, glucose_data, profile, autosens_input = false, meal_input = false, microbolus = false, reservoir_input = false, clock = new Date()){
-    var glucose_status = freeaps_glucoseGetLast(glucose_data);
+    try {
+        middleware(iob, currenttemp, glucose, profile, autosens, meal, microbolusAllowed, reservoir, clock);
+    } catch (error) {
+        console.log("Invalid middleware: " + error)
+    }
+
+    var glucose_status = freeaps_glucoseGetLast(glucose);
     var autosens_data = null;
 
-    if (autosens_input) {
-        autosens_data = autosens_input;
+    if (autosens) {
+        autosens_data = autosens;
     }
 
     var reservoir_data = null;
-    if (reservoir_input) {
-        reservoir_data = reservoir_input;
+    if (reservoir) {
+        reservoir_data = reservoir;
     }
 
-    var meal_data = { };
-    if (meal_input) {
-        meal_data = meal_input;
+    var meal_data = {};
+    if (meal) {
+        meal_data = meal;
     }
 
-    return freeaps_determineBasal(glucose_status, currenttemp, iob_data, profile, autosens_data, meal_data, freeaps_basalSetTemp, microbolus, reservoir_data, clock);
+    return freeaps_determineBasal(glucose_status, currenttemp, iob, profile, autosens_data, meal_data, freeaps_basalSetTemp, microbolusAllowed, reservoir_data, clock);
 }

+ 0 - 4
FreeAPS/Resources/javascript/prepare/iob.js

@@ -1,8 +1,4 @@
 //для monitor/iob.json параметры: monitor/pumphistory-24h-zoned.json settings/profile.json monitor/clock-zoned.json settings/autosens.json
-var console = {
-    log: function(...args) { _consoleLog(args); },
-    error: function(...args) { _consoleLog(args); }
-};
 
 function generate(pumphistory_data, profile_data, clock_data, autosens_data = null){
     var inputs = {

+ 6 - 0
FreeAPS/Resources/javascript/prepare/log.js

@@ -0,0 +1,6 @@
+var console = {
+    log: function(...args) { _consoleLog(args); },
+    error: function(...args) { _consoleLog(args); }
+};
+var printLog = function(...args) { console.log(args); };
+var process = { stderr: { write: printLog } };

+ 0 - 4
FreeAPS/Resources/javascript/prepare/meal.js

@@ -1,8 +1,4 @@
 //для monitor/meal.json параметры: monitor/pumphistory-24h-zoned.json settings/profile.json monitor/clock-zoned.json monitor/glucose.json settings/basal_profile.json monitor/carbhistory.json
-var console = {
-    log: function(...args) { _consoleLog(args); },
-    error: function(...args) { _consoleLog(args); }
-};
 
 function generate(pumphistory_data, profile_data, clock_data, glucose_data, basalprofile_data, carbhistory = false){
     if ( typeof(profile_data.carb_ratio) === 'undefined' || profile_data.carb_ratio < 3 ) {

+ 0 - 4
FreeAPS/Resources/javascript/prepare/profile.js

@@ -1,9 +1,5 @@
 //для pumpprofile.json параметры: settings/settings.json settings/bg_targets.json settings/insulin_sensitivities.json settings/basal_profile.json preferences.json settings/carb_ratios.json settings/temptargets.json settings/model.json
 //для profile.json параметры: settings/settings.json settings/bg_targets.json settings/insulin_sensitivities.json settings/basal_profile.json preferences.json settings/carb_ratios.json settings/temptargets.json settings/model.json settings/autotune.json
-var console = {
-    log: function(...args) { _consoleLog(args); },
-    error: function(...args) { _consoleLog(args); }
-};
 
 function generate(pumpsettings_data, bgtargets_data, isf_data, basalprofile_data, preferences_input = false, carbratio_input = false, temptargets_input = false, model_input = false, autotune_input = false){
     if (bgtargets_data.units !== 'mg/dL') {

+ 21 - 16
FreeAPS/Sources/APS/OpenAPS/Constants.swift

@@ -1,24 +1,29 @@
 extension OpenAPS {
     enum Bundle {
-        static let iob = "bundle/iob"
-        static let meal = "bundle/meal"
-        static let autotunePrep = "bundle/autotune-prep"
-        static let autotuneCore = "bundle/autotune-core"
-        static let getLastGlucose = "bundle/glucose-get-last"
-        static let basalSetTemp = "bundle/basal-set-temp"
-        static let determineBasal = "bundle/determine-basal"
-        static let autosens = "bundle/autosens"
-        static let profile = "bundle/profile"
+        static let iob = "bundle/iob.js"
+        static let meal = "bundle/meal.js"
+        static let autotunePrep = "bundle/autotune-prep.js"
+        static let autotuneCore = "bundle/autotune-core.js"
+        static let getLastGlucose = "bundle/glucose-get-last.js"
+        static let basalSetTemp = "bundle/basal-set-temp.js"
+        static let determineBasal = "bundle/determine-basal.js"
+        static let autosens = "bundle/autosens.js"
+        static let profile = "bundle/profile.js"
     }
 
     enum Prepare {
-        static let iob = "prepare/iob"
-        static let meal = "prepare/meal"
-        static let autotunePrep = "prepare/autotune-prep"
-        static let autotuneCore = "prepare/autotune-core"
-        static let determineBasal = "prepare/determine-basal"
-        static let autosens = "prepare/autosens"
-        static let profile = "prepare/profile"
+        static let iob = "prepare/iob.js"
+        static let meal = "prepare/meal.js"
+        static let autotunePrep = "prepare/autotune-prep.js"
+        static let autotuneCore = "prepare/autotune-core.js"
+        static let determineBasal = "prepare/determine-basal.js"
+        static let autosens = "prepare/autosens.js"
+        static let profile = "prepare/profile.js"
+        static let log = "prepare/log.js"
+    }
+
+    enum Middleware {
+        static let determineBasal = "middleware/determine_basal.js"
     }
 
     enum Settings {

+ 28 - 2
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -206,6 +206,7 @@ final class OpenAPS {
     private func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON) -> RawJSON {
         dispatchPrecondition(condition: .onQueue(processQueue))
         return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: Prepare.log))
             worker.evaluate(script: Script(name: Bundle.iob))
             worker.evaluate(script: Script(name: Prepare.iob))
             return worker.call(function: Function.generate, with: [
@@ -220,6 +221,7 @@ final class OpenAPS {
     private func meal(pumphistory: JSON, profile: JSON, basalProfile: JSON, clock: JSON, carbs: JSON, glucose: JSON) -> RawJSON {
         dispatchPrecondition(condition: .onQueue(processQueue))
         return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: Prepare.log))
             worker.evaluate(script: Script(name: Bundle.meal))
             worker.evaluate(script: Script(name: Prepare.meal))
             return worker.call(function: Function.generate, with: [
@@ -243,6 +245,7 @@ final class OpenAPS {
     ) -> RawJSON {
         dispatchPrecondition(condition: .onQueue(processQueue))
         return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: Prepare.log))
             worker.evaluate(script: Script(name: Bundle.autotunePrep))
             worker.evaluate(script: Script(name: Prepare.autotunePrep))
             return worker.call(function: Function.generate, with: [
@@ -263,6 +266,7 @@ final class OpenAPS {
     ) -> RawJSON {
         dispatchPrecondition(condition: .onQueue(processQueue))
         return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: Prepare.log))
             worker.evaluate(script: Script(name: Bundle.autotuneCore))
             worker.evaluate(script: Script(name: Prepare.autotuneCore))
             return worker.call(function: Function.generate, with: [
@@ -285,10 +289,16 @@ final class OpenAPS {
     ) -> RawJSON {
         dispatchPrecondition(condition: .onQueue(processQueue))
         return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: Prepare.log))
+            worker.evaluate(script: Script(name: Prepare.determineBasal))
             worker.evaluate(script: Script(name: Bundle.basalSetTemp))
             worker.evaluate(script: Script(name: Bundle.getLastGlucose))
             worker.evaluate(script: Script(name: Bundle.determineBasal))
-            worker.evaluate(script: Script(name: Prepare.determineBasal))
+
+            if let middleware = self.middlewareScript(name: OpenAPS.Middleware.determineBasal) {
+                worker.evaluate(script: middleware)
+            }
+
             return worker.call(
                 function: Function.generate,
                 with: [
@@ -315,6 +325,7 @@ final class OpenAPS {
     ) -> RawJSON {
         dispatchPrecondition(condition: .onQueue(processQueue))
         return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: Prepare.log))
             worker.evaluate(script: Script(name: Bundle.autosens))
             worker.evaluate(script: Script(name: Prepare.autosens))
             return worker.call(
@@ -334,6 +345,7 @@ final class OpenAPS {
     private func exportDefaultPreferences() -> RawJSON {
         dispatchPrecondition(condition: .onQueue(processQueue))
         return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: Prepare.log))
             worker.evaluate(script: Script(name: Bundle.profile))
             worker.evaluate(script: Script(name: Prepare.profile))
             return worker.call(function: Function.exportDefaults, with: [])
@@ -353,6 +365,7 @@ final class OpenAPS {
     ) -> RawJSON {
         dispatchPrecondition(condition: .onQueue(processQueue))
         return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: Prepare.log))
             worker.evaluate(script: Script(name: Bundle.profile))
             worker.evaluate(script: Script(name: Prepare.profile))
             return worker.call(
@@ -380,8 +393,21 @@ final class OpenAPS {
         storage.retrieveRaw(name) ?? OpenAPS.defaults(for: name)
     }
 
+    private func middlewareScript(name: String) -> Script? {
+        if let body = storage.retrieveRaw(name) {
+            return Script(name: "Middleware", body: body)
+        }
+
+        if let url = Foundation.Bundle.main.url(forResource: "js/\(name)", withExtension: "") {
+            return Script(name: "Middleware", body: try! String(contentsOf: url))
+        }
+
+        return nil
+    }
+
     static func defaults(for file: String) -> RawJSON {
-        guard let url = Foundation.Bundle.main.url(forResource: "json/defaults/\(file)", withExtension: "") else {
+        let prefix = file.hasSuffix(".json") ? "json/defaults" : "javascript"
+        guard let url = Foundation.Bundle.main.url(forResource: "\(prefix)/\(file)", withExtension: "") else {
             return ""
         }
         return (try? String(contentsOf: url)) ?? ""

+ 6 - 1
FreeAPS/Sources/APS/OpenAPS/Script.swift

@@ -6,6 +6,11 @@ struct Script {
 
     init(name: String) {
         self.name = name
-        body = try! String(contentsOf: Bundle.main.url(forResource: "javascript/\(name)", withExtension: "js")!)
+        body = try! String(contentsOf: Bundle.main.url(forResource: "javascript/\(name)", withExtension: "")!)
+    }
+
+    init(name: String, body: String) {
+        self.name = name
+        self.body = body
     }
 }

+ 5 - 0
FreeAPS/Sources/Modules/ConfigEditor/ConfigEditorProvider.swift

@@ -11,6 +11,11 @@ extension ConfigEditor {
         }
 
         func save(_ value: RawJSON, as file: String) {
+            if file.hasSuffix(".js") {
+                storage.save(value, as: file)
+                return
+            }
+
             guard let data = value.data(using: .utf8), (try? JSONSerialization.jsonObject(with: data, options: [])) != nil else {
                 warning(.service, "Invalid JSON")
                 return

+ 2 - 0
FreeAPS/Sources/Modules/Settings/View/SettingsRootView.swift

@@ -80,6 +80,8 @@ extension Settings {
                         Group {
                             Text("Target presets").chevronCell()
                                 .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.tempTargetsPresets), from: self)
+                            Text("Middleware").chevronCell()
+                                .navigationLink(to: .configEditor(file: OpenAPS.Middleware.determineBasal), from: self)
                         }
                     }
                 }