APS plugins injection

This commit is contained in:
Milos Kozak 2019-12-30 10:28:56 +01:00
parent 716053229a
commit 270c1c69f4
4 changed files with 168 additions and 95 deletions

View file

@ -22,10 +22,10 @@ import info.nightscout.androidaps.plugins.aps.loop.APSResult;
import info.nightscout.androidaps.plugins.aps.loop.ScriptReader;
import info.nightscout.androidaps.plugins.aps.openAPSMA.events.EventOpenAPSUpdateGui;
import info.nightscout.androidaps.plugins.aps.openAPSMA.events.EventOpenAPSUpdateResultGui;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.bus.RxBusWrapper;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
@ -36,11 +36,19 @@ import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.HardLimits;
import info.nightscout.androidaps.utils.Profiler;
import info.nightscout.androidaps.utils.Round;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
@Singleton
public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
private final AAPSLogger aapsLogger;
private final RxBusWrapper rxBus;
private final ConstraintChecker constraintChecker;
private final ResourceHelper resourceHelper;
private final ProfileFunction profileFunction;
private final MainApp mainApp;
private final ConfigBuilderPlugin configBuilderPlugin;
private final TreatmentsPlugin treatmentsPlugin;
// last values
DetermineBasalAdapterAMAJS lastDetermineBasalAdapterAMAJS = null;
long lastAPSRun = 0;
@ -48,7 +56,16 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
AutosensResult lastAutosensResult = null;
@Inject
public OpenAPSAMAPlugin(AAPSLogger aapsLogger) {
public OpenAPSAMAPlugin(
AAPSLogger aapsLogger,
RxBusWrapper rxBus,
ConstraintChecker constraintChecker,
ResourceHelper resourceHelper,
ProfileFunction profileFunction,
MainApp mainApp,
ConfigBuilderPlugin configBuilderPlugin,
TreatmentsPlugin treatmentsPlugin
) {
super(new PluginDescription()
.mainType(PluginType.APS)
.fragmentClass(OpenAPSAMAFragment.class.getName())
@ -58,17 +75,24 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
.description(R.string.description_ama)
);
this.aapsLogger = aapsLogger;
this.rxBus = rxBus;
this.constraintChecker = constraintChecker;
this.resourceHelper = resourceHelper;
this.profileFunction = profileFunction;
this.mainApp = mainApp;
this.configBuilderPlugin = configBuilderPlugin;
this.treatmentsPlugin = treatmentsPlugin;
}
@Override
public boolean specialEnableCondition() {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
PumpInterface pump = configBuilderPlugin.getActivePump();
return pump == null || pump.getPumpDescription().isTempBasalCapable;
}
@Override
public boolean specialShowInListCondition() {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
PumpInterface pump = configBuilderPlugin.getActivePump();
return pump == null || pump.getPumpDescription().isTempBasalCapable;
}
@ -87,37 +111,37 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
aapsLogger.debug(LTag.APS, "invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback);
lastAPSResult = null;
DetermineBasalAdapterAMAJS determineBasalAdapterAMAJS;
determineBasalAdapterAMAJS = new DetermineBasalAdapterAMAJS(new ScriptReader(MainApp.instance().getBaseContext()), aapsLogger);
determineBasalAdapterAMAJS = new DetermineBasalAdapterAMAJS(new ScriptReader(mainApp), aapsLogger);
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
Profile profile = ProfileFunctions.getInstance().getProfile();
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
Profile profile = profileFunction.getProfile();
PumpInterface pump = configBuilderPlugin.getActivePump();
if (profile == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.noprofileselected)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.noprofileselected));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.noprofileselected)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.noprofileselected));
return;
}
if (pump == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.nopumpselected)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.nopumpselected));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.nopumpselected)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.nopumpselected));
return;
}
if (!isEnabled(PluginType.APS)) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_disabled)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.openapsma_disabled));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.openapsma_disabled)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.openapsma_disabled));
return;
}
if (glucoseStatus == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.openapsma_noglucosedata));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.openapsma_noglucosedata)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.openapsma_noglucosedata));
return;
}
double maxBasal = ConstraintChecker.getInstance().getMaxBasalAllowed(profile).value();
double maxBasal = constraintChecker.getMaxBasalAllowed(profile).value();
double minBg = profile.getTargetLowMgdl();
double maxBg = profile.getTargetHighMgdl();
double targetBg = profile.getTargetMgdl();
@ -131,17 +155,17 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
Profiler.log(aapsLogger, LTag.APS, "calculateIobArrayInDia()", startPart);
startPart = System.currentTimeMillis();
MealData mealData = TreatmentsPlugin.getPlugin().getMealData();
MealData mealData = treatmentsPlugin.getMealData();
Profiler.log(aapsLogger, LTag.APS, "getMealData()", startPart);
double maxIob = ConstraintChecker.getInstance().getMaxIOBAllowed().value();
double maxIob = constraintChecker.getMaxIOBAllowed().value();
minBg = HardLimits.verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]);
maxBg = HardLimits.verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]);
targetBg = HardLimits.verifyHardLimits(targetBg, "targetBg", HardLimits.VERY_HARD_LIMIT_TARGET_BG[0], HardLimits.VERY_HARD_LIMIT_TARGET_BG[1]);
boolean isTempTarget = false;
TempTarget tempTarget = TreatmentsPlugin.getPlugin().getTempTargetFromHistory(System.currentTimeMillis());
TempTarget tempTarget = treatmentsPlugin.getTempTargetFromHistory(System.currentTimeMillis());
if (tempTarget != null) {
isTempTarget = true;
minBg = HardLimits.verifyHardLimits(tempTarget.low, "minBg", HardLimits.VERY_HARD_LIMIT_TEMP_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_TEMP_MIN_BG[1]);
@ -162,10 +186,10 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
return;
startPart = System.currentTimeMillis();
if (ConstraintChecker.getInstance().isAutosensModeEnabled().value()) {
if (constraintChecker.isAutosensModeEnabled().value()) {
AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensDataSynchronized("OpenAPSPlugin");
if (autosensData == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openaps_noasdata)));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.openaps_noasdata)));
return;
}
lastAutosensResult = autosensData.autosensResult;
@ -179,7 +203,7 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
start = System.currentTimeMillis();
try {
determineBasalAdapterAMAJS.setData(profile, maxIob, maxBasal, minBg, maxBg, targetBg, ConfigBuilderPlugin.getPlugin().getActivePump().getBaseBasalRate(), iobArray, glucoseStatus, mealData,
determineBasalAdapterAMAJS.setData(profile, maxIob, maxBasal, minBg, maxBg, targetBg, configBuilderPlugin.getActivePump().getBaseBasalRate(), iobArray, glucoseStatus, mealData,
lastAutosensResult.ratio, //autosensDataRatio
isTempTarget
);
@ -198,7 +222,7 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
lastAPSResult = null;
lastAPSRun = 0;
} else {
if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress())
if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !treatmentsPlugin.isTempBasalInProgress())
determineBasalResultAMA.tempBasalRequested = false;
determineBasalResultAMA.iob = iobArray[0];
@ -215,7 +239,7 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
lastAPSResult = determineBasalResultAMA;
lastAPSRun = now;
}
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateGui());
rxBus.send(new EventOpenAPSUpdateGui());
//deviceStatus.suggested = determineBasalResultAMA.json;
}

View file

@ -22,10 +22,10 @@ import info.nightscout.androidaps.plugins.aps.loop.APSResult;
import info.nightscout.androidaps.plugins.aps.loop.ScriptReader;
import info.nightscout.androidaps.plugins.aps.openAPSMA.events.EventOpenAPSUpdateGui;
import info.nightscout.androidaps.plugins.aps.openAPSMA.events.EventOpenAPSUpdateResultGui;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.bus.RxBusWrapper;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil;
@ -33,6 +33,7 @@ import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.HardLimits;
import info.nightscout.androidaps.utils.Profiler;
import info.nightscout.androidaps.utils.Round;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
import static info.nightscout.androidaps.utils.HardLimits.checkOnlyHardLimits;
import static info.nightscout.androidaps.utils.HardLimits.verifyHardLimits;
@ -40,6 +41,13 @@ import static info.nightscout.androidaps.utils.HardLimits.verifyHardLimits;
@Singleton
public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
private final AAPSLogger aapsLogger;
private final RxBusWrapper rxBus;
private final ConstraintChecker constraintChecker;
private final ResourceHelper resourceHelper;
private final ProfileFunction profileFunction;
private final MainApp mainApp;
private final ConfigBuilderPlugin configBuilderPlugin;
private final TreatmentsPlugin treatmentsPlugin;
// last values
DetermineBasalAdapterMAJS lastDetermineBasalAdapterMAJS = null;
@ -47,7 +55,16 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
DetermineBasalResultMA lastAPSResult = null;
@Inject
public OpenAPSMAPlugin(AAPSLogger aapsLogger) {
public OpenAPSMAPlugin(
AAPSLogger aapsLogger,
RxBusWrapper rxBus,
ConstraintChecker constraintChecker,
ResourceHelper resourceHelper,
ProfileFunction profileFunction,
MainApp mainApp,
ConfigBuilderPlugin configBuilderPlugin,
TreatmentsPlugin treatmentsPlugin
) {
super(new PluginDescription()
.mainType(PluginType.APS)
.fragmentClass(OpenAPSMAFragment.class.getName())
@ -57,17 +74,24 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
.description(R.string.description_ma)
);
this.aapsLogger = aapsLogger;
this.rxBus = rxBus;
this.constraintChecker = constraintChecker;
this.resourceHelper = resourceHelper;
this.profileFunction = profileFunction;
this.mainApp = mainApp;
this.configBuilderPlugin = configBuilderPlugin;
this.treatmentsPlugin = treatmentsPlugin;
}
@Override
public boolean specialEnableCondition() {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
PumpInterface pump = configBuilderPlugin.getActivePump();
return pump == null || pump.getPumpDescription().isTempBasalCapable;
}
@Override
public boolean specialShowInListCondition() {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
PumpInterface pump = configBuilderPlugin.getActivePump();
return pump == null || pump.getPumpDescription().isTempBasalCapable;
}
@ -86,37 +110,37 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
aapsLogger.debug(LTag.APS, "invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback);
lastAPSResult = null;
DetermineBasalAdapterMAJS determineBasalAdapterMAJS;
determineBasalAdapterMAJS = new DetermineBasalAdapterMAJS(new ScriptReader(MainApp.instance().getBaseContext()), aapsLogger);
determineBasalAdapterMAJS = new DetermineBasalAdapterMAJS(new ScriptReader(mainApp), aapsLogger);
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
Profile profile = ProfileFunctions.getInstance().getProfile();
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
Profile profile = profileFunction.getProfile();
PumpInterface pump = configBuilderPlugin.getActivePump();
if (profile == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.noprofileselected)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.noprofileselected));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.noprofileselected)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.noprofileselected));
return;
}
if (pump == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.nopumpselected)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.nopumpselected));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.nopumpselected)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.nopumpselected));
return;
}
if (!isEnabled(PluginType.APS)) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_disabled)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.openapsma_disabled));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.openapsma_disabled)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.openapsma_disabled));
return;
}
if (glucoseStatus == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.openapsma_noglucosedata));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.openapsma_noglucosedata)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.openapsma_noglucosedata));
return;
}
double maxBasal = ConstraintChecker.getInstance().getMaxBasalAllowed(profile).value();
double maxBasal = constraintChecker.getMaxBasalAllowed(profile).value();
double minBg = profile.getTargetLowMgdl();
double maxBg = profile.getTargetHighMgdl();
@ -126,23 +150,23 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
maxBg = Round.roundTo(maxBg, 0.1d);
long start = System.currentTimeMillis();
TreatmentsPlugin.getPlugin().updateTotalIOBTreatments();
TreatmentsPlugin.getPlugin().updateTotalIOBTempBasals();
IobTotal bolusIob = TreatmentsPlugin.getPlugin().getLastCalculationTreatments();
IobTotal basalIob = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals();
treatmentsPlugin.updateTotalIOBTreatments();
treatmentsPlugin.updateTotalIOBTempBasals();
IobTotal bolusIob = treatmentsPlugin.getLastCalculationTreatments();
IobTotal basalIob = treatmentsPlugin.getLastCalculationTempBasals();
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
MealData mealData = TreatmentsPlugin.getPlugin().getMealData();
MealData mealData = treatmentsPlugin.getMealData();
double maxIob = ConstraintChecker.getInstance().getMaxIOBAllowed().value();
double maxIob = constraintChecker.getMaxIOBAllowed().value();
Profiler.log(aapsLogger, LTag.APS, "MA data gathering", start);
minBg = verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]);
maxBg = verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]);
targetBg = verifyHardLimits(targetBg, "targetBg", HardLimits.VERY_HARD_LIMIT_TARGET_BG[0], HardLimits.VERY_HARD_LIMIT_TARGET_BG[1]);
TempTarget tempTarget = TreatmentsPlugin.getPlugin().getTempTargetFromHistory(System.currentTimeMillis());
TempTarget tempTarget = treatmentsPlugin.getTempTargetFromHistory(System.currentTimeMillis());
if (tempTarget != null) {
minBg = verifyHardLimits(tempTarget.low, "minBg", HardLimits.VERY_HARD_LIMIT_TEMP_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_TEMP_MIN_BG[1]);
maxBg = verifyHardLimits(tempTarget.high, "maxBg", HardLimits.VERY_HARD_LIMIT_TEMP_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_TEMP_MAX_BG[1]);
@ -162,7 +186,7 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
start = System.currentTimeMillis();
try {
determineBasalAdapterMAJS.setData(profile, maxIob, maxBasal, minBg, maxBg, targetBg, ConfigBuilderPlugin.getPlugin().getActivePump().getBaseBasalRate(), iobTotal, glucoseStatus, mealData);
determineBasalAdapterMAJS.setData(profile, maxIob, maxBasal, minBg, maxBg, targetBg, configBuilderPlugin.getActivePump().getBaseBasalRate(), iobTotal, glucoseStatus, mealData);
} catch (JSONException e) {
FabricPrivacy.logException(e);
return;
@ -179,8 +203,8 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
lastAPSResult = null;
lastAPSRun = 0;
} else {
// Fix bug determinef basal
if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress())
// Fix bug determine basal
if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !treatmentsPlugin.isTempBasalInProgress())
determineBasalResultMA.tempBasalRequested = false;
determineBasalResultMA.iob = iobTotal;
@ -195,7 +219,7 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
lastAPSResult = determineBasalResultMA;
lastAPSRun = now;
}
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateGui());
rxBus.send(new EventOpenAPSUpdateGui());
}

View file

@ -25,10 +25,10 @@ import info.nightscout.androidaps.plugins.aps.loop.APSResult;
import info.nightscout.androidaps.plugins.aps.loop.ScriptReader;
import info.nightscout.androidaps.plugins.aps.openAPSMA.events.EventOpenAPSUpdateGui;
import info.nightscout.androidaps.plugins.aps.openAPSMA.events.EventOpenAPSUpdateResultGui;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.bus.RxBusWrapper;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult;
@ -41,12 +41,20 @@ import info.nightscout.androidaps.utils.HardLimits;
import info.nightscout.androidaps.utils.Profiler;
import info.nightscout.androidaps.utils.Round;
import info.nightscout.androidaps.utils.ToastUtils;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
@Singleton
public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, ConstraintsInterface {
private final AAPSLogger aapsLogger;
private final RxBusWrapper rxBus;
private final ConstraintChecker constraintChecker;
private final ResourceHelper resourceHelper;
private final ProfileFunction profileFunction;
private final MainApp mainApp;
private final ConfigBuilderPlugin configBuilderPlugin;
private final TreatmentsPlugin treatmentsPlugin;
private static OpenAPSSMBPlugin openAPSSMBPlugin;
private final AAPSLogger aapsLogger;
/**
* @deprecated Use dagger to get an instance
@ -65,7 +73,16 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
AutosensResult lastAutosensResult = null;
@Inject
public OpenAPSSMBPlugin(AAPSLogger aapsLogger) {
public OpenAPSSMBPlugin(
AAPSLogger aapsLogger,
RxBusWrapper rxBus,
ConstraintChecker constraintChecker,
ResourceHelper resourceHelper,
ProfileFunction profileFunction,
MainApp mainApp,
ConfigBuilderPlugin configBuilderPlugin,
TreatmentsPlugin treatmentsPlugin
) {
super(new PluginDescription()
.mainType(PluginType.APS)
.fragmentClass(OpenAPSSMBFragment.class.getName())
@ -76,17 +93,24 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
);
this.openAPSSMBPlugin = this; // TODO: only while transitioning to Dagger
this.aapsLogger = aapsLogger;
this.rxBus = rxBus;
this.constraintChecker = constraintChecker;
this.resourceHelper = resourceHelper;
this.profileFunction = profileFunction;
this.mainApp = mainApp;
this.configBuilderPlugin = configBuilderPlugin;
this.treatmentsPlugin = treatmentsPlugin;
}
@Override
public boolean specialEnableCondition() {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
PumpInterface pump = configBuilderPlugin.getActivePump();
return pump == null || pump.getPumpDescription().isTempBasalCapable;
}
@Override
public boolean specialShowInListCondition() {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
PumpInterface pump = configBuilderPlugin.getActivePump();
return pump == null || pump.getPumpDescription().isTempBasalCapable;
}
@ -102,42 +126,42 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
@Override
public void invoke(String initiator, boolean tempBasalFallback) {
aapsLogger.debug(LTag.APS, "invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback);
aapsLogger.debug(LTag.APS, "invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback);
lastAPSResult = null;
DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS;
determineBasalAdapterSMBJS = new DetermineBasalAdapterSMBJS(new ScriptReader(MainApp.instance().getBaseContext()), aapsLogger);
determineBasalAdapterSMBJS = new DetermineBasalAdapterSMBJS(new ScriptReader(mainApp), aapsLogger);
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
Profile profile = ProfileFunctions.getInstance().getProfile();
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
Profile profile = profileFunction.getProfile();
PumpInterface pump = configBuilderPlugin.getActivePump();
if (profile == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.noprofileselected)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.noprofileselected));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.noprofileselected)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.noprofileselected));
return;
}
if (pump == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.nopumpselected)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.nopumpselected));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.nopumpselected)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.nopumpselected));
return;
}
if (!isEnabled(PluginType.APS)) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_disabled)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.openapsma_disabled));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.openapsma_disabled)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.openapsma_disabled));
return;
}
if (glucoseStatus == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata)));
aapsLogger.debug(LTag.APS, MainApp.gs(R.string.openapsma_noglucosedata));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.openapsma_noglucosedata)));
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.openapsma_noglucosedata));
return;
}
Constraint<Double> inputConstraints = new Constraint<>(0d); // fake. only for collecting all results
Constraint<Double> maxBasalConstraint = ConstraintChecker.getInstance().getMaxBasalAllowed(profile);
Constraint<Double> maxBasalConstraint = constraintChecker.getMaxBasalAllowed(profile);
inputConstraints.copyReasons(maxBasalConstraint);
double maxBasal = maxBasalConstraint.value();
double minBg = profile.getTargetLowMgdl();
@ -150,10 +174,10 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
long start = System.currentTimeMillis();
long startPart = System.currentTimeMillis();
MealData mealData = TreatmentsPlugin.getPlugin().getMealData();
Profiler.log(aapsLogger, LTag.APS, "getMealData()", startPart);
MealData mealData = treatmentsPlugin.getMealData();
Profiler.log(aapsLogger, LTag.APS, "getMealData()", startPart);
Constraint<Double> maxIOBAllowedConstraint = ConstraintChecker.getInstance().getMaxIOBAllowed();
Constraint<Double> maxIOBAllowedConstraint = constraintChecker.getMaxIOBAllowed();
inputConstraints.copyReasons(maxIOBAllowedConstraint);
double maxIob = maxIOBAllowedConstraint.value();
@ -162,7 +186,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
targetBg = verifyHardLimits(targetBg, "targetBg", HardLimits.VERY_HARD_LIMIT_TARGET_BG[0], HardLimits.VERY_HARD_LIMIT_TARGET_BG[1]);
boolean isTempTarget = false;
TempTarget tempTarget = TreatmentsPlugin.getPlugin().getTempTargetFromHistory(System.currentTimeMillis());
TempTarget tempTarget = treatmentsPlugin.getTempTargetFromHistory(System.currentTimeMillis());
if (tempTarget != null) {
isTempTarget = true;
minBg = verifyHardLimits(tempTarget.low, "minBg", HardLimits.VERY_HARD_LIMIT_TEMP_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_TEMP_MIN_BG[1]);
@ -183,10 +207,10 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
return;
startPart = System.currentTimeMillis();
if (ConstraintChecker.getInstance().isAutosensModeEnabled().value()) {
if (constraintChecker.isAutosensModeEnabled().value()) {
AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensDataSynchronized("OpenAPSPlugin");
if (autosensData == null) {
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openaps_noasdata)));
rxBus.send(new EventOpenAPSUpdateResultGui(resourceHelper.gs(R.string.openaps_noasdata)));
return;
}
lastAutosensResult = autosensData.autosensResult;
@ -196,27 +220,27 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
}
IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayForSMB(lastAutosensResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, isTempTarget);
Profiler.log(aapsLogger, LTag.APS, "calculateIobArrayInDia()", startPart);
Profiler.log(aapsLogger, LTag.APS, "calculateIobArrayInDia()", startPart);
startPart = System.currentTimeMillis();
Constraint<Boolean> smbAllowed = new Constraint<>(!tempBasalFallback);
ConstraintChecker.getInstance().isSMBModeEnabled(smbAllowed);
constraintChecker.isSMBModeEnabled(smbAllowed);
inputConstraints.copyReasons(smbAllowed);
Constraint<Boolean> advancedFiltering = new Constraint<>(!tempBasalFallback);
ConstraintChecker.getInstance().isAdvancedFilteringEnabled(advancedFiltering);
constraintChecker.isAdvancedFilteringEnabled(advancedFiltering);
inputConstraints.copyReasons(advancedFiltering);
Constraint<Boolean> uam = new Constraint<>(true);
ConstraintChecker.getInstance().isUAMEnabled(uam);
constraintChecker.isUAMEnabled(uam);
inputConstraints.copyReasons(uam);
Profiler.log(aapsLogger, LTag.APS, "detectSensitivityandCarbAbsorption()", startPart);
Profiler.log(aapsLogger, LTag.APS, "SMB data gathering", start);
Profiler.log(aapsLogger, LTag.APS, "detectSensitivityandCarbAbsorption()", startPart);
Profiler.log(aapsLogger, LTag.APS, "SMB data gathering", start);
start = System.currentTimeMillis();
try {
determineBasalAdapterSMBJS.setData(profile, maxIob, maxBasal, minBg, maxBg, targetBg, ConfigBuilderPlugin.getPlugin().getActivePump().getBaseBasalRate(), iobArray, glucoseStatus, mealData,
determineBasalAdapterSMBJS.setData(profile, maxIob, maxBasal, minBg, maxBg, targetBg, configBuilderPlugin.getActivePump().getBaseBasalRate(), iobArray, glucoseStatus, mealData,
lastAutosensResult.ratio, //autosensDataRatio
isTempTarget,
smbAllowed.value(),
@ -231,16 +255,16 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
long now = System.currentTimeMillis();
DetermineBasalResultSMB determineBasalResultSMB = determineBasalAdapterSMBJS.invoke();
Profiler.log(aapsLogger, LTag.APS, "SMB calculation", start);
Profiler.log(aapsLogger, LTag.APS, "SMB calculation", start);
if (determineBasalResultSMB == null) {
aapsLogger.error(LTag.APS, "SMB calculation returned null");
aapsLogger.error(LTag.APS, "SMB calculation returned null");
lastDetermineBasalAdapterSMBJS = null;
lastAPSResult = null;
lastAPSRun = 0;
} else {
// TODO still needed with oref1?
// Fix bug determine basal
if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress())
if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !treatmentsPlugin.isTempBasalInProgress())
determineBasalResultSMB.tempBasalRequested = false;
determineBasalResultSMB.iob = iobArray[0];
@ -257,7 +281,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
lastAPSResult = determineBasalResultSMB;
lastAPSRun = now;
}
RxBus.Companion.getINSTANCE().send(new EventOpenAPSUpdateGui());
rxBus.send(new EventOpenAPSUpdateGui());
//deviceStatus.suggested = determineBasalResultAMA.json;
}
@ -272,12 +296,12 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr
if (newvalue < lowLimit || newvalue > highLimit) {
newvalue = Math.max(newvalue, lowLimit);
newvalue = Math.min(newvalue, highLimit);
String msg = String.format(MainApp.gs(R.string.valueoutofrange), valueName);
String msg = String.format(resourceHelper.gs(R.string.valueoutofrange), valueName);
msg += ".\n";
msg += String.format(MainApp.gs(R.string.valuelimitedto), value, newvalue);
msg += String.format(resourceHelper.gs(R.string.valuelimitedto), value, newvalue);
aapsLogger.error(LTag.APS, msg);
NSUpload.uploadError(msg);
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), msg, R.raw.error);
ToastUtils.showToastInUiThread(mainApp, msg, R.raw.error);
}
return newvalue;
}

View file

@ -5,7 +5,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import dagger.android.support.DaggerDialogFragment
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.constraints.objectives.events.EventObjectivesUpdateGui
@ -15,11 +14,13 @@ import info.nightscout.androidaps.plugins.constraints.objectives.objectives.Obje
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.ToastUtils
import info.nightscout.androidaps.utils.resources.ResourceHelper
import kotlinx.android.synthetic.main.objectives_exam_fragment.*
import javax.inject.Inject
class ObjectivesExamDialog : DaggerDialogFragment() {
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var resourceHelper: ResourceHelper
companion object {
var objective: Objective? = null
@ -75,7 +76,7 @@ class ObjectivesExamDialog : DaggerDialogFragment() {
objectives_exam_hints.addView(h.generate(context))
}
// Disabled to
objectives_exam_disabledto.text = MainApp.gs(R.string.answerdisabledto, DateUtil.timeString(task.disabledTo))
objectives_exam_disabledto.text = resourceHelper.gs(R.string.answerdisabledto, DateUtil.timeString(task.disabledTo))
objectives_exam_disabledto.visibility = if (task.isEnabledAnswer) View.GONE else View.VISIBLE
// Buttons
objectives_exam_verify.isEnabled = !task.answered && task.isEnabledAnswer