2018-03-19 18:21:02 +01:00
|
|
|
package info.nightscout.androidaps.data;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import info.nightscout.androidaps.MainApp;
|
2018-03-19 22:20:42 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.Constraint;
|
2018-03-19 18:21:02 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
|
|
|
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
|
|
|
import info.nightscout.utils.SP;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mike on 19.03.2018.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class ConstraintChecker implements ConstraintsInterface {
|
|
|
|
|
|
|
|
private MainApp mainApp;
|
|
|
|
|
|
|
|
public ConstraintChecker(MainApp mainApp) {
|
|
|
|
this.mainApp = mainApp;
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:45:23 +01:00
|
|
|
|
|
|
|
public Constraint<Boolean> isLoopInvokationAllowed() {
|
|
|
|
return isLoopInvokationAllowed(new Constraint<>(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Constraint<Boolean> isClosedLoopAllowed() {
|
|
|
|
return isClosedLoopAllowed(new Constraint<>(true));
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:21:02 +01:00
|
|
|
@Override
|
2018-03-19 18:45:23 +01:00
|
|
|
public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {
|
2018-03-19 18:21:02 +01:00
|
|
|
|
|
|
|
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
|
|
|
|
for (PluginBase p : constraintsPlugins) {
|
|
|
|
ConstraintsInterface constraint = (ConstraintsInterface) p;
|
|
|
|
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
|
2018-03-19 18:45:23 +01:00
|
|
|
constraint.isLoopInvokationAllowed(value);
|
2018-03-19 18:21:02 +01:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-19 18:45:23 +01:00
|
|
|
public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
|
2018-03-19 18:21:02 +01:00
|
|
|
|
|
|
|
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
|
|
|
|
for (PluginBase p : constraintsPlugins) {
|
|
|
|
ConstraintsInterface constraint = (ConstraintsInterface) p;
|
|
|
|
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
|
2018-03-19 18:45:23 +01:00
|
|
|
constraint.isClosedLoopAllowed(value);
|
2018-03-19 18:21:02 +01:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isAutosensModeEnabled() {
|
|
|
|
boolean result = true;
|
|
|
|
|
|
|
|
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
|
|
|
|
for (PluginBase p : constraintsPlugins) {
|
|
|
|
ConstraintsInterface constrain = (ConstraintsInterface) p;
|
|
|
|
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
|
|
|
|
result = result && constrain.isAutosensModeEnabled();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isAMAModeEnabled() {
|
|
|
|
boolean result = SP.getBoolean("openapsama_useautosens", false);
|
|
|
|
|
|
|
|
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
|
|
|
|
for (PluginBase p : constraintsPlugins) {
|
|
|
|
ConstraintsInterface constrain = (ConstraintsInterface) p;
|
|
|
|
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
|
|
|
|
result = result && constrain.isAMAModeEnabled();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isSMBModeEnabled() {
|
|
|
|
boolean result = true; // TODO update for SMB // SP.getBoolean("openapsama_useautosens", false);
|
|
|
|
|
|
|
|
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
|
|
|
|
for (PluginBase p : constraintsPlugins) {
|
|
|
|
ConstraintsInterface constrain = (ConstraintsInterface) p;
|
|
|
|
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
|
|
|
|
result = result && constrain.isSMBModeEnabled();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Double applyBasalConstraints(Double absoluteRate) {
|
|
|
|
Double rateAfterConstrain = absoluteRate;
|
|
|
|
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
|
|
|
|
for (PluginBase p : constraintsPlugins) {
|
|
|
|
ConstraintsInterface constrain = (ConstraintsInterface) p;
|
|
|
|
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
|
|
|
|
rateAfterConstrain = Math.min(constrain.applyBasalConstraints(absoluteRate), rateAfterConstrain);
|
|
|
|
}
|
|
|
|
return rateAfterConstrain;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Integer applyBasalConstraints(Integer percentRate) {
|
|
|
|
Integer rateAfterConstrain = percentRate;
|
|
|
|
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
|
|
|
|
for (PluginBase p : constraintsPlugins) {
|
|
|
|
ConstraintsInterface constrain = (ConstraintsInterface) p;
|
|
|
|
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
|
|
|
|
rateAfterConstrain = Math.min(constrain.applyBasalConstraints(percentRate), rateAfterConstrain);
|
|
|
|
}
|
|
|
|
return rateAfterConstrain;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Double applyBolusConstraints(Double insulin) {
|
|
|
|
Double insulinAfterConstrain = insulin;
|
|
|
|
ArrayList<PluginBase> 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<PluginBase> 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<PluginBase> 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|