rhino logging and cleanups

This commit is contained in:
AdrianLxM 2017-10-15 16:10:58 +02:00
parent e65b4bf858
commit d32a874baa
2 changed files with 67 additions and 161 deletions

View file

@ -9,11 +9,14 @@ import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function; import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeJSON; import org.mozilla.javascript.NativeJSON;
import org.mozilla.javascript.NativeObject; import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.RhinoException;
import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.Constants;
@ -42,91 +45,10 @@ public class DetermineBasalAdapterMAJS {
private String storedProfile = null; private String storedProfile = null;
private String storedMeal_data = null; private String storedMeal_data = null;
/**
* Main code
*/
public DetermineBasalAdapterMAJS(ScriptReader scriptReader) throws IOException { public DetermineBasalAdapterMAJS(ScriptReader scriptReader) throws IOException {
mScriptReader = scriptReader; mScriptReader = scriptReader;
//initLogCallback();
//initProcessExitCallback();
/* try {
setTestData();
rhinotest();
} catch (JSONException e) {
e.printStackTrace();
}*/
} }
public void rhinotest(){
Context rhino = Context.enter();
Scriptable scope = rhino.initStandardObjects();
// Turn off optimization to make Rhino Android compatible
rhino.setOptimizationLevel(-1);
try {
//set module parent
rhino.evaluateString(scope, "var module = {\"parent\":Boolean(1)};", "JavaScript", 0, null);
//generate functions "determine_basal" and "setTempBasal"
rhino.evaluateString(scope, readFile("OpenAPSMA/determine-basal.js"), "JavaScript", 0, null);
String setTempBasalCode= "var setTempBasal = function (rate, duration, profile, rT, offline) {" +
"rT.duration = duration;\n" +
" rT.rate = rate;" +
"return rT;" +
"};";
rhino.evaluateString(scope, setTempBasalCode, "setTempBasal.js", 0, null);
// Get the functionName defined in JavaScriptCode
// Object obj = scope.get(functionNameInJavaScriptCode, scope);
Object determineBasalObj = scope.get("determine_basal", scope);
Object setTempBasalObj = scope.get("setTempBasal", scope);
if (determineBasalObj instanceof Function && setTempBasalObj instanceof Function) {
Function determineBasalJS = (Function) determineBasalObj;
Function setTempBasalJS = (Function) setTempBasalObj;
//prepare parameters
Object[] params = new Object[]{
makeParam(mGlucoseStatus, rhino, scope),
makeParam(mCurrentTemp, rhino, scope),
makeParam(mIobData, rhino, scope),
makeParam(mProfile, rhino, scope),
"undefined",
makeParam(mMealData, rhino, scope),
setTempBasalJS};
NativeObject jsResult = (NativeObject) determineBasalJS.call(rhino, scope, scope, params);
// Parse the jsResult object to a String
String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString();
log.debug("rhino result: + " + result);
} else {
log.debug("Problem loading JS Functions");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
Context.exit();
}
}
public Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) {
Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), new Callable() {
@Override
public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
return objects[1];
}
});
return param;
}
public DetermineBasalResultMA invoke() { public DetermineBasalResultMA invoke() {
DetermineBasalResultMA determineBasalResultMA = null; DetermineBasalResultMA determineBasalResultMA = null;
@ -136,6 +58,12 @@ public class DetermineBasalAdapterMAJS {
rhino.setOptimizationLevel(-1); rhino.setOptimizationLevel(-1);
try { try {
//register logger callback for console.log and console.error
ScriptableObject.defineClass(scope, LoggerCallback.class);
Scriptable myLogger = rhino.newObject(scope, "LoggerCallback", null);
scope.put("console", scope, myLogger);
//set module parent //set module parent
rhino.evaluateString(scope, "var module = {\"parent\":Boolean(1)};", "JavaScript", 0, null); rhino.evaluateString(scope, "var module = {\"parent\":Boolean(1)};", "JavaScript", 0, null);
@ -148,12 +76,7 @@ public class DetermineBasalAdapterMAJS {
"return rT;" + "return rT;" +
"};"; "};";
rhino.evaluateString(scope, setTempBasalCode, "setTempBasal.js", 0, null); rhino.evaluateString(scope, setTempBasalCode, "setTempBasal.js", 0, null);
// Get the functionName defined in JavaScriptCode
// Object obj = scope.get(functionNameInJavaScriptCode, scope);
Object determineBasalObj = scope.get("determine_basal", scope); Object determineBasalObj = scope.get("determine_basal", scope);
Object setTempBasalObj = scope.get("setTempBasal", scope); Object setTempBasalObj = scope.get("setTempBasal", scope);
if (determineBasalObj instanceof Function && setTempBasalObj instanceof Function) { if (determineBasalObj instanceof Function && setTempBasalObj instanceof Function) {
@ -186,6 +109,14 @@ public class DetermineBasalAdapterMAJS {
} }
} catch (IOException e) { } catch (IOException e) {
log.debug("IOException"); log.debug("IOException");
} catch (RhinoException e){
log.error("RhinoException: (" + e.lineNumber() + ","+ e.columnNumber() + ") " + e.toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} finally { } finally {
Context.exit(); Context.exit();
} }
@ -219,37 +150,6 @@ public class DetermineBasalAdapterMAJS {
return storedMeal_data; return storedMeal_data;
} }
/*
private void initProcessExitCallback() {
JavaVoidCallback callbackProccessExit = new JavaVoidCallback() {
@Override
public void invoke(V8Object arg0, V8Array parameters) {
if (parameters.length() > 0) {
Object arg1 = parameters.get(0);
log.error("ProccessExit " + arg1);
}
}
};
mV8rt.registerJavaMethod(callbackProccessExit, "proccessExit");
mV8rt.executeVoidScript("var process = {\"exit\": function () { proccessExit(); } };");
}
private void initLogCallback() {
JavaVoidCallback callbackLog = new JavaVoidCallback() {
@Override
public void invoke(V8Object arg0, V8Array parameters) {
if (parameters.length() > 0) {
Object arg1 = parameters.get(0);
if (Config.logAPSResult)
log.debug("Input params: " + arg1);
}
}
};
mV8rt.registerJavaMethod(callbackLog, "log");
mV8rt.executeVoidScript("var console = {\"log\":log, \"error\":log};");
}
*/
public void setData(Profile profile, public void setData(Profile profile,
double maxIob, double maxIob,
double maxBasal, double maxBasal,
@ -307,50 +207,6 @@ public class DetermineBasalAdapterMAJS {
mMealData.put("boluses", mealData.boluses); mMealData.put("boluses", mealData.boluses);
} }
public void setTestData() throws JSONException {
mProfile = new JSONObject();
mProfile.put("max_iob", 5d);
mProfile.put("dia", 3d);
mProfile.put("type", "current");
mProfile.put("max_daily_basal", 1d);
mProfile.put("max_basal", 4d);
mProfile.put("min_bg", 89d);
mProfile.put("max_bg", 91d);
mProfile.put("target_bg", 90d);
mProfile.put("carb_ratio", 5d);
mProfile.put("sens", 40d);
mProfile.put("current_basal", 0.8);
if (true) {
mProfile.put("out_units", "mmol/L");
}
mCurrentTemp = new JSONObject();
mCurrentTemp.put("duration", 15d);
mCurrentTemp.put("rate", 0.2d);
mIobData = new JSONObject();
mIobData.put("iob", -0.1d); //netIob
mIobData.put("activity", -0.003d); //netActivity
mIobData.put("bolussnooze", 0d); //bolusIob
mIobData.put("basaliob", -0.1d);
mIobData.put("netbasalinsulin", -0.11);
mIobData.put("hightempinsulin", 0.03);
mGlucoseStatus = new JSONObject();
mGlucoseStatus.put("glucose", 170);
mGlucoseStatus.put("delta", 1.93);
mGlucoseStatus.put("avgdelta", 2.1);
mMealData = new JSONObject();
mMealData.put("carbs", 0);
mMealData.put("boluses", 0);
}
public String readFile(String filename) throws IOException { public String readFile(String filename) throws IOException {
byte[] bytes = mScriptReader.readFile(filename); byte[] bytes = mScriptReader.readFile(filename);
String string = new String(bytes, "UTF-8"); String string = new String(bytes, "UTF-8");
@ -360,4 +216,14 @@ public class DetermineBasalAdapterMAJS {
return string; return string;
} }
public Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) {
Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), new Callable() {
@Override
public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
return objects[1];
}
});
return param;
}
} }

View file

@ -0,0 +1,40 @@
package info.nightscout.androidaps.plugins.OpenAPSMA;
import org.mozilla.javascript.ScriptableObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.utils.ToastUtils;
/**
* Created by adrian on 15/10/17.
*/
public class LoggerCallback extends ScriptableObject {
private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterMAJS.class);
public LoggerCallback() {
//empty constructor needed for Rhino
}
@Override
public String getClassName() {
return "LoggerCallback";
}
public void jsConstructor() {
//empty constructor on JS site; could work as setter
}
public void jsFunction_log(String s) {
log.debug(s);
}
public void jsFunction_error(String s) {
log.error(s);
}
}