safety run if SMB fails

This commit is contained in:
AdrianLxM 2018-04-25 01:18:40 +02:00
parent 4217e82e0a
commit a6a4a8837d
9 changed files with 45 additions and 32 deletions

View file

@ -11,5 +11,5 @@ public interface APSInterface {
public APSResult getLastAPSResult(); public APSResult getLastAPSResult();
public Date getLastAPSRun(); public Date getLastAPSRun();
public void invoke(String initiator); public void invoke(String initiator, boolean safety);
} }

View file

@ -9,6 +9,7 @@ import android.app.TaskStackBuilder;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.SystemClock;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
@ -253,7 +254,11 @@ public class LoopPlugin extends PluginBase {
return isDisconnected; return isDisconnected;
} }
public void invoke(String initiator, boolean allowNotification) { public void invoke(String initiator, boolean allowNotification){
invoke(initiator, allowNotification, false);
}
public void invoke(String initiator, boolean allowNotification, boolean safety) {
try { try {
if (Config.logFunctionCalls) if (Config.logFunctionCalls)
log.debug("invoke from " + initiator); log.debug("invoke from " + initiator);
@ -284,7 +289,7 @@ public class LoopPlugin extends PluginBase {
APSInterface usedAPS = ConfigBuilderPlugin.getActiveAPS(); APSInterface usedAPS = ConfigBuilderPlugin.getActiveAPS();
if (usedAPS != null && ((PluginBase) usedAPS).isEnabled(PluginType.APS)) { if (usedAPS != null && ((PluginBase) usedAPS).isEnabled(PluginType.APS)) {
usedAPS.invoke(initiator); usedAPS.invoke(initiator, safety);
result = usedAPS.getLastAPSResult(); result = usedAPS.getLastAPSResult();
} }
@ -348,16 +353,24 @@ public class LoopPlugin extends PluginBase {
if (result.enacted || result.success) { if (result.enacted || result.success) {
lastRun.tbrSetByPump = result; lastRun.tbrSetByPump = result;
lastRun.lastEnact = lastRun.lastAPSRun; lastRun.lastEnact = lastRun.lastAPSRun;
}
MainApp.bus().post(new EventLoopUpdateGui()); MainApp.getConfigBuilder().applySMBRequest(resultAfterConstraints, new Callback() {
} @Override
}); public void run() {
MainApp.getConfigBuilder().applySMBRequest(resultAfterConstraints, new Callback() { //Callback is only called if a bolus was acutally requested
@Override if (result.enacted || result.success) {
public void run() { lastRun.smbSetByPump = result;
if (result.enacted || result.success) { lastRun.lastEnact = lastRun.lastAPSRun;
lastRun.smbSetByPump = result; } else {
lastRun.lastEnact = lastRun.lastAPSRun; new Thread(() -> {
SystemClock.sleep(1000);
LoopPlugin.getPlugin().invoke("safety", allowNotification, true);
}).start();
FabricPrivacy.getInstance().logCustom(new CustomEvent("Loop_Run_Safety"));
}
MainApp.bus().post(new EventLoopUpdateGui());
}
});
} }
MainApp.bus().post(new EventLoopUpdateGui()); MainApp.bus().post(new EventLoopUpdateGui());
} }

View file

@ -72,7 +72,7 @@ public class OpenAPSAMAFragment extends SubscriberFragment implements View.OnCli
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.openapsma_run: case R.id.openapsma_run:
OpenAPSAMAPlugin.getPlugin().invoke("OpenAPSAMA button"); OpenAPSAMAPlugin.getPlugin().invoke("OpenAPSAMA button", false);
FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_AMA_Run")); FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_AMA_Run"));
break; break;
} }

View file

@ -88,8 +88,8 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
} }
@Override @Override
public void invoke(String initiator) { public void invoke(String initiator, boolean safety) {
log.debug("invoke from " + initiator); log.debug("invoke from " + initiator + " safety: " + safety);
lastAPSResult = null; lastAPSResult = null;
DetermineBasalAdapterAMAJS determineBasalAdapterAMAJS; DetermineBasalAdapterAMAJS determineBasalAdapterAMAJS;
try { try {

View file

@ -66,7 +66,7 @@ public class OpenAPSMAFragment extends SubscriberFragment implements View.OnClic
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.openapsma_run: case R.id.openapsma_run:
OpenAPSMAPlugin.getPlugin().invoke("OpenAPSMA button"); OpenAPSMAPlugin.getPlugin().invoke("OpenAPSMA button", false);
FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_MA_Run")); FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_MA_Run"));
break; break;
} }

View file

@ -88,8 +88,8 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
} }
@Override @Override
public void invoke(String initiator) { public void invoke(String initiator, boolean safety) {
log.debug("invoke from " + initiator); log.debug("invoke from " + initiator + " safety: " + safety);
lastAPSResult = null; lastAPSResult = null;
DetermineBasalAdapterMAJS determineBasalAdapterMAJS = null; DetermineBasalAdapterMAJS determineBasalAdapterMAJS = null;
try { try {

View file

@ -214,7 +214,7 @@ public class DetermineBasalAdapterSMBJS {
double autosensDataRatio, double autosensDataRatio,
boolean tempTargetSet, boolean tempTargetSet,
boolean microBolusAllowed, boolean microBolusAllowed,
boolean smbAlwaysAllowed boolean advancedFiltering
) throws JSONException { ) throws JSONException {
String units = profile.getUnits(); String units = profile.getUnits();
@ -250,13 +250,13 @@ public class DetermineBasalAdapterSMBJS {
mProfile.put("min_5m_carbimpact", SP.getDouble(R.string.key_openapsama_min_5m_carbimpact, SMBDefaults.min_5m_carbimpact)); mProfile.put("min_5m_carbimpact", SP.getDouble(R.string.key_openapsama_min_5m_carbimpact, SMBDefaults.min_5m_carbimpact));
} }
mProfile.put("remainingCarbsCap", SMBDefaults.remainingCarbsCap); mProfile.put("remainingCarbsCap", SMBDefaults.remainingCarbsCap);
mProfile.put("enableUAM", SP.getBoolean(R.string.key_use_uam, false)); mProfile.put("enableUAM", SP.getBoolean(R.string.key_use_uam, false)&& advancedFiltering);
mProfile.put("A52_risk_enable", SMBDefaults.A52_risk_enable); mProfile.put("A52_risk_enable", SMBDefaults.A52_risk_enable);
mProfile.put("enableSMB_with_COB", SP.getBoolean(R.string.key_enableSMB_with_COB, false)); mProfile.put("enableSMB_with_COB", SP.getBoolean(R.string.key_enableSMB_with_COB, false));
mProfile.put("enableSMB_with_temptarget", SP.getBoolean(R.string.key_enableSMB_with_temptarget, false)); mProfile.put("enableSMB_with_temptarget", SP.getBoolean(R.string.key_enableSMB_with_temptarget, false));
mProfile.put("allowSMB_with_high_temptarget", SP.getBoolean(R.string.key_allowSMB_with_high_temptarget, false)); mProfile.put("allowSMB_with_high_temptarget", SP.getBoolean(R.string.key_allowSMB_with_high_temptarget, false));
mProfile.put("enableSMB_always", SP.getBoolean(R.string.key_enableSMB_always, false) && smbAlwaysAllowed); mProfile.put("enableSMB_always", SP.getBoolean(R.string.key_enableSMB_always, false) && advancedFiltering);
mProfile.put("enableSMB_after_carbs", SP.getBoolean(R.string.key_enableSMB_after_carbs, false) && smbAlwaysAllowed); mProfile.put("enableSMB_after_carbs", SP.getBoolean(R.string.key_enableSMB_after_carbs, false) && advancedFiltering);
mProfile.put("maxSMBBasalMinutes", SP.getInt("key_smbmaxminutes", SMBDefaults.maxSMBBasalMinutes)); mProfile.put("maxSMBBasalMinutes", SP.getInt("key_smbmaxminutes", SMBDefaults.maxSMBBasalMinutes));
mProfile.put("carbsReqThreshold", SMBDefaults.carbsReqThreshold); mProfile.put("carbsReqThreshold", SMBDefaults.carbsReqThreshold);
@ -315,7 +315,7 @@ public class DetermineBasalAdapterSMBJS {
mAutosensData.put("ratio", 1.0); mAutosensData.put("ratio", 1.0);
} }
mMicrobolusAllowed = microBolusAllowed; mMicrobolusAllowed = microBolusAllowed;
mSMBAlwaysAllowed = smbAlwaysAllowed; mSMBAlwaysAllowed = advancedFiltering;
} }

View file

@ -66,7 +66,7 @@ public class OpenAPSSMBFragment extends SubscriberFragment {
@OnClick(R.id.openapsma_run) @OnClick(R.id.openapsma_run)
public void onRunClick() { public void onRunClick() {
OpenAPSSMBPlugin.getPlugin().invoke("OpenAPSSMB button"); OpenAPSSMBPlugin.getPlugin().invoke("OpenAPSSMB button", false);
FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_SMB_Run")); FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_SMB_Run"));
} }

View file

@ -91,8 +91,8 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
} }
@Override @Override
public void invoke(String initiator) { public void invoke(String initiator, boolean safety) {
log.debug("invoke from " + initiator); log.debug("invoke from " + initiator + " safety: " + safety);
lastAPSResult = null; lastAPSResult = null;
DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS = null; DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS = null;
try { try {
@ -184,13 +184,13 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
lastAutosensResult = new AutosensResult(); lastAutosensResult = new AutosensResult();
} }
Constraint<Boolean> smbAllowed = new Constraint<>(true); Constraint<Boolean> smbAllowed = new Constraint<>(!safety);
MainApp.getConstraintChecker().isSMBModeEnabled(smbAllowed); MainApp.getConstraintChecker().isSMBModeEnabled(smbAllowed);
inputConstraints.copyReasons(smbAllowed); inputConstraints.copyReasons(smbAllowed);
Constraint<Boolean> smbAlwaysEnabled = new Constraint<>(true); Constraint<Boolean> advancedFiltering = new Constraint<>(!safety);
MainApp.getConstraintChecker().isAdvancedFilteringEnabled(smbAlwaysEnabled); MainApp.getConstraintChecker().isAdvancedFilteringEnabled(advancedFiltering);
inputConstraints.copyReasons(smbAlwaysEnabled); inputConstraints.copyReasons(advancedFiltering);
Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart); Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart);
Profiler.log(log, "SMB data gathering", start); Profiler.log(log, "SMB data gathering", start);
@ -201,7 +201,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
lastAutosensResult.ratio, //autosensDataRatio lastAutosensResult.ratio, //autosensDataRatio
isTempTarget, isTempTarget,
smbAllowed.value(), smbAllowed.value(),
smbAlwaysEnabled.value() advancedFiltering.value()
); );
} catch (JSONException e) { } catch (JSONException e) {
log.error(e.getMessage()); log.error(e.getMessage());