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;
  }
  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 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 initModuleParent() {
   mV8rt.executeVoidScript("var module = {\"parent\":Boolean(1)};");
 }