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.SetupProgress;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.BasalSchedule; 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.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.CommunicationException;
import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalSetupProgressException; import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalSetupProgressException;
import info.nightscout.androidaps.plugins.pump.omnipod.exception.NonceOutOfSyncException; 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); private static final Logger LOG = LoggerFactory.getLogger(L.PUMP);
protected final OmnipodCommunicationService communicationService; protected final OmnipodCommunicationService communicationService;
private final PodStateChangedHandler podStateChangedHandler;
protected PodSessionState podState; protected PodSessionState podState;
public OmnipodManager(OmnipodCommunicationService communicationService, PodSessionState podState) { public OmnipodManager(OmnipodCommunicationService communicationService, PodSessionState podState,
PodStateChangedHandler podStateChangedHandler) {
if (communicationService == null) { if (communicationService == null) {
throw new IllegalArgumentException("Communication service cannot be null"); throw new IllegalArgumentException("Communication service cannot be null");
} }
this.communicationService = communicationService; this.communicationService = communicationService;
this.podState = podState; 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. // After priming should have been finished, the pod state is verified.
// The result of that verification is passed to the SetupActionResultHandler // The result of that verification is passed to the SetupActionResultHandler
public void pairAndPrime(SetupActionResultHandler resultHandler) { public void pairAndPrime(SetupActionResultHandler resultHandler) {
if (podState == null) { 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)) { if (podState.getSetupProgress().isBefore(SetupProgress.PRIMING_FINISHED)) {
communicationService.executeAction(new PrimeAction(new PrimeService(), podState)); 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; SetupActionResult result = null;
for (int i = 0; ACTION_VERIFICATION_TRIES > i; i++) { for (int i = 0; ACTION_VERIFICATION_TRIES > i; i++) {
try { try {
StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState)); StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState));
statusResponseHandler.handle(delayedStatusResponse); setupActionResponseHandler.handle(delayedStatusResponse);
if (podState.getSetupProgress().equals(expectedSetupProgress)) { if (podState.getSetupProgress().equals(expectedSetupProgress)) {
result = new SetupActionResult(SetupActionResult.ResultType.SUCCESS); 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.SetupProgress;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; 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.PodSetupState;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodStateChangedHandler;
import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException;
public class PairAction implements OmnipodAction<PodSessionState> { public class PairAction implements OmnipodAction<PodSessionState> {
private final PairService service; private final PairService service;
private final int address; 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) { if (pairService == null) {
throw new ActionInitializationException("Pair service cannot be null"); throw new ActionInitializationException("Pair service cannot be null");
} }
this.service = pairService; this.service = pairService;
this.address = address; this.address = address;
this.podStateChangedHandler = podStateChangedHandler;
} }
public PairAction(PairService service) { public PairAction(PairService service, PodStateChangedHandler podStateChangedHandler) {
this(service, generateRandomAddress()); this(service, generateRandomAddress(), podStateChangedHandler);
} }
private static int generateRandomAddress() { private static int generateRandomAddress() {
@ -47,7 +50,7 @@ public class PairAction implements OmnipodAction<PodSessionState> {
PodSessionState podState = new PodSessionState(timeZone, address, activationDate, confirmPairingResponse.getPiVersion(), PodSessionState podState = new PodSessionState(timeZone, address, activationDate, confirmPairingResponse.getPiVersion(),
confirmPairingResponse.getPmVersion(), confirmPairingResponse.getLot(), confirmPairingResponse.getTid(), confirmPairingResponse.getPmVersion(), confirmPairingResponse.getLot(), confirmPairingResponse.getTid(),
setupState.getPacketNumber(), setupState.getMessageNumber()); setupState.getPacketNumber(), setupState.getMessageNumber(), podStateChangedHandler);
podState.setSetupProgress(SetupProgress.POD_CONFIGURED); podState.setSetupProgress(SetupProgress.POD_CONFIGURED);
return podState; return podState;

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.defs.state;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.joda.time.Duration; 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.NonceState;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.SetupProgress; 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.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.OmniCRC;
import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst;
import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodUtil; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodUtil;
@ -28,6 +26,7 @@ import info.nightscout.androidaps.utils.SP;
public class PodSessionState extends PodState { public class PodSessionState extends PodState {
private final Map<AlertSlot, AlertType> configuredAlerts; private final Map<AlertSlot, AlertType> configuredAlerts;
private final PodStateChangedHandler stateChangedHandler;
private DateTime activatedAt; private DateTime activatedAt;
private DateTime expiresAt; private DateTime expiresAt;
private final FirmwareVersion piVersion; private final FirmwareVersion piVersion;
@ -44,7 +43,8 @@ public class PodSessionState extends PodState {
private DeliveryStatus lastDeliveryStatus; private DeliveryStatus lastDeliveryStatus;
public PodSessionState(DateTimeZone timeZone, int address, DateTime activatedAt, FirmwareVersion piVersion, 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); super(address, messageNumber, packetNumber);
if (timeZone == null) { if (timeZone == null) {
throw new IllegalArgumentException("Time zone can not be null"); throw new IllegalArgumentException("Time zone can not be null");
@ -61,6 +61,7 @@ public class PodSessionState extends PodState {
this.pmVersion = pmVersion; this.pmVersion = pmVersion;
this.lot = lot; this.lot = lot;
this.tid = tid; this.tid = tid;
this.stateChangedHandler = stateChangedHandler;
this.nonceState = new NonceState(lot, tid); this.nonceState = new NonceState(lot, tid);
store(); store();
} }
@ -224,13 +225,22 @@ public class PodSessionState extends PodState {
private void store() { private void store() {
Gson gson = OmnipodUtil.getGsonInstance(); Gson gson = OmnipodUtil.getGsonInstance();
SP.putString(OmnipodConst.Prefs.PodState, gson.toJson(this)); SP.putString(OmnipodConst.Prefs.PodState, gson.toJson(this));
OmnipodUtil.setPodSessionState(this); if (stateChangedHandler != null) {
stateChangedHandler.handle(this);
}
} }
@Override @Override
public String toString() { public String toString() {
return "PodSessionState{" + return "PodSessionState{" +
"activatedAt=" + activatedAt + "address=" + address +
", packetNumber=" + packetNumber +
", messageNumber=" + messageNumber +
", faultEvent=" + faultEvent +
", configuredAlerts=" + configuredAlerts +
", stateChangedHandler=" + stateChangedHandler +
", activatedAt=" + activatedAt +
", expiresAt=" + expiresAt +
", piVersion=" + piVersion + ", piVersion=" + piVersion +
", pmVersion=" + pmVersion + ", pmVersion=" + pmVersion +
", lot=" + lot + ", lot=" + lot +
@ -239,14 +249,9 @@ public class PodSessionState extends PodState {
", timeZone=" + timeZone + ", timeZone=" + timeZone +
", nonceState=" + nonceState + ", nonceState=" + nonceState +
", setupProgress=" + setupProgress + ", setupProgress=" + setupProgress +
", configuredAlerts=" + configuredAlerts +
", activeAlerts=" + activeAlerts + ", activeAlerts=" + activeAlerts +
", basalSchedule=" + basalSchedule + ", basalSchedule=" + basalSchedule +
", lastDeliveryStatus=" + lastDeliveryStatus + ", 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) { public AapsOmnipodManager(OmnipodCommunicationService communicationService, PodSessionState podState, OmnipodPumpStatus pumpStatus) {
delegate = new OmnipodManager(communicationService, podState); delegate = new OmnipodManager(communicationService, podState, OmnipodUtil::setPodSessionState);
this.pumpStatus = pumpStatus; this.pumpStatus = pumpStatus;
instance = this; instance = this;
} }