rhino MA looping

This commit is contained in:
AdrianLxM 2017-10-15 14:06:13 +02:00
parent 190305c6df
commit e65b4bf858
2 changed files with 80 additions and 81 deletions

View file

@ -1,7 +1,5 @@
package info.nightscout.androidaps.plugins.OpenAPSMA; package info.nightscout.androidaps.plugins.OpenAPSMA;
import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Object;
import com.j256.ormlite.logger.Log; import com.j256.ormlite.logger.Log;
import org.json.JSONException; import org.json.JSONException;
@ -31,21 +29,13 @@ import info.nightscout.utils.SP;
public class DetermineBasalAdapterMAJS { public class DetermineBasalAdapterMAJS {
private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterMAJS.class); private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterMAJS.class);
private ScriptReader mScriptReader = null; private ScriptReader mScriptReader = null;
V8 mV8rt;
private JSONObject mProfile; private JSONObject mProfile;
private JSONObject mGlucoseStatus; private JSONObject mGlucoseStatus;
private JSONObject mIobData; private JSONObject mIobData;
private JSONObject mMealData; private JSONObject mMealData;
private JSONObject mCurrentTemp; private JSONObject mCurrentTemp;
private final String PARAM_currentTemp = "currentTemp";
private final String PARAM_iobData = "iobData";
private final String PARAM_glucoseStatus = "glucose_status";
private final String PARAM_profile = "profile";
private final String PARAM_meal_data = "meal_data";
private String storedCurrentTemp = null; private String storedCurrentTemp = null;
public String storedIobData = null; public String storedIobData = null;
private String storedGlucoseStatus = null; private String storedGlucoseStatus = null;
@ -57,17 +47,16 @@ public class DetermineBasalAdapterMAJS {
*/ */
public DetermineBasalAdapterMAJS(ScriptReader scriptReader) throws IOException { public DetermineBasalAdapterMAJS(ScriptReader scriptReader) throws IOException {
mV8rt = V8.createV8Runtime();
mScriptReader = scriptReader; mScriptReader = scriptReader;
//initLogCallback(); //initLogCallback();
//initProcessExitCallback(); //initProcessExitCallback();
try { /* try {
setTestData(); setTestData();
rhinotest(); rhinotest();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }*/
} }
@ -103,25 +92,6 @@ public class DetermineBasalAdapterMAJS {
Function setTempBasalJS = (Function) setTempBasalObj; Function setTempBasalJS = (Function) setTempBasalObj;
//prepare parameters //prepare parameters
Object param1 = NativeJSON.parse(rhino, scope, "{\"athing\": {\"anotherthing\": 2.3}}", new Callable() {
@Override
public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
return objects[1];
}
});
/*
"var rT = determine_basal(" +
PARAM_glucoseStatus + ", " +
PARAM_currentTemp + ", " +
PARAM_iobData + ", " +
PARAM_profile + ", " +
"undefined, " +
PARAM_meal_data + ", " +
"setTempBasal" +
");");
*/
Object[] params = new Object[]{ Object[] params = new Object[]{
makeParam(mGlucoseStatus, rhino, scope), makeParam(mGlucoseStatus, rhino, scope),
makeParam(mCurrentTemp, rhino, scope), makeParam(mCurrentTemp, rhino, scope),
@ -134,10 +104,10 @@ public class DetermineBasalAdapterMAJS {
NativeObject jsResult = (NativeObject) determineBasalJS.call(rhino, scope, scope, params); NativeObject jsResult = (NativeObject) determineBasalJS.call(rhino, scope, scope, params);
// Parse the jsResult object to a String // Parse the jsResult object to a String
String result = rhino.toString(jsResult); //TODO: remove, use NativeObject/HashMap String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString();
log.debug("rhino result: + " + result); log.debug("rhino result: + " + result);
} else { } else {
log.debug("hod ned kloppt"); log.debug("Problem loading JS Functions");
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -158,46 +128,75 @@ public class DetermineBasalAdapterMAJS {
public DetermineBasalResultMA invoke() { public DetermineBasalResultMA invoke() {
mV8rt.executeVoidScript( DetermineBasalResultMA determineBasalResultMA = null;
"console.error(\"determine_basal(\"+\n" +
"JSON.stringify(" + PARAM_glucoseStatus + ")+ \", \" +\n" +
"JSON.stringify(" + PARAM_currentTemp + ")+ \", \" +\n" +
"JSON.stringify(" + PARAM_iobData + ")+ \", \" +\n" +
"JSON.stringify(" + PARAM_profile + ")+ \", \" +\n" +
"JSON.stringify(" + PARAM_meal_data + ")+ \") \");"
);
mV8rt.executeVoidScript(
"var rT = determine_basal(" +
PARAM_glucoseStatus + ", " +
PARAM_currentTemp + ", " +
PARAM_iobData + ", " +
PARAM_profile + ", " +
"undefined, " +
PARAM_meal_data + ", " +
"setTempBasal" +
");");
Context rhino = Context.enter();
Scriptable scope = rhino.initStandardObjects();
// Turn off optimization to make Rhino Android compatible
rhino.setOptimizationLevel(-1);
String ret = mV8rt.executeStringScript("JSON.stringify(rT);");
if (Config.logAPSResult)
log.debug("Result: " + ret);
V8Object v8ObjectReuslt = mV8rt.getObject("rT");
DetermineBasalResultMA result = null;
try { try {
result = new DetermineBasalResultMA(v8ObjectReuslt, new JSONObject(ret)); //set module parent
} catch (JSONException e) { rhino.evaluateString(scope, "var module = {\"parent\":Boolean(1)};", "JavaScript", 0, null);
log.error("Unhandled exception", e);
//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();
if (Config.logAPSResult)
log.debug("Result: " + result);
try {
determineBasalResultMA = new DetermineBasalResultMA(jsResult, new JSONObject(result));
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
} else {
log.debug("Problem loading JS Functions");
}
} catch (IOException e) {
log.debug("IOException");
} finally {
Context.exit();
} }
storedGlucoseStatus = mV8rt.executeStringScript("JSON.stringify(" + PARAM_glucoseStatus + ");"); storedGlucoseStatus = mGlucoseStatus.toString();
storedIobData = mV8rt.executeStringScript("JSON.stringify(" + PARAM_iobData + ");"); storedIobData = mIobData.toString();
storedCurrentTemp = mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");"); storedCurrentTemp = mCurrentTemp.toString();
storedProfile = mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");"); storedProfile = mProfile.toString();
storedMeal_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");"); storedMeal_data = mMealData.toString();
return result; return determineBasalResultMA;
} }
String getGlucoseStatusParam() { String getGlucoseStatusParam() {

View file

@ -7,6 +7,7 @@ import com.eclipsesource.v8.V8Object;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.mozilla.javascript.NativeObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -22,38 +23,37 @@ public class DetermineBasalResultMA extends APSResult {
public String mealAssist; public String mealAssist;
public IobTotal iob; public IobTotal iob;
public DetermineBasalResultMA(V8Object result, JSONObject j) { public DetermineBasalResultMA(NativeObject result, JSONObject j) {
json = j; json = j;
if (result.contains("error")) { if (result.containsKey("error")) {
reason = result.getString("error"); reason = (String) result.get("error");
changeRequested = false; changeRequested = false;
rate = -1; rate = -1;
duration = -1; duration = -1;
mealAssist = ""; mealAssist = "";
} else { } else {
reason = result.getString("reason"); reason = result.get("reason").toString();
eventualBG = result.getDouble("eventualBG"); eventualBG = (Double) result.get("eventualBG");
snoozeBG = result.getDouble("snoozeBG"); snoozeBG = (Double) result.get("snoozeBG");
if (result.contains("rate")) { if (result.containsKey("rate")) {
rate = result.getDouble("rate"); rate = (Double) result.get("rate");
if (rate < 0d) rate = 0d; if (rate < 0d) rate = 0d;
changeRequested = true; changeRequested = true;
} else { } else {
rate = -1; rate = -1;
changeRequested = false; changeRequested = false;
} }
if (result.contains("duration")) { if (result.containsKey("duration")) {
duration = result.getInteger("duration"); duration = ((Double) result.get("duration")).intValue();
//changeRequested as above //changeRequested as above
} else { } else {
duration = -1; duration = -1;
changeRequested = false; changeRequested = false;
} }
if (result.contains("mealAssist")) { if (result.containsKey("mealAssist")) {
mealAssist = result.getString("mealAssist"); mealAssist = result.get("mealAssist").toString();
} else mealAssist = ""; } else mealAssist = "";
} }
result.release();
} }
public DetermineBasalResultMA() { public DetermineBasalResultMA() {