make autosens configurable in settings, determinebasaladapter refactoring

This commit is contained in:
Milos Kozak 2017-01-08 14:21:29 +01:00
parent e71b478941
commit 68a127d804
7 changed files with 51 additions and 61 deletions

View file

@ -17,6 +17,7 @@ import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.DanaR.BluetoothDevicePreference; import info.nightscout.androidaps.plugins.DanaR.BluetoothDevicePreference;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin;
import info.nightscout.utils.LocaleHelper; import info.nightscout.utils.LocaleHelper;
public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -86,6 +87,8 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
addPreferencesFromResource(R.xml.pref_closedmode); addPreferencesFromResource(R.xml.pref_closedmode);
if (Config.OPENAPSENABLED) if (Config.OPENAPSENABLED)
addPreferencesFromResource(R.xml.pref_openapsma); addPreferencesFromResource(R.xml.pref_openapsma);
if (MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class).isEnabled(PluginBase.APS))
addPreferencesFromResource(R.xml.pref_openapsama);
addPreferencesFromResource(R.xml.pref_nightscout); addPreferencesFromResource(R.xml.pref_nightscout);
if (Config.DANAR) { if (Config.DANAR) {
DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);

View file

@ -697,7 +697,8 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
@Override @Override
public boolean isAMAModeEnabled() { public boolean isAMAModeEnabled() {
boolean result = true; SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
boolean result = preferences.getBoolean("openapsama_useautosens", false);
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) { for (PluginBase p : constraintsPlugins) {

View file

@ -35,7 +35,7 @@ public class DetermineBasalAdapterAMAJS {
private V8Array mIobData; private V8Array mIobData;
private V8Object mMealData; private V8Object mMealData;
private V8Object mCurrentTemp; private V8Object mCurrentTemp;
private V8Object mAutosensData; private V8Object mAutosensData = null;
private final String PARAM_currentTemp = "currentTemp"; private final String PARAM_currentTemp = "currentTemp";
private final String PARAM_iobData = "iobData"; private final String PARAM_iobData = "iobData";
@ -61,61 +61,12 @@ public class DetermineBasalAdapterAMAJS {
mV8rt = V8.createV8Runtime(); mV8rt = V8.createV8Runtime();
mScriptReader = scriptReader; mScriptReader = scriptReader;
init();
initLogCallback(); initLogCallback();
initProcessExitCallback(); initProcessExitCallback();
initModuleParent(); initModuleParent();
loadScript(); 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() { public DetermineBasalResultAMA invoke() {
log.debug(">>> Invoking detemine_basal <<<"); log.debug(">>> Invoking detemine_basal <<<");
@ -124,7 +75,10 @@ public class DetermineBasalAdapterAMAJS {
log.debug("Current temp: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");")); log.debug("Current temp: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");"));
log.debug("Profile: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");")); log.debug("Profile: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");"));
log.debug("Meal data: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");")); log.debug("Meal data: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");"));
if (mAutosensData != null)
log.debug("Autosens data: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_autosens_data + ");")); log.debug("Autosens data: " + mV8rt.executeStringScript("JSON.stringify(" + PARAM_autosens_data + ");"));
else
log.debug("Autosens data: " + "undefined");
mV8rt.executeVoidScript( mV8rt.executeVoidScript(
"var rT = determine_basal(" + "var rT = determine_basal(" +
@ -156,7 +110,10 @@ public class DetermineBasalAdapterAMAJS {
storedCurrentTemp = mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");"); storedCurrentTemp = mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");");
storedProfile = mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");"); storedProfile = mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");");
storedMeal_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");"); storedMeal_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");");
if (mAutosensData != null)
storedAutosens_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_autosens_data + ");"); storedAutosens_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_autosens_data + ");");
else
storedAutosens_data = "undefined";
return result; return result;
} }
@ -269,6 +226,7 @@ public class DetermineBasalAdapterAMAJS {
String units = profile.getUnits(); String units = profile.getUnits();
mProfile = new V8Object(mV8rt);
mProfile.add("max_iob", maxIob); mProfile.add("max_iob", maxIob);
mProfile.add("carbs_hr", profile.getCarbAbsorbtionRate()); mProfile.add("carbs_hr", profile.getCarbAbsorbtionRate());
mProfile.add("dia", profile.getDia()); mProfile.add("dia", profile.getDia());
@ -280,27 +238,44 @@ public class DetermineBasalAdapterAMAJS {
mProfile.add("target_bg", targetBg); mProfile.add("target_bg", targetBg);
mProfile.add("carb_ratio", profile.getIc(profile.secondsFromMidnight())); mProfile.add("carb_ratio", profile.getIc(profile.secondsFromMidnight()));
mProfile.add("sens", NSProfile.toMgdl(profile.getIsf(NSProfile.secondsFromMidnight()).doubleValue(), units)); 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("current_basal", pump.getBaseBasalRate());
mProfile.add("temptargetSet", tempTargetSet); mProfile.add("temptargetSet", tempTargetSet);
mProfile.add("autosens_adjust_targets", MainApp.getConfigBuilder().isAMAModeEnabled()); mProfile.add("autosens_adjust_targets", MainApp.getConfigBuilder().isAMAModeEnabled());
mProfile.add("min_5m_carbimpact", min_5m_carbimpact); 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("duration", pump.getTempBasalRemainingMinutes());
mCurrentTemp.add("rate", pump.getTempBasalAbsoluteRate()); mCurrentTemp.add("rate", pump.getTempBasalAbsoluteRate());
mV8rt.add(PARAM_currentTemp, mCurrentTemp);
mIobData = mV8rt.executeArrayScript(IobTotal.convertToJSONArray(iobArray).toString()); mIobData = mV8rt.executeArrayScript(IobTotal.convertToJSONArray(iobArray).toString());
mV8rt.add(PARAM_iobData, mIobData); mV8rt.add(PARAM_iobData, mIobData);
mGlucoseStatus = new V8Object(mV8rt);
mGlucoseStatus.add("glucose", glucoseStatus.glucose); mGlucoseStatus.add("glucose", glucoseStatus.glucose);
mGlucoseStatus.add("delta", glucoseStatus.delta); mGlucoseStatus.add("delta", glucoseStatus.delta);
mGlucoseStatus.add("short_avgdelta", glucoseStatus.short_avgdelta); mGlucoseStatus.add("short_avgdelta", glucoseStatus.short_avgdelta);
mGlucoseStatus.add("long_avgdelta", glucoseStatus.long_avgdelta); mGlucoseStatus.add("long_avgdelta", glucoseStatus.long_avgdelta);
mV8rt.add(PARAM_glucoseStatus, mGlucoseStatus);
mMealData = new V8Object(mV8rt);
mMealData.add("carbs", mealData.carbs); mMealData.add("carbs", mealData.carbs);
mMealData.add("boluses", mealData.boluses); mMealData.add("boluses", mealData.boluses);
mMealData.add("mealCOB", mealData.mealCOB); mMealData.add("mealCOB", mealData.mealCOB);
mV8rt.add(PARAM_meal_data, mMealData);
if (MainApp.getConfigBuilder().isAMAModeEnabled()) {
mAutosensData = new V8Object(mV8rt);
mAutosensData.add("ratio", autosensDataRatio); mAutosensData.add("ratio", autosensDataRatio);
mV8rt.add(PARAM_autosens_data, mAutosensData);
} else {
mV8rt.addUndefined(PARAM_autosens_data);
}
} }

View file

@ -16,7 +16,6 @@ public class DetermineBasalResultAMA extends APSResult {
public JSONObject json = new JSONObject(); public JSONObject json = new JSONObject();
public double eventualBG; public double eventualBG;
public double snoozeBG; public double snoozeBG;
public String mealAssist;
public IobTotal iob; public IobTotal iob;
public DetermineBasalResultAMA(V8Object result, JSONObject j) { public DetermineBasalResultAMA(V8Object result, JSONObject j) {
@ -26,7 +25,6 @@ public class DetermineBasalResultAMA extends APSResult {
changeRequested = false; changeRequested = false;
rate = -1; rate = -1;
duration = -1; duration = -1;
mealAssist = "";
} else { } else {
reason = result.getString("reason"); reason = result.getString("reason");
eventualBG = result.getDouble("eventualBG"); eventualBG = result.getDouble("eventualBG");
@ -46,9 +44,6 @@ public class DetermineBasalResultAMA extends APSResult {
duration = -1; duration = -1;
changeRequested = false; changeRequested = false;
} }
if (result.contains("mealAssist")) {
mealAssist = result.getString("mealAssist");
} else mealAssist = "";
} }
result.release(); result.release();
} }
@ -74,7 +69,6 @@ public class DetermineBasalResultAMA extends APSResult {
} }
newResult.eventualBG = eventualBG; newResult.eventualBG = eventualBG;
newResult.snoozeBG = snoozeBG; newResult.snoozeBG = snoozeBG;
newResult.mealAssist = new String(mealAssist);
return newResult; return newResult;
} }

View file

@ -17,7 +17,9 @@ public class JSONFormatter {
public static Spanned format(final String jsonString) { public static Spanned format(final String jsonString) {
final JsonVisitor visitor = new JsonVisitor(4, ' '); final JsonVisitor visitor = new JsonVisitor(4, ' ');
try { 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)); return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0));
else else
return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0)); return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0));

View file

@ -403,4 +403,5 @@
<string name="array_of_elements">Array of %d elements.\nActual value:</string> <string name="array_of_elements">Array of %d elements.\nActual value:</string>
<string name="openapsma_autosensdata_label">Autosens data</string> <string name="openapsma_autosensdata_label">Autosens data</string>
<string name="openapsma_scriptdebugdata_label">Script debug</string> <string name="openapsma_scriptdebugdata_label">Script debug</string>
<string name="openapsama_useautosens">Use AMA autosens feature</string>
</resources> </resources>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="openapsama"
android:title="@string/openapsama">
<SwitchPreference
android:defaultValue="false"
android:key="openapsama_useautosens"
android:title="@string/openapsama_useautosens" />
</PreferenceCategory>
</PreferenceScreen>