From 79d64dff6456a38e7780b57a7c40a314fc90eb71 Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Sun, 3 May 2020 13:31:19 +0200 Subject: [PATCH] Use static Pod address in between pairing retries and remove unused methods from OmnipodUtil --- .../pump/omnipod/comm/OmnipodManager.java | 4 + .../omnipod/dialogs/PodManagementActivity.kt | 2 +- .../driver/comm/AapsOmnipodManager.java | 23 ++++-- .../pump/omnipod/util/OmnipodConst.java | 1 + .../pump/omnipod/util/OmnipodUtil.java | 77 ++++++------------- 5 files changed, 45 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java index 0c30d2f6c4..16962dd567 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java @@ -597,6 +597,10 @@ public class OmnipodManager { return 0x1f000000 | (new Random().nextInt() & 0x000fffff); } + public static boolean isValidAddress(int address) { + return (0x1f000000 | (address & 0x000fffff)) == address; + } + public static class BolusCommandResult { private final CommandDeliveryStatus commandDeliveryStatus; private final SingleSubject deliveryResultSubject; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dialogs/PodManagementActivity.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dialogs/PodManagementActivity.kt index 12a08605c3..47c9a36ca5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dialogs/PodManagementActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dialogs/PodManagementActivity.kt @@ -144,7 +144,7 @@ class PodManagementActivity : NoSplashAppCompatActivity() { val isPodSessionActive = (OmnipodUtil.getPodSessionState() != null) initpod_remove_pod.isEnabled = isPodSessionActive - initpod_reset_pod.isEnabled = isPodSessionActive + initpod_reset_pod.isEnabled = isPodSessionActive || OmnipodUtil.hasNextPodAddress() if (OmnipodUtil.getDriverState() == OmnipodDriverState.NotInitalized) { // if rileylink is not running we disable all operations diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java index 354ffc3f48..7d7337dba6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java @@ -84,6 +84,7 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface private static AapsOmnipodManager instance; private OmnipodPumpStatus pumpStatus; + private Integer nextPodAddress; private Date lastBolusTime; private Double lastBolusUnits; @@ -98,6 +99,7 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface OmnipodUtil.setPodSessionState(podSessionState); updatePumpStatus(podSessionState); }); + this.nextPodAddress = OmnipodUtil.getNextPodAddress(); this.pumpStatus = _pumpStatus; instance = this; } @@ -171,8 +173,7 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface long time = System.currentTimeMillis(); if (PodInitActionType.PairAndPrimeWizardStep.equals(podInitActionType)) { try { - // BS FIXME use static address for retries - int address = OmnipodManager.generateRandomAddress(); + int address = obtainNextPodAddress(); Disposable disposable = delegate.pairAndPrime(address).subscribe(res -> // handleSetupActionResult(podInitActionType, podInitReceiver, res, time, null)); @@ -279,6 +280,7 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface reportImplicitlyCanceledTbr(); OmnipodUtil.setPodSessionState(null); + OmnipodUtil.removeNextPodAddress(); addSuccessToHistory(System.currentTimeMillis(), PodHistoryEntryType.ResetPodState, null); @@ -544,18 +546,15 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface } } - - public long addSuccessToHistory(long requestTime, PodHistoryEntryType entryType, Object data) { + private long addSuccessToHistory(long requestTime, PodHistoryEntryType entryType, Object data) { return addToHistory(requestTime, entryType, data, true); } - public long addFailureToHistory(long requestTime, PodHistoryEntryType entryType, Object data) { + private long addFailureToHistory(long requestTime, PodHistoryEntryType entryType, Object data) { return addToHistory(requestTime, entryType, data, false); } - - public long addToHistory(long requestTime, PodHistoryEntryType entryType, Object data, boolean success) { - + private long addToHistory(long requestTime, PodHistoryEntryType entryType, Object data, boolean success) { PodHistory podHistory = new PodHistory(requestTime, entryType); if (data != null) { @@ -572,7 +571,15 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface MainApp.getDbHelper().createOrUpdate(podHistory); return podHistory.getPumpId(); + } + private int obtainNextPodAddress() { + if(nextPodAddress == null) { + nextPodAddress = OmnipodManager.generateRandomAddress(); + OmnipodUtil.setNextPodAddress(nextPodAddress); + } + + return nextPodAddress; } private void handleSetupActionResult(PodInitActionType podInitActionType, PodInitReceiver podInitReceiver, SetupActionResult res, long time, Profile profile) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java index f7b78250c3..d12fbd31df 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java @@ -14,6 +14,7 @@ public class OmnipodConst { public class Prefs { public static final String PodState = Prefix + "pod_state"; + public static final String NextPodAddress = Prefix + "next_pod_address"; public static final int BeepBasalEnabled = R.string.key_omnipod_beep_basal_enabled; public static final int BeepBolusEnabled = R.string.key_omnipod_beep_bolus_enabled; public static final int BeepSMBEnabled = R.string.key_omnipod_beep_smb_enabled; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodUtil.java index a7035ac4e5..96f5e39861 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodUtil.java @@ -26,6 +26,7 @@ import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.data.RLHistoryItem; import info.nightscout.androidaps.plugins.pump.omnipod.OmnipodPumpPlugin; +import info.nightscout.androidaps.plugins.pump.omnipod.comm.OmnipodManager; import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodCommandType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodCommunicationManagerInterface; import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodPodType; @@ -38,6 +39,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.events.EventOmnipodDevice import info.nightscout.androidaps.plugins.pump.omnipod.service.RileyLinkOmnipodService; import info.nightscout.androidaps.plugins.pump.omnipod_dash.OmnipodDashPumpPlugin; import info.nightscout.androidaps.utils.OKDialog; +import info.nightscout.androidaps.utils.SP; /** * Created by andy on 4/8/19. @@ -47,7 +49,6 @@ public class OmnipodUtil extends RileyLinkUtil { private static final Logger LOG = LoggerFactory.getLogger(L.PUMPCOMM); - private static boolean lowLevelDebug = true; private static RileyLinkOmnipodService omnipodService; private static OmnipodPumpStatus omnipodPumpStatus; private static OmnipodCommandType currentCommand; @@ -63,61 +64,14 @@ public class OmnipodUtil extends RileyLinkUtil { return gsonInstance; } - public static int makeUnsignedShort(int b2, int b1) { - int k = (b2 & 0xff) << 8 | b1 & 0xff; - return k; - } - - public static byte[] getByteArrayFromUnsignedShort(int shortValue, boolean returnFixedSize) { - byte highByte = (byte) (shortValue >> 8 & 0xFF); - byte lowByte = (byte) (shortValue & 0xFF); - - if (highByte > 0) { - return createByteArray(highByte, lowByte); - } else { - return returnFixedSize ? createByteArray(highByte, lowByte) : createByteArray(lowByte); - } - - } - - - public static byte[] createByteArray(byte... data) { - return data; - } - - - public static byte[] createByteArray(List data) { - - byte[] array = new byte[data.size()]; - - for (int i = 0; i < data.size(); i++) { - array[i] = data.get(i); - } - - return array; - } - - - public static boolean isLowLevelDebug() { - return lowLevelDebug; - } - - - public static void setLowLevelDebug(boolean lowLevelDebug) { - OmnipodUtil.lowLevelDebug = lowLevelDebug; - } - - public static OmnipodCommunicationManagerInterface getOmnipodCommunicationManager() { return (OmnipodCommunicationManagerInterface) RileyLinkUtil.rileyLinkCommunicationManager; } - public static RileyLinkOmnipodService getOmnipodService() { return OmnipodUtil.omnipodService; } - public static void setOmnipodService(RileyLinkOmnipodService medtronicService) { OmnipodUtil.omnipodService = medtronicService; } @@ -126,24 +80,20 @@ public class OmnipodUtil extends RileyLinkUtil { return OmnipodUtil.currentCommand; } - // FIXME public static void setCurrentCommand(OmnipodCommandType currentCommand) { OmnipodUtil.currentCommand = currentCommand; if (currentCommand != null) historyRileyLink.add(new RLHistoryItem(currentCommand)); - } - public static boolean isSame(Double d1, Double d2) { double diff = d1 - d2; return (Math.abs(diff) <= 0.000001); } - public static void displayNotConfiguredDialog(Context context) { OKDialog.show(context, MainApp.gs(R.string.combo_warning), MainApp.gs(R.string.omnipod_error_operation_not_possible_no_configuration), null); @@ -239,7 +189,6 @@ public class OmnipodUtil extends RileyLinkUtil { return OmnipodDashPumpPlugin.getPlugin().isEnabled(PluginType.PUMP); } - public static void setPumpType(PumpType pumpType) { OmnipodUtil.pumpType = pumpType; } @@ -247,4 +196,26 @@ public class OmnipodUtil extends RileyLinkUtil { public static PumpType getPumpType() { return pumpType; } + + public static Integer getNextPodAddress() { + if(SP.contains(OmnipodConst.Prefs.NextPodAddress)) { + int nextPodAddress = SP.getInt(OmnipodConst.Prefs.NextPodAddress, 0); + if (OmnipodManager.isValidAddress(nextPodAddress)) { + return nextPodAddress; + } + } + return null; + } + + public static boolean hasNextPodAddress() { + return getNextPodAddress() != null; + } + + public static void setNextPodAddress(int address) { + SP.putInt(OmnipodConst.Prefs.NextPodAddress, address); + } + + public static void removeNextPodAddress() { + SP.remove(OmnipodConst.Prefs.NextPodAddress); + } }