Add PodStateChangedHandler

This commit is contained in:
Bart Sopers 2019-12-01 01:11:28 +01:00
parent 40487ffb06
commit 932533ace7
5 changed files with 45 additions and 22 deletions

View file

@ -38,6 +38,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodInfoType;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.SetupProgress;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.BasalSchedule;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodStateChangedHandler;
import info.nightscout.androidaps.plugins.pump.omnipod.exception.CommunicationException;
import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalSetupProgressException;
import info.nightscout.androidaps.plugins.pump.omnipod.exception.NonceOutOfSyncException;
@ -51,21 +52,29 @@ public class OmnipodManager {
private static final Logger LOG = LoggerFactory.getLogger(L.PUMP);
protected final OmnipodCommunicationService communicationService;
private final PodStateChangedHandler podStateChangedHandler;
protected PodSessionState podState;
public OmnipodManager(OmnipodCommunicationService communicationService, PodSessionState podState) {
public OmnipodManager(OmnipodCommunicationService communicationService, PodSessionState podState,
PodStateChangedHandler podStateChangedHandler) {
if (communicationService == null) {
throw new IllegalArgumentException("Communication service cannot be null");
}
this.communicationService = communicationService;
this.podState = podState;
this.podStateChangedHandler = podStateChangedHandler;
}
public OmnipodManager(OmnipodCommunicationService communicationService, PodSessionState podState) {
this(communicationService, podState, null);
}
// After priming should have been finished, the pod state is verified.
// The result of that verification is passed to the SetupActionResultHandler
public void pairAndPrime(SetupActionResultHandler resultHandler) {
if (podState == null) {
podState = communicationService.executeAction(new PairAction(new PairService()));
podState = communicationService.executeAction(
new PairAction(new PairService(), podStateChangedHandler));
}
if (podState.getSetupProgress().isBefore(SetupProgress.PRIMING_FINISHED)) {
communicationService.executeAction(new PrimeAction(new PrimeService(), podState));
@ -261,12 +270,12 @@ public class OmnipodManager {
}
}
private void verifySetupAction(StatusResponseHandler statusResponseHandler, SetupProgress expectedSetupProgress, SetupActionResultHandler resultHandler) {
private void verifySetupAction(StatusResponseHandler setupActionResponseHandler, SetupProgress expectedSetupProgress, SetupActionResultHandler resultHandler) {
SetupActionResult result = null;
for (int i = 0; ACTION_VERIFICATION_TRIES > i; i++) {
try {
StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState));
statusResponseHandler.handle(delayedStatusResponse);
setupActionResponseHandler.handle(delayedStatusResponse);
if (podState.getSetupProgress().equals(expectedSetupProgress)) {
result = new SetupActionResult(SetupActionResult.ResultType.SUCCESS);

View file

@ -11,22 +11,25 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.Ver
import info.nightscout.androidaps.plugins.pump.omnipod.defs.SetupProgress;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSetupState;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodStateChangedHandler;
import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException;
public class PairAction implements OmnipodAction<PodSessionState> {
private final PairService service;
private final int address;
private final PodStateChangedHandler podStateChangedHandler;
public PairAction(PairService pairService, int address) {
public PairAction(PairService pairService, int address, PodStateChangedHandler podStateChangedHandler) {
if (pairService == null) {
throw new ActionInitializationException("Pair service cannot be null");
}
this.service = pairService;
this.address = address;
this.podStateChangedHandler = podStateChangedHandler;
}
public PairAction(PairService service) {
this(service, generateRandomAddress());
public PairAction(PairService service, PodStateChangedHandler podStateChangedHandler) {
this(service, generateRandomAddress(), podStateChangedHandler);
}
private static int generateRandomAddress() {
@ -47,7 +50,7 @@ public class PairAction implements OmnipodAction<PodSessionState> {
PodSessionState podState = new PodSessionState(timeZone, address, activationDate, confirmPairingResponse.getPiVersion(),
confirmPairingResponse.getPmVersion(), confirmPairingResponse.getLot(), confirmPairingResponse.getTid(),
setupState.getPacketNumber(), setupState.getMessageNumber());
setupState.getPacketNumber(), setupState.getMessageNumber(), podStateChangedHandler);
podState.setSetupProgress(SetupProgress.POD_CONFIGURED);
return podState;

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.defs.state;
import com.google.gson.Gson;
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
import org.joda.time.DateTimeZone;
import org.joda.time.Duration;
@ -19,7 +18,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.defs.FirmwareVersion;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.NonceState;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.SetupProgress;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.BasalSchedule;
import info.nightscout.androidaps.plugins.pump.omnipod.events.EventOmnipodDeviceStatusChange;
import info.nightscout.androidaps.plugins.pump.omnipod.util.OmniCRC;
import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst;
import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodUtil;
@ -28,6 +26,7 @@ import info.nightscout.androidaps.utils.SP;
public class PodSessionState extends PodState {
private final Map<AlertSlot, AlertType> configuredAlerts;
private final PodStateChangedHandler stateChangedHandler;
private DateTime activatedAt;
private DateTime expiresAt;
private final FirmwareVersion piVersion;
@ -44,7 +43,8 @@ public class PodSessionState extends PodState {
private DeliveryStatus lastDeliveryStatus;
public PodSessionState(DateTimeZone timeZone, int address, DateTime activatedAt, FirmwareVersion piVersion,
FirmwareVersion pmVersion, int lot, int tid, int packetNumber, int messageNumber) {
FirmwareVersion pmVersion, int lot, int tid, int packetNumber, int messageNumber,
PodStateChangedHandler stateChangedHandler) {
super(address, messageNumber, packetNumber);
if (timeZone == null) {
throw new IllegalArgumentException("Time zone can not be null");
@ -61,6 +61,7 @@ public class PodSessionState extends PodState {
this.pmVersion = pmVersion;
this.lot = lot;
this.tid = tid;
this.stateChangedHandler = stateChangedHandler;
this.nonceState = new NonceState(lot, tid);
store();
}
@ -207,11 +208,11 @@ public class PodSessionState extends PodState {
@Override
public void updateFromStatusResponse(StatusResponse statusResponse) {
DateTime activatedAtCalculated = getTime().minus(statusResponse.getTimeActive());
if(activatedAt == null) {
if (activatedAt == null) {
activatedAt = activatedAtCalculated;
}
DateTime expiresAtCalculated = activatedAtCalculated.plus(OmnipodConst.NOMINAL_POD_LIFE);
if(expiresAt == null || expiresAtCalculated.isBefore(expiresAt) || expiresAtCalculated.isAfter(expiresAt.plusMinutes(1))) {
if (expiresAt == null || expiresAtCalculated.isBefore(expiresAt) || expiresAtCalculated.isAfter(expiresAt.plusMinutes(1))) {
expiresAt = expiresAtCalculated;
}
@ -224,13 +225,22 @@ public class PodSessionState extends PodState {
private void store() {
Gson gson = OmnipodUtil.getGsonInstance();
SP.putString(OmnipodConst.Prefs.PodState, gson.toJson(this));
OmnipodUtil.setPodSessionState(this);
if (stateChangedHandler != null) {
stateChangedHandler.handle(this);
}
}
@Override
public String toString() {
return "PodSessionState{" +
"activatedAt=" + activatedAt +
"address=" + address +
", packetNumber=" + packetNumber +
", messageNumber=" + messageNumber +
", faultEvent=" + faultEvent +
", configuredAlerts=" + configuredAlerts +
", stateChangedHandler=" + stateChangedHandler +
", activatedAt=" + activatedAt +
", expiresAt=" + expiresAt +
", piVersion=" + piVersion +
", pmVersion=" + pmVersion +
", lot=" + lot +
@ -239,14 +249,9 @@ public class PodSessionState extends PodState {
", timeZone=" + timeZone +
", nonceState=" + nonceState +
", setupProgress=" + setupProgress +
", configuredAlerts=" + configuredAlerts +
", activeAlerts=" + activeAlerts +
", basalSchedule=" + basalSchedule +
", lastDeliveryStatus=" + lastDeliveryStatus +
", address=" + address +
", packetNumber=" + packetNumber +
", messageNumber=" + messageNumber +
", faultEvent=" + faultEvent +
'}';
}
}

View file

@ -0,0 +1,6 @@
package info.nightscout.androidaps.plugins.pump.omnipod.defs.state;
@FunctionalInterface
public interface PodStateChangedHandler {
void handle(PodSessionState podState);
}

View file

@ -60,7 +60,7 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface
}
public AapsOmnipodManager(OmnipodCommunicationService communicationService, PodSessionState podState, OmnipodPumpStatus pumpStatus) {
delegate = new OmnipodManager(communicationService, podState);
delegate = new OmnipodManager(communicationService, podState, OmnipodUtil::setPodSessionState);
this.pumpStatus = pumpStatus;
instance = this;
}
@ -160,7 +160,7 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface
});
} catch (Exception ex) {
String comment = handleAndTranslateException(ex);
if(OmnipodManager.isCertainFailure(ex)) {
if (OmnipodManager.isCertainFailure(ex)) {
return new PumpEnactResult().success(false).enacted(false).comment(comment);
} else {
// TODO notify user about uncertain failure