From 91590f04ceeafb5c9bce021b0223df49b9d8a569 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 14 Feb 2023 11:22:14 +0100 Subject: [PATCH] EROS: uses static Random instance --- .../action/AssignAddressAction.java | 76 ------------------- .../action/AssignAddressAction.kt | 63 +++++++++++++++ 2 files changed, 63 insertions(+), 76 deletions(-) delete mode 100644 pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.java create mode 100644 pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.kt diff --git a/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.java b/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.java deleted file mode 100644 index 4ac4a473bf..0000000000 --- a/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.java +++ /dev/null @@ -1,76 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.communication.action; - -import org.joda.time.DateTimeZone; - -import java.util.Collections; -import java.util.Random; - -import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.communication.message.OmnipodMessage; -import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.communication.message.command.AssignAddressCommand; -import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.communication.message.response.VersionResponse; -import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.definition.OmnipodConstants; -import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.definition.PacketType; -import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.exception.IllegalMessageAddressException; -import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.exception.IllegalPacketTypeException; -import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.exception.IllegalVersionResponseTypeException; -import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.manager.ErosPodStateManager; -import info.nightscout.androidaps.plugins.pump.omnipod.eros.rileylink.manager.OmnipodRileyLinkCommunicationManager; -import info.nightscout.rx.logging.AAPSLogger; - -public class AssignAddressAction implements OmnipodAction { - private final ErosPodStateManager podStateManager; - private final AAPSLogger aapsLogger; - - public AssignAddressAction(ErosPodStateManager podStateManager, AAPSLogger aapsLogger) { - if (podStateManager == null) { - throw new IllegalArgumentException("podStateManager can not be null"); - } - if (aapsLogger == null) { - throw new IllegalArgumentException("Logger can not be null"); - } - this.podStateManager = podStateManager; - this.aapsLogger = aapsLogger; - } - - @Override - public Void execute(OmnipodRileyLinkCommunicationManager communicationService) { - if (!podStateManager.hasPodState()) { - podStateManager.initState(generateRandomAddress()); - } - - if (podStateManager.getActivationProgress().needsPairing()) { - AssignAddressCommand assignAddress = new AssignAddressCommand(podStateManager.getAddress()); - OmnipodMessage assignAddressMessage = new OmnipodMessage(OmnipodConstants.DEFAULT_ADDRESS, - Collections.singletonList(assignAddress), podStateManager.getMessageNumber()); - - try { - VersionResponse assignAddressResponse = communicationService.exchangeMessages(VersionResponse.class, podStateManager, assignAddressMessage, - OmnipodConstants.DEFAULT_ADDRESS, podStateManager.getAddress()); - - if (!assignAddressResponse.isAssignAddressVersionResponse()) { - throw new IllegalVersionResponseTypeException("assignAddress", "setupPod"); - } - if (assignAddressResponse.getAddress() != podStateManager.getAddress()) { - throw new IllegalMessageAddressException(podStateManager.getAddress(), assignAddressResponse.getAddress()); - } - - podStateManager.setInitializationParameters(assignAddressResponse.getLot(), assignAddressResponse.getTid(), // - assignAddressResponse.getPiVersion(), assignAddressResponse.getPmVersion(), DateTimeZone.getDefault(), assignAddressResponse.getPodProgressStatus()); - } catch (IllegalPacketTypeException ex) { - if (ex.getActual() == PacketType.ACK && podStateManager.isPodInitialized()) { - // When we already assigned the address before, it's possible to only get an ACK here - aapsLogger.debug("Received ACK instead of response in AssignAddressAction. Ignoring because we already assigned the address successfully"); - } else { - throw ex; - } - } - } - - return null; - } - - private static int generateRandomAddress() { - // Create random address with 20 bits to match PDM, could easily use 24 bits instead - return 0x1f000000 | (new Random().nextInt() & 0x000fffff); - } -} diff --git a/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.kt b/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.kt new file mode 100644 index 0000000000..46228e37b0 --- /dev/null +++ b/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.kt @@ -0,0 +1,63 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.communication.action + +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.communication.message.MessageBlock +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.communication.message.OmnipodMessage +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.communication.message.command.AssignAddressCommand +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.communication.message.response.VersionResponse +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.definition.OmnipodConstants +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.definition.PacketType +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.exception.IllegalMessageAddressException +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.exception.IllegalPacketTypeException +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.exception.IllegalVersionResponseTypeException +import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.manager.ErosPodStateManager +import info.nightscout.androidaps.plugins.pump.omnipod.eros.rileylink.manager.OmnipodRileyLinkCommunicationManager +import info.nightscout.rx.logging.AAPSLogger +import org.joda.time.DateTimeZone +import java.util.Random + +class AssignAddressAction( + private val podStateManager: ErosPodStateManager, + private val aapsLogger: AAPSLogger +) : OmnipodAction { + + override fun execute(communicationService: OmnipodRileyLinkCommunicationManager) { + if (!podStateManager.hasPodState()) { + podStateManager.initState(generateRandomAddress()) + } + if (podStateManager.activationProgress.needsPairing()) { + val assignAddress = AssignAddressCommand(podStateManager.address) + val assignAddressMessage = OmnipodMessage(OmnipodConstants.DEFAULT_ADDRESS, listOf(assignAddress), podStateManager.messageNumber) + try { + val assignAddressResponse = communicationService.exchangeMessages( + VersionResponse::class.java, podStateManager, assignAddressMessage, + OmnipodConstants.DEFAULT_ADDRESS, podStateManager.address + ) + if (!assignAddressResponse.isAssignAddressVersionResponse) { + throw IllegalVersionResponseTypeException("assignAddress", "setupPod") + } + if (assignAddressResponse.address != podStateManager.address) { + throw IllegalMessageAddressException(podStateManager.address, assignAddressResponse.address) + } + podStateManager.setInitializationParameters( + assignAddressResponse.lot, assignAddressResponse.tid, // + assignAddressResponse.piVersion, assignAddressResponse.pmVersion, DateTimeZone.getDefault(), assignAddressResponse.podProgressStatus + ) + } catch (ex: IllegalPacketTypeException) { + if (ex.actual == PacketType.ACK && podStateManager.isPodInitialized) { + // When we already assigned the address before, it's possible to only get an ACK here + aapsLogger.debug("Received ACK instead of response in AssignAddressAction. Ignoring because we already assigned the address successfully") + } else { + throw ex + } + } + } + } + + companion object { + + private val random: Random = Random() + private fun generateRandomAddress(): Int = + // Create random address with 20 bits to match PDM, could easily use 24 bits instead + 0x1f000000 or (random.nextInt() and 0x000fffff) + } +} \ No newline at end of file