OmnipodPumpPlugin formatting and ordering

This commit is contained in:
Bart Sopers 2020-08-22 02:11:09 +02:00
parent e9d53eea52
commit 749851f249

View file

@ -121,22 +121,17 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
// variables for handling statuses and history // variables for handling statuses and history
protected boolean firstRun = true; protected boolean firstRun = true;
protected boolean isRefresh = false; protected boolean isRefresh = false;
private boolean isInitialized = false;
private RileyLinkOmnipodService rileyLinkOmnipodService;
private boolean busy = false;
protected boolean hasTimeDateOrTimeZoneChanged = false; protected boolean hasTimeDateOrTimeZoneChanged = false;
protected boolean serviceRunning = false;
protected boolean displayConnectionMessages = false;
private boolean isInitialized = false;
private RileyLinkOmnipodService rileyLinkOmnipodService;
private boolean busy = false;
private int timeChangeRetries = 0; private int timeChangeRetries = 0;
private Profile currentProfile; private Profile currentProfile;
private long nextPodCheck = 0L; private long nextPodCheck = 0L;
private boolean sentIdToFirebase; private boolean sentIdToFirebase;
// BEGIN PumpPluginAbstract
protected boolean serviceRunning = false;
protected boolean displayConnectionMessages = false;
// END PumpPluginAbstract
@Inject @Inject
public OmnipodPumpPlugin( public OmnipodPumpPlugin(
HasAndroidInjector injector, HasAndroidInjector injector,
@ -186,12 +181,6 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
this.serviceConnection = new ServiceConnection() { this.serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
aapsLogger.debug(LTag.PUMP, "RileyLinkOmnipodService is disconnected");
rileyLinkOmnipodService = null;
}
@Override @Override
public void onServiceConnected(ComponentName name, IBinder service) { public void onServiceConnected(ComponentName name, IBinder service) {
aapsLogger.debug(LTag.PUMP, "RileyLinkOmnipodService is connected"); aapsLogger.debug(LTag.PUMP, "RileyLinkOmnipodService is connected");
@ -211,6 +200,12 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
} }
}).start(); }).start();
} }
@Override
public void onServiceDisconnected(ComponentName name) {
aapsLogger.debug(LTag.PUMP, "RileyLinkOmnipodService is disconnected");
rileyLinkOmnipodService = null;
}
}; };
} }
@ -349,6 +344,11 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return isServiceSet() && isInitialized; return isServiceSet() && isInitialized;
} }
@Override
public boolean isSuspended() {
return !podStateManager.isPodRunning() || podStateManager.isSuspended();
}
@Override @Override
public boolean isBusy() { public boolean isBusy() {
if (displayConnectionMessages) if (displayConnectionMessages)
@ -363,6 +363,10 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return false; return false;
} }
@Override public void setBusy(boolean busy) {
this.busy = busy;
}
@Override @Override
public void triggerPumpConfigurationChangedEvent() { public void triggerPumpConfigurationChangedEvent() {
rxBus.send(new EventOmnipodPumpValuesChanged()); rxBus.send(new EventOmnipodPumpValuesChanged());
@ -390,14 +394,6 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
omnipodPumpStatus.setLastCommunicationToNow(); omnipodPumpStatus.setLastCommunicationToNow();
} }
@Override @NonNull public PumpDescription getPumpDescription() {
return pumpDescription;
}
public OmnipodUIComm getDeviceCommandExecutor() {
return rileyLinkOmnipodService.getDeviceCommandExecutor();
}
@Override @Override
public boolean isConnected() { public boolean isConnected() {
if (displayConnectionMessages) if (displayConnectionMessages)
@ -412,25 +408,6 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return !isServiceSet() || !rileyLinkOmnipodService.isInitialized(); return !isServiceSet() || !rileyLinkOmnipodService.isInitialized();
} }
public void connect(String reason) {
if (displayConnectionMessages)
aapsLogger.debug(LTag.PUMP, "connect (reason={}) [PumpPluginAbstract] - default (empty) implementation." + reason);
}
public void disconnect(String reason) {
if (displayConnectionMessages)
aapsLogger.debug(LTag.PUMP, "disconnect (reason={}) [PumpPluginAbstract] - default (empty) implementation." + reason);
}
public void stopConnecting() {
if (displayConnectionMessages)
aapsLogger.debug(LTag.PUMP, "stopConnecting [PumpPluginAbstract] - default (empty) implementation.");
}
@Override @Override
public boolean isHandshakeInProgress() { public boolean isHandshakeInProgress() {
if (displayConnectionMessages) if (displayConnectionMessages)
@ -444,9 +421,19 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
aapsLogger.debug(LTag.PUMP, "finishHandshaking [OmnipodPumpPlugin] - default (empty) implementation."); aapsLogger.debug(LTag.PUMP, "finishHandshaking [OmnipodPumpPlugin] - default (empty) implementation.");
} }
@Override @Override public void connect(String reason) {
public boolean isSuspended() { if (displayConnectionMessages)
return !podStateManager.isPodRunning() || podStateManager.isSuspended(); aapsLogger.debug(LTag.PUMP, "connect (reason={}) [PumpPluginAbstract] - default (empty) implementation." + reason);
}
@Override public void disconnect(String reason) {
if (displayConnectionMessages)
aapsLogger.debug(LTag.PUMP, "disconnect (reason={}) [PumpPluginAbstract] - default (empty) implementation." + reason);
}
@Override public void stopConnecting() {
if (displayConnectionMessages)
aapsLogger.debug(LTag.PUMP, "stopConnecting [PumpPluginAbstract] - default (empty) implementation.");
} }
@Override @Override
@ -514,64 +501,43 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
} }
} }
public void setBusy(boolean busy) { @NotNull
this.busy = busy; @Override
} public PumpEnactResult setNewBasalProfile(Profile profile) {
aapsLogger.info(LTag.PUMP, getLogPrefix() + "setNewBasalProfile");
private void getPodPumpStatus() { // this shouldn't be needed, but let's do check if profile setting we are setting is same as current one
// TODO read pod status if (this.currentProfile != null && this.currentProfile.areProfileBasalPatternsSame(profile)) {
aapsLogger.error(LTag.PUMP, "getPodPumpStatus() NOT IMPLEMENTED"); return new PumpEnactResult(getInjector()) //
} .success(true) //
.enacted(false) //
public void addPodStatusRequest(OmnipodStatusRequest pumpStatusRequest) { .comment(resourceHelper.gs(R.string.omnipod_cmd_basal_profile_not_set_is_same));
if (pumpStatusRequest == OmnipodStatusRequest.ResetState) {
resetStatusState();
} else {
omnipodStatusRequestList.add(pumpStatusRequest);
} }
}
public void resetStatusState() {
firstRun = true;
isRefresh = true;
}
// FIXME do we actually need this? If a user presses refresh during an action,
// I suppose the GetStatusCommand would just be queued?
private void setRefreshButtonEnabled(boolean enabled) {
rxBus.send(new EventOmnipodRefreshButtonState(enabled));
}
private void initializePump(boolean realInit) {
aapsLogger.info(LTag.PUMP, getLogPrefix() + "initializePump - start");
// TODO ccc
//OmnipodPumpStatus podPumpStatus = getPodPumpStatusObject();
setRefreshButtonEnabled(false); setRefreshButtonEnabled(false);
if (podStateManager.isPodInitialized()) { OmnipodUITask responseTask = getDeviceCommandExecutor().executeCommand(OmnipodCommandType.SetBasalProfile,
aapsLogger.debug(LTag.PUMP, "PodStateManager (saved): " + podStateManager); profile);
// TODO handle if session state too old
getPodPumpStatus(); PumpEnactResult result = responseTask.getResult();
aapsLogger.info(LTag.PUMP, getLogPrefix() + "Basal Profile was set: " + result.success);
if (result.success) {
this.currentProfile = profile;
Notification notification = new Notification(Notification.PROFILE_SET_OK,
resourceHelper.gs(R.string.profile_set_ok),
Notification.INFO, 60);
rxBus.send(new EventNewNotification(notification));
} else { } else {
aapsLogger.debug(LTag.PUMP, "No Pod running"); Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE,
resourceHelper.gs(R.string.failedupdatebasalprofile),
Notification.URGENT);
rxBus.send(new EventNewNotification(notification));
} }
finishAction("Omnipod Pump"); return result;
if (!sentIdToFirebase) {
Bundle params = new Bundle();
params.putString("version", BuildConfig.VERSION);
fabricPrivacy.getFirebaseAnalytics().logEvent("OmnipodPumpInit", params);
sentIdToFirebase = true;
}
isInitialized = true;
this.firstRun = false;
} }
@Override @Override
@ -617,45 +583,38 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return 75; return 75;
} }
protected void triggerUIChange() { @NonNull @Override
rxBus.send(new EventOmnipodPumpValuesChanged()); public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
}
@Override
public boolean isFakingTempsByExtendedBoluses() {
return false;
}
@NotNull @Override public PumpEnactResult loadTDDs() {
aapsLogger.debug(LTag.PUMP, "loadTDDs [OmnipodPumpPlugin] - Not implemented.");
return getOperationNotSupportedWithCustomText(info.nightscout.androidaps.core.R.string.pump_operation_not_supported_by_pump_driver);
}
@NonNull
protected PumpEnactResult deliverBolus(final DetailedBolusInfo detailedBolusInfo) {
aapsLogger.info(LTag.PUMP, getLogPrefix() + "deliverBolus - {}", detailedBolusInfo);
setRefreshButtonEnabled(false);
try { try {
if (detailedBolusInfo.insulin == 0 && detailedBolusInfo.carbs == 0) {
// neither carbs nor bolus requested
aapsLogger.error("deliverTreatment: Invalid input");
return new PumpEnactResult(getInjector()).success(false).enacted(false).bolusDelivered(0d).carbsDelivered(0d)
.comment(getResourceHelper().gs(info.nightscout.androidaps.core.R.string.invalidinput));
} else if (detailedBolusInfo.insulin > 0) {
// bolus needed, ask pump to deliver it
return deliverBolus(detailedBolusInfo);
} else {
//if (MedtronicHistoryData.doubleBolusDebug)
// aapsLogger.debug("DoubleBolusDebug: deliverTreatment::(carb only entry)");
OmnipodUITask responseTask = getDeviceCommandExecutor().executeCommand(OmnipodCommandType.SetBolus, // no bolus required, carb only treatment
detailedBolusInfo); activePlugin.getActiveTreatments().addToHistoryTreatment(detailedBolusInfo, true);
PumpEnactResult result = responseTask.getResult(); EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE;
bolusingEvent.setT(new Treatment());
bolusingEvent.getT().isSMB = detailedBolusInfo.isSMB;
bolusingEvent.setPercent(100);
rxBus.send(bolusingEvent);
setRefreshButtonEnabled(true); aapsLogger.debug(LTag.PUMP, "deliverTreatment: Carb only treatment.");
if (result.success) { return new PumpEnactResult(getInjector()).success(true).enacted(true).bolusDelivered(0d)
incrementStatistics(detailedBolusInfo.isSMB ? OmnipodConst.Statistics.SMBBoluses .carbsDelivered(detailedBolusInfo.carbs).comment(getResourceHelper().gs(info.nightscout.androidaps.core.R.string.common_resultok));
: OmnipodConst.Statistics.StandardBoluses);
result.carbsDelivered(detailedBolusInfo.carbs);
} }
return result;
} finally { } finally {
finishAction("Bolus"); triggerUIChange();
} }
} }
@ -676,12 +635,6 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
//finishAction("Bolus"); //finishAction("Bolus");
} }
private void incrementStatistics(String statsKey) {
long currentCount = sp.getLong(statsKey, 0L);
currentCount++;
sp.putLong(statsKey, currentCount);
}
// if enforceNew===true current temp basal is canceled and new TBR set (duration is prolonged), // if enforceNew===true current temp basal is canceled and new TBR set (duration is prolonged),
// if false and the same rate is requested enacted=false and success=true is returned and TBR is not changed // if false and the same rate is requested enacted=false and success=true is returned and TBR is not changed
@Override @Override
@ -733,27 +686,6 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return getOperationNotSupportedWithCustomText(info.nightscout.androidaps.core.R.string.pump_operation_not_supported_by_pump_driver); return getOperationNotSupportedWithCustomText(info.nightscout.androidaps.core.R.string.pump_operation_not_supported_by_pump_driver);
} }
protected TempBasalPair readTBR() {
// TODO we can do it like this or read status from pod ??
if (omnipodPumpStatus.tempBasalEnd < System.currentTimeMillis()) {
// TBR done
omnipodPumpStatus.clearTemporaryBasal();
return null;
}
return omnipodPumpStatus.getTemporaryBasal();
}
protected void finishAction(String overviewKey) {
if (overviewKey != null)
rxBus.send(new EventRefreshOverview(overviewKey, false));
triggerUIChange();
setRefreshButtonEnabled(true);
}
@Override @Override
public PumpEnactResult cancelTempBasal(boolean enforceNew) { public PumpEnactResult cancelTempBasal(boolean enforceNew) {
@ -796,51 +728,6 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return getOperationNotSupportedWithCustomText(info.nightscout.androidaps.core.R.string.pump_operation_not_supported_by_pump_driver); return getOperationNotSupportedWithCustomText(info.nightscout.androidaps.core.R.string.pump_operation_not_supported_by_pump_driver);
} }
@NotNull
@Override
public String serialNumber() {
return podStateManager.hasPodState() ? String.valueOf(podStateManager.getAddress()) : "None";
}
@NotNull
@Override
public PumpEnactResult setNewBasalProfile(Profile profile) {
aapsLogger.info(LTag.PUMP, getLogPrefix() + "setNewBasalProfile");
// this shouldn't be needed, but let's do check if profile setting we are setting is same as current one
if (this.currentProfile != null && this.currentProfile.areProfileBasalPatternsSame(profile)) {
return new PumpEnactResult(getInjector()) //
.success(true) //
.enacted(false) //
.comment(resourceHelper.gs(R.string.omnipod_cmd_basal_profile_not_set_is_same));
}
setRefreshButtonEnabled(false);
OmnipodUITask responseTask = getDeviceCommandExecutor().executeCommand(OmnipodCommandType.SetBasalProfile,
profile);
PumpEnactResult result = responseTask.getResult();
aapsLogger.info(LTag.PUMP, getLogPrefix() + "Basal Profile was set: " + result.success);
if (result.success) {
this.currentProfile = profile;
Notification notification = new Notification(Notification.PROFILE_SET_OK,
resourceHelper.gs(R.string.profile_set_ok),
Notification.INFO, 60);
rxBus.send(new EventNewNotification(notification));
} else {
Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE,
resourceHelper.gs(R.string.failedupdatebasalprofile),
Notification.URGENT);
rxBus.send(new EventNewNotification(notification));
}
return result;
}
@NonNull @Override @NonNull @Override
public JSONObject getJSONStatus(Profile profile, String profileName, String version) { public JSONObject getJSONStatus(Profile profile, String profileName, String version) {
@ -889,6 +776,25 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return pump; return pump;
} }
@Override public ManufacturerType manufacturer() {
return pumpType.getManufacturer();
}
@Override @NotNull
public PumpType model() {
return pumpType;
}
@NotNull
@Override
public String serialNumber() {
return podStateManager.hasPodState() ? String.valueOf(podStateManager.getAddress()) : "None";
}
@Override @NonNull public PumpDescription getPumpDescription() {
return pumpDescription;
}
// FIXME i18n, null checks: iob, TDD // FIXME i18n, null checks: iob, TDD
@NonNull @Override @NonNull @Override
public String shortStatus(boolean veryShort) { public String shortStatus(boolean veryShort) {
@ -921,40 +827,18 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return ret; return ret;
} }
@Override
public boolean isFakingTempsByExtendedBoluses() {
return false;
}
@NonNull @Override @NotNull @Override public PumpEnactResult loadTDDs() {
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { aapsLogger.debug(LTag.PUMP, "loadTDDs [OmnipodPumpPlugin] - Not implemented.");
return getOperationNotSupportedWithCustomText(info.nightscout.androidaps.core.R.string.pump_operation_not_supported_by_pump_driver);
}
try { @Override public boolean canHandleDST() {
if (detailedBolusInfo.insulin == 0 && detailedBolusInfo.carbs == 0) { return false;
// neither carbs nor bolus requested
aapsLogger.error("deliverTreatment: Invalid input");
return new PumpEnactResult(getInjector()).success(false).enacted(false).bolusDelivered(0d).carbsDelivered(0d)
.comment(getResourceHelper().gs(info.nightscout.androidaps.core.R.string.invalidinput));
} else if (detailedBolusInfo.insulin > 0) {
// bolus needed, ask pump to deliver it
return deliverBolus(detailedBolusInfo);
} else {
//if (MedtronicHistoryData.doubleBolusDebug)
// aapsLogger.debug("DoubleBolusDebug: deliverTreatment::(carb only entry)");
// no bolus required, carb only treatment
activePlugin.getActiveTreatments().addToHistoryTreatment(detailedBolusInfo, true);
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE;
bolusingEvent.setT(new Treatment());
bolusingEvent.getT().isSMB = detailedBolusInfo.isSMB;
bolusingEvent.setPercent(100);
rxBus.send(bolusingEvent);
aapsLogger.debug(LTag.PUMP, "deliverTreatment: Carb only treatment.");
return new PumpEnactResult(getInjector()).success(true).enacted(true).bolusDelivered(0d)
.carbsDelivered(detailedBolusInfo.carbs).comment(getResourceHelper().gs(info.nightscout.androidaps.core.R.string.common_resultok));
}
} finally {
triggerUIChange();
}
} }
@Override @Override
@ -962,19 +846,6 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return customActions; return customActions;
} }
@Override public ManufacturerType manufacturer() {
return pumpType.getManufacturer();
}
@Override @NotNull
public PumpType model() {
return pumpType;
}
@Override public boolean canHandleDST() {
return false;
}
@Override @Override
public void executeCustomAction(CustomActionType customActionType) { public void executeCustomAction(CustomActionType customActionType) {
OmnipodCustomActionType mcat = (OmnipodCustomActionType) customActionType; OmnipodCustomActionType mcat = (OmnipodCustomActionType) customActionType;
@ -1022,6 +893,125 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface,
return false; return false;
} }
public OmnipodUIComm getDeviceCommandExecutor() {
return rileyLinkOmnipodService.getDeviceCommandExecutor();
}
private void getPodPumpStatus() {
// TODO read pod status
aapsLogger.error(LTag.PUMP, "getPodPumpStatus() NOT IMPLEMENTED");
}
@Override public void addPodStatusRequest(OmnipodStatusRequest pumpStatusRequest) {
if (pumpStatusRequest == OmnipodStatusRequest.ResetState) {
resetStatusState();
} else {
omnipodStatusRequestList.add(pumpStatusRequest);
}
}
public void resetStatusState() {
firstRun = true;
isRefresh = true;
}
// FIXME do we actually need this? If a user presses refresh during an action,
// I suppose the GetStatusCommand would just be queued?
private void setRefreshButtonEnabled(boolean enabled) {
rxBus.send(new EventOmnipodRefreshButtonState(enabled));
}
private void initializePump(boolean realInit) {
aapsLogger.info(LTag.PUMP, getLogPrefix() + "initializePump - start");
// TODO ccc
//OmnipodPumpStatus podPumpStatus = getPodPumpStatusObject();
setRefreshButtonEnabled(false);
if (podStateManager.isPodInitialized()) {
aapsLogger.debug(LTag.PUMP, "PodStateManager (saved): " + podStateManager);
// TODO handle if session state too old
getPodPumpStatus();
} else {
aapsLogger.debug(LTag.PUMP, "No Pod running");
}
finishAction("Omnipod Pump");
if (!sentIdToFirebase) {
Bundle params = new Bundle();
params.putString("version", BuildConfig.VERSION);
fabricPrivacy.getFirebaseAnalytics().logEvent("OmnipodPumpInit", params);
sentIdToFirebase = true;
}
isInitialized = true;
this.firstRun = false;
}
protected void triggerUIChange() {
rxBus.send(new EventOmnipodPumpValuesChanged());
}
@NonNull
protected PumpEnactResult deliverBolus(final DetailedBolusInfo detailedBolusInfo) {
aapsLogger.info(LTag.PUMP, getLogPrefix() + "deliverBolus - {}", detailedBolusInfo);
setRefreshButtonEnabled(false);
try {
OmnipodUITask responseTask = getDeviceCommandExecutor().executeCommand(OmnipodCommandType.SetBolus,
detailedBolusInfo);
PumpEnactResult result = responseTask.getResult();
setRefreshButtonEnabled(true);
if (result.success) {
incrementStatistics(detailedBolusInfo.isSMB ? OmnipodConst.Statistics.SMBBoluses
: OmnipodConst.Statistics.StandardBoluses);
result.carbsDelivered(detailedBolusInfo.carbs);
}
return result;
} finally {
finishAction("Bolus");
}
}
private void incrementStatistics(String statsKey) {
long currentCount = sp.getLong(statsKey, 0L);
currentCount++;
sp.putLong(statsKey, currentCount);
}
protected TempBasalPair readTBR() {
// TODO we can do it like this or read status from pod ??
if (omnipodPumpStatus.tempBasalEnd < System.currentTimeMillis()) {
// TBR done
omnipodPumpStatus.clearTemporaryBasal();
return null;
}
return omnipodPumpStatus.getTemporaryBasal();
}
protected void finishAction(String overviewKey) {
if (overviewKey != null)
rxBus.send(new EventRefreshOverview(overviewKey, false));
triggerUIChange();
setRefreshButtonEnabled(true);
}
public PumpType getPumpType() { public PumpType getPumpType() {
return pumpType; return pumpType;
} }