From 68a127d804bca07607ba881e287d3b3f1ef59d77 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 8 Jan 2017 14:21:29 +0100 Subject: [PATCH] make autosens configurable in settings, determinebasaladapter refactoring --- .../androidaps/PreferencesActivity.java | 3 + .../ConfigBuilder/ConfigBuilderPlugin.java | 3 +- .../DetermineBasalAdapterAMAJS.java | 81 +++++++------------ .../OpenAPSAMA/DetermineBasalResultAMA.java | 6 -- .../info/nightscout/utils/JSONFormatter.java | 4 +- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_openapsama.xml | 14 ++++ 7 files changed, 51 insertions(+), 61 deletions(-) create mode 100644 app/src/main/res/xml/pref_openapsama.xml diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index 6f8b439268..8c19b68d85 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -17,6 +17,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.DanaR.BluetoothDevicePreference; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; import info.nightscout.utils.LocaleHelper; public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { @@ -86,6 +87,8 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre addPreferencesFromResource(R.xml.pref_closedmode); if (Config.OPENAPSENABLED) addPreferencesFromResource(R.xml.pref_openapsma); + if (MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class).isEnabled(PluginBase.APS)) + addPreferencesFromResource(R.xml.pref_openapsama); addPreferencesFromResource(R.xml.pref_nightscout); if (Config.DANAR) { DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 92e2b7729d..ffd27f771b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -697,7 +697,8 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain @Override public boolean isAMAModeEnabled() { - boolean result = true; + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + boolean result = preferences.getBoolean("openapsama_useautosens", false); ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java index 86e01d0a41..1f1a4df274 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java @@ -35,7 +35,7 @@ public class DetermineBasalAdapterAMAJS { private V8Array mIobData; private V8Object mMealData; private V8Object mCurrentTemp; - private V8Object mAutosensData; + private V8Object mAutosensData = null; private final String PARAM_currentTemp = "currentTemp"; private final String PARAM_iobData = "iobData"; @@ -61,61 +61,12 @@ public class DetermineBasalAdapterAMAJS { mV8rt = V8.createV8Runtime(); mScriptReader = scriptReader; - init(); initLogCallback(); initProcessExitCallback(); initModuleParent(); loadScript(); } - public void init() { - // Profile - mProfile = new V8Object(mV8rt); - mProfile.add("max_iob", 0); - mProfile.add("carbs_hr", 0); - mProfile.add("dia", 0); - mProfile.add("type", "current"); - mProfile.add("max_daily_basal", 0); - mProfile.add("max_basal", 0); - mProfile.add("max_bg", 0); - mProfile.add("min_bg", 0); - mProfile.add("carb_ratio", 0); - mProfile.add("sens", 0); - mProfile.add("max_daily_safety_multiplier", Constants.MAX_DAILY_SAFETY_MULTIPLIER); - mProfile.add("current_basal_safety_multiplier", Constants.CURRENT_BASAL_SAFETY_MULTIPLIER); - mProfile.add("skip_neutral_temps", true); - mProfile.add("temptargetSet", false); - mProfile.add("autosens_adjust_targets", false); - mProfile.add("min_5m_carbimpact", 0); - mProfile.add("current_basal", 0); - mV8rt.add(PARAM_profile, mProfile); - // Current temp - mCurrentTemp = new V8Object(mV8rt); - mCurrentTemp.add("temp", "absolute"); - mCurrentTemp.add("duration", 0); - mCurrentTemp.add("rate", 0); - mV8rt.add(PARAM_currentTemp, mCurrentTemp); - // IOB data -// mIobData = new V8Array(mV8rt); -// mV8rt.add(PARAM_iobData, mIobData); - // Glucose status - mGlucoseStatus = new V8Object(mV8rt); - mGlucoseStatus.add("glucose", 0); - mGlucoseStatus.add("delta", 0); - mGlucoseStatus.add("avgdelta", 0); - mV8rt.add(PARAM_glucoseStatus, mGlucoseStatus); - // Meal data - mMealData = new V8Object(mV8rt); - mMealData.add("carbs", 0); - mMealData.add("boluses", 0); - mMealData.add("mealCOB", 0.0d); - mMealData.add("ratio", 0.0d); - mV8rt.add(PARAM_meal_data, mMealData); - // Autosens data - mAutosensData = new V8Object(mV8rt); - mV8rt.add(PARAM_autosens_data, mAutosensData); - } - public DetermineBasalResultAMA invoke() { log.debug(">>> Invoking detemine_basal <<<"); @@ -124,7 +75,10 @@ public class DetermineBasalAdapterAMAJS { log.debug("Current temp: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");")); log.debug("Profile: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");")); log.debug("Meal data: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");")); - log.debug("Autosens data: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_autosens_data + ");")); + if (mAutosensData != null) + log.debug("Autosens data: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_autosens_data + ");")); + else + log.debug("Autosens data: " + "undefined"); mV8rt.executeVoidScript( "var rT = determine_basal(" + @@ -156,7 +110,10 @@ public class DetermineBasalAdapterAMAJS { storedCurrentTemp = mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");"); storedProfile = mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");"); storedMeal_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");"); - storedAutosens_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_autosens_data + ");"); + if (mAutosensData != null) + storedAutosens_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_autosens_data + ");"); + else + storedAutosens_data = "undefined"; return result; } @@ -269,6 +226,7 @@ public class DetermineBasalAdapterAMAJS { String units = profile.getUnits(); + mProfile = new V8Object(mV8rt); mProfile.add("max_iob", maxIob); mProfile.add("carbs_hr", profile.getCarbAbsorbtionRate()); mProfile.add("dia", profile.getDia()); @@ -280,27 +238,44 @@ public class DetermineBasalAdapterAMAJS { mProfile.add("target_bg", targetBg); mProfile.add("carb_ratio", profile.getIc(profile.secondsFromMidnight())); mProfile.add("sens", NSProfile.toMgdl(profile.getIsf(NSProfile.secondsFromMidnight()).doubleValue(), units)); + mProfile.add("max_daily_safety_multiplier", Constants.MAX_DAILY_SAFETY_MULTIPLIER); + mProfile.add("current_basal_safety_multiplier", Constants.CURRENT_BASAL_SAFETY_MULTIPLIER); + mProfile.add("skip_neutral_temps", true); mProfile.add("current_basal", pump.getBaseBasalRate()); mProfile.add("temptargetSet", tempTargetSet); mProfile.add("autosens_adjust_targets", MainApp.getConfigBuilder().isAMAModeEnabled()); mProfile.add("min_5m_carbimpact", min_5m_carbimpact); + mV8rt.add(PARAM_profile, mProfile); + mCurrentTemp = new V8Object(mV8rt); + mCurrentTemp.add("temp", "absolute"); mCurrentTemp.add("duration", pump.getTempBasalRemainingMinutes()); mCurrentTemp.add("rate", pump.getTempBasalAbsoluteRate()); + mV8rt.add(PARAM_currentTemp, mCurrentTemp); mIobData = mV8rt.executeArrayScript(IobTotal.convertToJSONArray(iobArray).toString()); mV8rt.add(PARAM_iobData, mIobData); + mGlucoseStatus = new V8Object(mV8rt); mGlucoseStatus.add("glucose", glucoseStatus.glucose); mGlucoseStatus.add("delta", glucoseStatus.delta); mGlucoseStatus.add("short_avgdelta", glucoseStatus.short_avgdelta); mGlucoseStatus.add("long_avgdelta", glucoseStatus.long_avgdelta); + mV8rt.add(PARAM_glucoseStatus, mGlucoseStatus); + mMealData = new V8Object(mV8rt); mMealData.add("carbs", mealData.carbs); mMealData.add("boluses", mealData.boluses); mMealData.add("mealCOB", mealData.mealCOB); + mV8rt.add(PARAM_meal_data, mMealData); - mAutosensData.add("ratio", autosensDataRatio); + if (MainApp.getConfigBuilder().isAMAModeEnabled()) { + mAutosensData = new V8Object(mV8rt); + mAutosensData.add("ratio", autosensDataRatio); + mV8rt.add(PARAM_autosens_data, mAutosensData); + } else { + mV8rt.addUndefined(PARAM_autosens_data); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalResultAMA.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalResultAMA.java index 7475c55de6..38d13aad2a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalResultAMA.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalResultAMA.java @@ -16,7 +16,6 @@ public class DetermineBasalResultAMA extends APSResult { public JSONObject json = new JSONObject(); public double eventualBG; public double snoozeBG; - public String mealAssist; public IobTotal iob; public DetermineBasalResultAMA(V8Object result, JSONObject j) { @@ -26,7 +25,6 @@ public class DetermineBasalResultAMA extends APSResult { changeRequested = false; rate = -1; duration = -1; - mealAssist = ""; } else { reason = result.getString("reason"); eventualBG = result.getDouble("eventualBG"); @@ -46,9 +44,6 @@ public class DetermineBasalResultAMA extends APSResult { duration = -1; changeRequested = false; } - if (result.contains("mealAssist")) { - mealAssist = result.getString("mealAssist"); - } else mealAssist = ""; } result.release(); } @@ -74,7 +69,6 @@ public class DetermineBasalResultAMA extends APSResult { } newResult.eventualBG = eventualBG; newResult.snoozeBG = snoozeBG; - newResult.mealAssist = new String(mealAssist); return newResult; } diff --git a/app/src/main/java/info/nightscout/utils/JSONFormatter.java b/app/src/main/java/info/nightscout/utils/JSONFormatter.java index 1d84a43bd6..a37169f061 100644 --- a/app/src/main/java/info/nightscout/utils/JSONFormatter.java +++ b/app/src/main/java/info/nightscout/utils/JSONFormatter.java @@ -17,7 +17,9 @@ public class JSONFormatter { public static Spanned format(final String jsonString) { final JsonVisitor visitor = new JsonVisitor(4, ' '); try { - if (jsonString.getBytes()[0] == '[') + if (jsonString.equals("undefined")) + return Html.fromHtml("undefined"); + else if (jsonString.getBytes()[0] == '[') return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0)); else return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0)); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ec4cb61d2d..8dc4213e35 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -403,4 +403,5 @@ Array of %d elements.\nActual value: Autosens data Script debug + Use AMA autosens feature diff --git a/app/src/main/res/xml/pref_openapsama.xml b/app/src/main/res/xml/pref_openapsama.xml new file mode 100644 index 0000000000..0dce8b7d8a --- /dev/null +++ b/app/src/main/res/xml/pref_openapsama.xml @@ -0,0 +1,14 @@ + + + + + + + + + \ No newline at end of file