public DatermineBasalResult invoke() { mV8rt.executeVoidScript( "console.error(\"determine_basal(\"+\n" + "JSON.stringify(" + PARAM_glucoseStatus + ")+ \", \" +\n" + "JSON.stringify(" + PARAM_currentTemp + ")+ \", \" + \n" + "JSON.stringify(" + PARAM_iobData + ")+ \", \" +\n" + "JSON.stringify(" + PARAM_profile + ")+ \") \");"); // determine_basal(glucose_status, currenttemp, iob_data, profile, autosens_data, meal_data, // tempBasalFunctions) try { mV8rt.executeVoidScript( "var rT = determine_basal(" + PARAM_glucoseStatus + ", " + PARAM_currentTemp + ", " + PARAM_iobData + ", " + PARAM_profile + ", " + PARAM_autosens_data + ", " + PARAM_meal_data + ", " + "tempBasalFunctions" + ");"); } catch (V8ScriptExecutionException e) { log.error(e.getMessage(), e); } String ret = ""; log.debug(mV8rt.executeStringScript("JSON.stringify(rT);")); V8Object v8ObjectReuslt = mV8rt.getObject("rT"); // { // V8Object result = v8ObjectReuslt; // log.debug(Arrays.toString(result.getKeys())); // } DatermineBasalResult result = new DatermineBasalResult(v8ObjectReuslt); return result; }
public DetermineBasalAdapterJS(ScriptReader scriptReader) throws IOException { mV8rt = V8.createV8Runtime(); mScriptReader = scriptReader; initProfile(); initGlucoseStatus(); initIobData(); initCurrentTemp(); initLogCallback(); initProcessExitCallback(); initModuleParent(); mV8rt.executeVoidScript( readFile("oref0/lib/round-basal.js"), "oref0/lib/determine-basal/round-basal.js", 0); mV8rt.executeVoidScript("var round_basal = module.exports;"); mV8rt.executeVoidScript("require = function() {return round_basal;};"); mV8rt.executeVoidScript( readFile("oref0/lib/basal-set-temp.js"), "oref0/lib/determine-basal/basal-set-temp.js ", 0); mV8rt.executeVoidScript("var tempBasalFunctions = module.exports;"); mCobData = new V8Object(mV8rt); mCobData.add("mealCOB", 0.0); mV8rt.add("meal_data", mCobData); // V8Object autosens_data = new V8Object(mV8rt); // runtime.add("autosens_data", autosens_data); mV8rt.executeVoidScript("autosens_data = undefined"); loadScript(); }
private void initLogCallback() { JavaVoidCallback callbackLog = new JavaVoidCallback() { @Override public void invoke(V8Object arg0, V8Array parameters) { if (parameters.length() > 0) { Object arg1 = parameters.get(0); log.debug("JSLOG " + arg1); } } }; mV8rt.registerJavaMethod(callbackLog, "log"); mV8rt.executeVoidScript("var console = {\"log\":log, \"error\":log};"); }
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.executeVoidScript("return \"\";"); } } }; mV8rt.registerJavaMethod(callbackProccessExit, "proccessExit"); mV8rt.executeVoidScript("var process = {\"exit\": function () { proccessExit(); } };"); }
private void initGlucoseStatus() { mGlucoseStatus = new V8Object(mV8rt); setGlucoseStatus(0.0, 0.0, 0.0); mV8rt.add(PARAM_glucoseStatus, mGlucoseStatus); }
private void initCurrentTemp() { mCurrentTemp = new V8Object(mV8rt); setCurrentTemp(30.0, 0.1); mCurrentTemp.add("temp", "absolute"); mV8rt.add(PARAM_currentTemp, mCurrentTemp); }
private void loadScript() throws IOException { mV8rt.executeVoidScript( readFile("oref0/lib/determine-basal/determine-basal.js"), "oref0/lib/determine-basal/determine-basal.js", 0); mV8rt.executeVoidScript("var determine_basal = module.exports;"); mV8rt.executeVoidScript( "var setTempBasal = function (rate, duration, profile, rT, offline) {" + "rT.duration = duration;\n" + " rT.rate = rate;" + "return rT;" + "};", "setTempBasal.js", 0); }
public void release() { try { mProfile.release(); mCurrentTemp.release(); mIobData.release(); mGlucoseStatus.release(); mCobData.release(); mV8rt.release(); } catch (Exception e) { log.error("release()", e); } }
private void initProfile() { mProfile = new V8Object(mV8rt); mProfile.add("max_iob", 2.0); mProfile.add("carbs_hr", 28.0); mProfile.add("dia", 4.0); mProfile.add("type", "current"); setProfile_CurrentBasal(1.6); mProfile.add("max_daily_basal", 1.1); setProfile_MaxBasal(2.0); mProfile.add("max_bg", 125); mProfile.add("min_bg", 106); mProfile.add("carbratio", 10); setProfile_Sens(27); mV8rt.add(PARAM_profile, mProfile); }
private void initIobData() { mIobData = new V8Object(mV8rt); setIobData(0.0, 0.0, 0.0); mV8rt.add(PARAM_iobData, mIobData); }
private void initModuleParent() { mV8rt.executeVoidScript("var module = {\"parent\":Boolean(1)};"); }