package info.nightscout.androidaps.data; import java.util.ArrayList; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; /** * Created by mike on 19.03.2018. */ public class ConstraintChecker implements ConstraintsInterface { private MainApp mainApp; public ConstraintChecker(MainApp mainApp) { this.mainApp = mainApp; } public Constraint isLoopInvokationAllowed() { return isLoopInvokationAllowed(new Constraint<>(true)); } public Constraint isClosedLoopAllowed() { return isClosedLoopAllowed(new Constraint<>(true)); } public Constraint isAutosensModeEnabled() { return isAutosensModeEnabled(new Constraint<>(true)); } public Constraint isAMAModeEnabled() { return isAMAModeEnabled(new Constraint<>(true)); } public Constraint isSMBModeEnabled() { return isSMBModeEnabled(new Constraint<>(true)); } @Override public Constraint isLoopInvokationAllowed(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; constraint.isLoopInvokationAllowed(value); } return value; } @Override public Constraint isClosedLoopAllowed(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; constraint.isClosedLoopAllowed(value); } return value; } @Override public Constraint isAutosensModeEnabled(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; constraint.isAutosensModeEnabled(value); } return value; } @Override public Constraint isAMAModeEnabled(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; constrain.isAMAModeEnabled(value); } return value; } @Override public Constraint isSMBModeEnabled(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; constraint.isSMBModeEnabled(value); } return value; } @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; constraint.applyBasalConstraints(absoluteRate, profile); } return absoluteRate; } @Override public Integer applyBasalPercentConstraints(Integer percentRate) { Integer rateAfterConstrain = percentRate; ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; rateAfterConstrain = Math.min(constrain.applyBasalPercentConstraints(percentRate), rateAfterConstrain); } return rateAfterConstrain; } @Override public Double applyBolusConstraints(Double insulin) { Double insulinAfterConstrain = insulin; ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; insulinAfterConstrain = Math.min(constrain.applyBolusConstraints(insulin), insulinAfterConstrain); } return insulinAfterConstrain; } @Override public Integer applyCarbsConstraints(Integer carbs) { Integer carbsAfterConstrain = carbs; ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; carbsAfterConstrain = Math.min(constrain.applyCarbsConstraints(carbs), carbsAfterConstrain); } return carbsAfterConstrain; } @Override public Double applyMaxIOBConstraints(Double maxIob) { Double maxIobAfterConstrain = maxIob; ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; maxIobAfterConstrain = Math.min(constrain.applyMaxIOBConstraints(maxIob), maxIobAfterConstrain); } return maxIobAfterConstrain; } }