refactor a bit

This commit is contained in:
AdrianLxM 2018-03-19 18:45:23 +01:00
parent ce63655097
commit 01a9e76827
16 changed files with 37 additions and 32 deletions

View file

@ -201,7 +201,7 @@ public class MainApp extends Application {
FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-G5Uploader")); FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-G5Uploader"));
else if (Config.PUMPCONTROL) else if (Config.PUMPCONTROL)
FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-PumpControl")); FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-PumpControl"));
else if (MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) else if (MainApp.getConstraintChecker().isClosedLoopAllowed().get())
FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-ClosedLoop")); FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-ClosedLoop"));
else else
FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-OpenLoop")); FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-OpenLoop"));

View file

@ -20,26 +20,35 @@ public class ConstraintChecker implements ConstraintsInterface {
this.mainApp = mainApp; this.mainApp = mainApp;
} }
public Constraint<Boolean> isLoopInvokationAllowed() {
return isLoopInvokationAllowed(new Constraint<>(true));
}
public Constraint<Boolean> isClosedLoopAllowed() {
return isClosedLoopAllowed(new Constraint<>(true));
}
@Override @Override
public Constraint<Boolean> limitRunningLoop(Constraint<Boolean> value) { public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) { for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constraint = (ConstraintsInterface) p; ConstraintsInterface constraint = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
constraint.limitRunningLoop(value); constraint.isLoopInvokationAllowed(value);
} }
return value; return value;
} }
@Override @Override
public Constraint<Boolean> limitClosedLoop(Constraint<Boolean> value) { public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) { for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constraint = (ConstraintsInterface) p; ConstraintsInterface constraint = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
constraint.limitClosedLoop(value); constraint.isClosedLoopAllowed(value);
} }
return value; return value;
} }

View file

@ -5,9 +5,9 @@ package info.nightscout.androidaps.interfaces;
*/ */
public interface ConstraintsInterface { public interface ConstraintsInterface {
Constraint<Boolean> limitRunningLoop(Constraint<Boolean> value); Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value);
Constraint<Boolean> limitClosedLoop(Constraint<Boolean> value); Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value);
boolean isAutosensModeEnabled(); boolean isAutosensModeEnabled();

View file

@ -184,7 +184,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface {
return new RequirementResult(true, ""); return new RequirementResult(true, "");
case 3: case 3:
Constraint<Boolean> closedLoopEnabled = new Constraint<>(true); Constraint<Boolean> closedLoopEnabled = new Constraint<>(true);
SafetyPlugin.getPlugin().limitClosedLoop(closedLoopEnabled); SafetyPlugin.getPlugin().isClosedLoopAllowed(closedLoopEnabled);
return new RequirementResult(closedLoopEnabled.get(), MainApp.sResources.getString(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.get())); return new RequirementResult(closedLoopEnabled.get(), MainApp.sResources.getString(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.get()));
case 4: case 4:
double maxIOB = MainApp.getConstraintChecker().applyMaxIOBConstraints(1000d); double maxIOB = MainApp.getConstraintChecker().applyMaxIOBConstraints(1000d);
@ -295,14 +295,14 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface {
* Constraints interface * Constraints interface
**/ **/
@Override @Override
public Constraint<Boolean> limitRunningLoop(Constraint<Boolean> value) { public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {
if (objectives.get(0).started.getTime() == 0) if (objectives.get(0).started.getTime() == 0)
value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 1)); value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 1));
return value; return value;
} }
@Override @Override
public Constraint<Boolean> limitClosedLoop(Constraint<Boolean> value) { public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
if (objectives.get(3).started.getTime() == 0) if (objectives.get(3).started.getTime() == 0)
value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 4)); value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 4));
return value; return value;

View file

@ -96,14 +96,14 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface {
* Constraints interface * Constraints interface
**/ **/
@Override @Override
public Constraint<Boolean> limitRunningLoop(Constraint<Boolean> value) { public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {
if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable) if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable)
value.set(false, MainApp.gs(R.string.pumpisnottempbasalcapable)); value.set(false, MainApp.gs(R.string.pumpisnottempbasalcapable));
return value; return value;
} }
@Override @Override
public Constraint<Boolean> limitClosedLoop(Constraint<Boolean> value) { public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
if (!MainApp.isEngineeringModeOrRelease()) if (!MainApp.isEngineeringModeOrRelease())
value.set(false, MainApp.gs(R.string.closed_loop_disabled_on_dev_branch)); value.set(false, MainApp.gs(R.string.closed_loop_disabled_on_dev_branch));

View file

@ -259,8 +259,7 @@ public class LoopPlugin implements PluginBase {
try { try {
if (Config.logFunctionCalls) if (Config.logFunctionCalls)
log.debug("invoke from " + initiator); log.debug("invoke from " + initiator);
Constraint<Boolean> loopEnabled = new Constraint<>(true); Constraint<Boolean> loopEnabled = MainApp.getConstraintChecker().isLoopInvokationAllowed();
MainApp.getConstraintChecker().limitRunningLoop(loopEnabled);
if (!loopEnabled.get()) { if (!loopEnabled.get()) {
String message = MainApp.sResources.getString(R.string.loopdisabled) + "\n" + loopEnabled.getReasons(); String message = MainApp.sResources.getString(R.string.loopdisabled) + "\n" + loopEnabled.getReasons();
@ -331,8 +330,7 @@ public class LoopPlugin implements PluginBase {
return; return;
} }
Constraint<Boolean> closedLoopEnabled = new Constraint<>(true); Constraint<Boolean> closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed();
MainApp.getConstraintChecker().limitClosedLoop(closedLoopEnabled);
if (closedLoopEnabled.get()) { if (closedLoopEnabled.get()) {
if (result.isChangeRequested()) { if (result.isChangeRequested()) {

View file

@ -249,7 +249,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface {
if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress())
determineBasalResultAMA.tempBasalReqested = false; determineBasalResultAMA.tempBasalReqested = false;
// limit requests on openloop mode // limit requests on openloop mode
if (!MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { if (!MainApp.getConstraintChecker().isClosedLoopAllowed().get()) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now);
if (activeTemp != null && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) { if (activeTemp != null && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) {

View file

@ -238,7 +238,7 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface {
if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress())
determineBasalResultMA.tempBasalReqested = false; determineBasalResultMA.tempBasalReqested = false;
// limit requests on openloop mode // limit requests on openloop mode
if (!MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { if (!MainApp.getConstraintChecker().isClosedLoopAllowed().get()) {
TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now);
if (activeTemp != null && determineBasalResultMA.rate == 0 && determineBasalResultMA.duration == 0) { if (activeTemp != null && determineBasalResultMA.rate == 0 && determineBasalResultMA.duration == 0) {
// going to cancel // going to cancel

View file

@ -216,8 +216,7 @@ public class DetermineBasalAdapterSMBJS {
String units = profile.getUnits(); String units = profile.getUnits();
Constraint<Boolean> closedLoopEnabled = new Constraint<>(true); Constraint<Boolean> closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed();
MainApp.getConstraintChecker().limitClosedLoop(closedLoopEnabled);
mProfile = new JSONObject(); mProfile = new JSONObject();

View file

@ -254,7 +254,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface {
if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress())
determineBasalResultSMB.tempBasalReqested = false; determineBasalResultSMB.tempBasalReqested = false;
// limit requests on openloop mode // limit requests on openloop mode
if (!MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { if (!MainApp.getConstraintChecker().isClosedLoopAllowed().get()) {
TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now);
if (activeTemp != null && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) { if (activeTemp != null && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) {
// going to cancel // going to cancel

View file

@ -1017,8 +1017,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
} }
Constraint<Boolean> closedLoopEnabled = new Constraint<>(true); Constraint<Boolean> closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed();
MainApp.getConstraintChecker().limitClosedLoop(closedLoopEnabled);
// open loop mode // open loop mode
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun; final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;

View file

@ -1422,14 +1422,14 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
private boolean validBasalRateProfileSelectedOnPump = true; private boolean validBasalRateProfileSelectedOnPump = true;
@Override @Override
public Constraint<Boolean> limitRunningLoop(Constraint<Boolean> value) { public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {
if (!validBasalRateProfileSelectedOnPump) if (!validBasalRateProfileSelectedOnPump)
value.set(false, MainApp.gs(R.string.novalidbasalrate)); value.set(false, MainApp.gs(R.string.novalidbasalrate));
return value; return value;
} }
@Override @Override
public Constraint<Boolean> limitClosedLoop(Constraint<Boolean> value) { public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
return value; return value;
} }

View file

@ -434,12 +434,12 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface,
*/ */
@Override @Override
public Constraint<Boolean> limitRunningLoop(Constraint<Boolean> value) { public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {
return value; return value;
} }
@Override @Override
public Constraint<Boolean> limitClosedLoop(Constraint<Boolean> value) { public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
return value; return value;
} }

View file

@ -274,12 +274,12 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface,
// Constraints interface // Constraints interface
@Override @Override
public Constraint<Boolean> limitRunningLoop(Constraint<Boolean> value) { public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {
return value; return value;
} }
@Override @Override
public Constraint<Boolean> limitClosedLoop(Constraint<Boolean> value) { public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
return value; return value;
} }

View file

@ -1080,12 +1080,12 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
// Constraints // Constraints
@Override @Override
public Constraint<Boolean> limitRunningLoop(Constraint<Boolean> value) { public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {
return value; return value;
} }
@Override @Override
public Constraint<Boolean> limitClosedLoop(Constraint<Boolean> value) { public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
return value; return value;
} }

View file

@ -435,7 +435,7 @@ public class ActionStringHandler {
// decide if enabled/disabled closed/open; what Plugin as APS? // decide if enabled/disabled closed/open; what Plugin as APS?
final LoopPlugin activeloop = MainApp.getConfigBuilder().getActiveLoop(); final LoopPlugin activeloop = MainApp.getConfigBuilder().getActiveLoop();
if (activeloop != null && activeloop.isEnabled(activeloop.getType())) { if (activeloop != null && activeloop.isEnabled(activeloop.getType())) {
if (MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { if (MainApp.getConstraintChecker().isClosedLoopAllowed().get()) {
ret += "CLOSED LOOP\n"; ret += "CLOSED LOOP\n";
} else { } else {
ret += "OPEN LOOP\n"; ret += "OPEN LOOP\n";