From 81b611ad92cd978ffdb0b704e2a1776f4f9531e7 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sun, 15 Oct 2017 03:21:32 +0200 Subject: [PATCH] rhino function passing --- .../main/assets/OpenAPSMA/determine-basal.js | 2 +- .../OpenAPSMA/DetermineBasalAdapterMAJS.java | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/assets/OpenAPSMA/determine-basal.js b/app/src/main/assets/OpenAPSMA/determine-basal.js index 266547c04d..acae24aa23 100644 --- a/app/src/main/assets/OpenAPSMA/determine-basal.js +++ b/app/src/main/assets/OpenAPSMA/determine-basal.js @@ -17,7 +17,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_ }; rT.error ='Error: could not get current basal rate'; - return rT; + return setTempBasal(0, 30, profile, rT, offline); if (typeof profile === 'undefined' || typeof profile.current_basal === 'undefined') { rT.error ='Error: could not get current basal rate'; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java index b79a3065ec..2d3dcef956 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java @@ -79,24 +79,36 @@ public class DetermineBasalAdapterMAJS { // been compressed to only one line using something like YUI //rhino.evaluateString(scope, javaScriptCode, "JavaScript", 1, null); rhino.evaluateString(scope, readFile("OpenAPSMA/determine-basal.js"), "JavaScript", 0, null); + String setTempBasal= "var setTempBasal = function (rate, duration, profile, rT, offline) {" + + "rT.duration = duration;\n" + + " rT.rate = rate;" + + "return rT;" + + "};"; + + rhino.evaluateString(scope, setTempBasal, "setTempBasal.js", 0, null); + // Get the functionName defined in JavaScriptCode //Object obj = scope.get(functionNameInJavaScriptCode, scope); Object obj = scope.get("determine_basal", scope); - if (obj instanceof Function) { + Object obj2 = scope.get("setTempBasal", scope); + + if (obj instanceof Function && obj2 instanceof Function) { Function jsFunction = (Function) obj; + Function jsFunctionSetTempBasal = (Function) obj2; + // Call the function with params - String[] params = { + Object[] params = { "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", - "setTempBasal"}; + jsFunctionSetTempBasal}; NativeObject jsResult = (NativeObject) jsFunction.call(rhino, scope, scope, params); @@ -127,6 +139,7 @@ public class DetermineBasalAdapterMAJS { mProfile.add("carb_ratio", 0); mProfile.add("sens", 0); mProfile.add("current_basal", 0); + String profileString = mProfile.toString(); mV8rt.add(PARAM_profile, mProfile); // Current temp mCurrentTemp = new V8Object(mV8rt);