EROS: uses static Random instance

This commit is contained in:
Milos Kozak 2023-02-14 11:22:14 +01:00
parent 21b3a70ad9
commit 91590f04ce
2 changed files with 63 additions and 76 deletions

View file

@ -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<Void> {
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);
}
}

View file

@ -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<Any> {
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<MessageBlock>(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)
}
}