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.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);

View file

@ -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<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -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));

View file

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