From c183a687988d8a9f0751daccd29f821a3406e395 Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Sun, 14 Feb 2021 21:12:22 +0100 Subject: [PATCH] WIP on Omnipod Dash set basal and some refactoring --- .../driver/pod/command/DeactivateCommand.java | 3 +- .../driver/pod/command/GetVersionCommand.java | 3 +- .../pod/command/ProgramAlertsCommand.java | 3 +- .../pod/command/ProgramBasalCommand.java | 118 ++++++++++++++++-- .../pod/command/ProgramInsulinCommand.java | 97 ++++++++++---- .../pod/command/SetUniqueIdCommand.java | 3 +- .../pod/command/SilenceAlertsCommand.java | 3 +- .../pod/command/StopDeliveryCommand.java | 4 +- .../command/base/HeaderEnabledCommand.java | 36 ------ .../pod/command/base/NonceEnabledCommand.java | 15 --- .../command/base/builder/CommandBuilder.java | 7 ++ .../builder/HeaderEnabledCommandBuilder.java | 36 ++++++ .../builder/NonceEnabledCommandBuilder.java | 19 +++ .../program/InsulinProgramElement.java | 28 ----- 14 files changed, 256 insertions(+), 119 deletions(-) create mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/CommandBuilder.java create mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/HeaderEnabledCommandBuilder.java create mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/NonceEnabledCommandBuilder.java delete mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/program/InsulinProgramElement.java diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/DeactivateCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/DeactivateCommand.java index ff7571bc6d..7f6fee2ce6 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/DeactivateCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/DeactivateCommand.java @@ -4,6 +4,7 @@ import java.nio.ByteBuffer; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.CommandType; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.NonceEnabledCommand; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder.NonceEnabledCommandBuilder; public final class DeactivateCommand extends NonceEnabledCommand { private static final short LENGTH = 6; @@ -31,7 +32,7 @@ public final class DeactivateCommand extends NonceEnabledCommand { '}'; } - public static final class Builder extends NonceEnabledBuilder { + public static final class Builder extends NonceEnabledCommandBuilder { @Override protected final DeactivateCommand buildCommand() { return new DeactivateCommand(Builder.this.address, sequenceNumber, multiCommandFlag, nonce); } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/GetVersionCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/GetVersionCommand.java index 0850c51059..6dc9f65f1e 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/GetVersionCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/GetVersionCommand.java @@ -4,6 +4,7 @@ import java.nio.ByteBuffer; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.CommandType; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.HeaderEnabledCommand; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder.HeaderEnabledCommandBuilder; public final class GetVersionCommand extends HeaderEnabledCommand { public static final int DEFAULT_ADDRESS = -1; // FIXME move @@ -33,7 +34,7 @@ public final class GetVersionCommand extends HeaderEnabledCommand { '}'; } - public static final class Builder extends HeaderEnabledBuilder { + public static final class Builder extends HeaderEnabledCommandBuilder { @Override protected final GetVersionCommand buildCommand() { return new GetVersionCommand(address, sequenceNumber, multiCommandFlag); } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramAlertsCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramAlertsCommand.java index 46d6695e92..ec46411954 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramAlertsCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramAlertsCommand.java @@ -6,6 +6,7 @@ import java.util.List; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.CommandType; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.NonceEnabledCommand; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder.NonceEnabledCommandBuilder; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.AlertConfiguration; public final class ProgramAlertsCommand extends NonceEnabledCommand { @@ -46,7 +47,7 @@ public final class ProgramAlertsCommand extends NonceEnabledCommand { '}'; } - public static final class Builder extends NonceEnabledBuilder { + public static final class Builder extends NonceEnabledCommandBuilder { private List alertConfigurations; public Builder setAlertConfigurations(List alertConfigurations) { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramBasalCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramBasalCommand.java index 95601577c0..5060ffb138 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramBasalCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramBasalCommand.java @@ -6,33 +6,131 @@ import java.util.List; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.CommandType; -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.program.InsulinProgramElement; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder.CommandBuilder; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.Encodable; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.ProgramReminder; // Always preceded by 0x1a ProgramInsulinCommand public final class ProgramBasalCommand implements Command { - private final List uniqueInsulinProgramElements; + private final List insulinProgramElements; + private final ProgramReminder programReminder; + private final byte currentInsulinProgramElementIndex; + private final short remainingTenthPulsesInCurrentInsulinProgramElement; + private final int delayUntilNextTenthPulseInUsec; - private ProgramBasalCommand(List uniqueInsulinProgramElements) { - this.uniqueInsulinProgramElements = new ArrayList<>(uniqueInsulinProgramElements); + private ProgramBasalCommand(List insulinProgramElements, ProgramReminder programReminder, byte currentInsulinProgramElementIndex, short remainingTenthPulsesInCurrentInsulinProgramElement, int delayUntilNextTenthPulseInUsec) { + this.insulinProgramElements = new ArrayList<>(insulinProgramElements); + this.programReminder = programReminder; + this.currentInsulinProgramElementIndex = currentInsulinProgramElementIndex; + this.remainingTenthPulsesInCurrentInsulinProgramElement = remainingTenthPulsesInCurrentInsulinProgramElement; + this.delayUntilNextTenthPulseInUsec = delayUntilNextTenthPulseInUsec; } public short getLength() { - return (short) (uniqueInsulinProgramElements.size() * 2 + 14); + return (short) (insulinProgramElements.size() * 2 + 14); } public byte getBodyLength() { - return (byte) (uniqueInsulinProgramElements.size() * 2 + 12); + return (byte) (insulinProgramElements.size() * 2 + 12); } @Override public byte[] getEncoded() { - return ByteBuffer.allocate(getLength()) // - // TODO - .array(); + ByteBuffer buffer = ByteBuffer.allocate(getLength()) // + .put(getCommandType().getValue()) // + .put(getBodyLength()) // + .put(programReminder.getEncoded()) // + .put(currentInsulinProgramElementIndex) // + .putShort(remainingTenthPulsesInCurrentInsulinProgramElement) // + .putInt(delayUntilNextTenthPulseInUsec); + for (InsulinProgramElement insulinProgramElement : insulinProgramElements) { + buffer.put(insulinProgramElement.getEncoded()); + } + return buffer.array(); } @Override public CommandType getCommandType() { return CommandType.PROGRAM_BASAL; } - // TODO builder + @Override public String toString() { + return "ProgramBasalCommand{" + + "uniqueInsulinProgramElements=" + insulinProgramElements + + ", programReminder=" + programReminder + + ", currentInsulinProgramElementIndex=" + currentInsulinProgramElementIndex + + ", remainingTenthPulsesInCurrentInsulinProgramElement=" + remainingTenthPulsesInCurrentInsulinProgramElement + + ", delayUntilNextTenthPulseInUsec=" + delayUntilNextTenthPulseInUsec + + '}'; + } + + public static class InsulinProgramElement implements Encodable { + private final short totalTenthPulses; + private final int delayBetweenTenthPulses; + + public InsulinProgramElement(byte totalTenthPulses, short delayBetweenTenthPulses) { + this.totalTenthPulses = totalTenthPulses; + this.delayBetweenTenthPulses = delayBetweenTenthPulses; + } + + @Override public byte[] getEncoded() { + return ByteBuffer.allocate(6) // + .putShort(totalTenthPulses) // + .putInt(delayBetweenTenthPulses) // + .array(); + } + } + + public static final class Builder implements CommandBuilder { + private List insulinProgramElements; + private ProgramReminder programReminder; + private Byte currentInsulinProgramElementIndex; + private Short remainingTenthPulsesInCurrentInsulinProgramElement; + private Integer delayUntilNextTenthPulseInUsec; + + public Builder setInsulinProgramElements(List insulinProgramElements) { + if (insulinProgramElements == null) { + throw new IllegalArgumentException("insulinProgramElements can not be null"); + } + this.insulinProgramElements = new ArrayList<>(insulinProgramElements); + return this; + } + + public Builder setProgramReminder(ProgramReminder programReminder) { + this.programReminder = programReminder; + return this; + } + + public Builder setCurrentInsulinProgramElementIndex(Byte currentInsulinProgramElementIndex) { + this.currentInsulinProgramElementIndex = currentInsulinProgramElementIndex; + return this; + } + + public Builder setRemainingTenthPulsesInCurrentInsulinProgramElement(Short remainingTenthPulsesInCurrentInsulinProgramElement) { + this.remainingTenthPulsesInCurrentInsulinProgramElement = remainingTenthPulsesInCurrentInsulinProgramElement; + return this; + } + + public Builder setDelayUntilNextTenthPulseInUsec(Integer delayUntilNextTenthPulseInUsec) { + this.delayUntilNextTenthPulseInUsec = delayUntilNextTenthPulseInUsec; + return this; + } + + @Override public ProgramBasalCommand build() { + if (insulinProgramElements == null) { + throw new IllegalArgumentException("insulinProgramElements can not be null"); + } + if (programReminder == null) { + throw new IllegalArgumentException("programReminder can not be null"); + } + if (currentInsulinProgramElementIndex == null) { + throw new IllegalArgumentException("currentInsulinProgramElementIndex can not be null"); + } + if (remainingTenthPulsesInCurrentInsulinProgramElement == null) { + throw new IllegalArgumentException("remainingTenthPulsesInCurrentInsulinProgramElement can not be null"); + } + if (delayUntilNextTenthPulseInUsec == null) { + throw new IllegalArgumentException("delayUntilNextTenthPulseInUsec can not be null"); + } + return new ProgramBasalCommand(insulinProgramElements, programReminder, currentInsulinProgramElementIndex, remainingTenthPulsesInCurrentInsulinProgramElement, delayUntilNextTenthPulseInUsec); + } + } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramInsulinCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramInsulinCommand.java index 7732611566..30e175d616 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramInsulinCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/ProgramInsulinCommand.java @@ -8,12 +8,14 @@ import java.util.List; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.CommandType; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.NonceEnabledCommand; -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.program.InsulinProgramElement; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder.NonceEnabledCommandBuilder; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.Encodable; // Always followed by one of: 0x13, 0x16, 0x17 public final class ProgramInsulinCommand extends NonceEnabledCommand { - private final List max8HourInsulinProgramElements; + private final List insulinProgramElements; private final byte currentHalfHourEntryIndex; + private final short checksum; private final short remainingEighthSecondsInCurrentHalfHourEntry; private final short remainingPulsesInCurrentHalfHourEntry; private final DeliveryType deliveryType; @@ -25,10 +27,11 @@ public final class ProgramInsulinCommand extends NonceEnabledCommand { CommandType.PROGRAM_BOLUS ); - private ProgramInsulinCommand(int address, short sequenceNumber, boolean multiCommandFlag, int nonce, List max8HourInsulinProgramElements, byte currentHalfHourEntryIndex, short remainingEighthSecondsInCurrentHalfHourEntry, short remainingPulsesInCurrentHalfHourEntry, DeliveryType deliveryType, Command interlockCommand) { + private ProgramInsulinCommand(int address, short sequenceNumber, boolean multiCommandFlag, int nonce, List insulinProgramElements, byte currentHalfHourEntryIndex, short checksum, short remainingEighthSecondsInCurrentHalfHourEntry, short remainingPulsesInCurrentHalfHourEntry, DeliveryType deliveryType, Command interlockCommand) { super(CommandType.PROGRAM_INSULIN, address, sequenceNumber, multiCommandFlag, nonce); - this.max8HourInsulinProgramElements = new ArrayList<>(max8HourInsulinProgramElements); + this.insulinProgramElements = new ArrayList<>(insulinProgramElements); this.currentHalfHourEntryIndex = currentHalfHourEntryIndex; + this.checksum = checksum; this.remainingEighthSecondsInCurrentHalfHourEntry = remainingEighthSecondsInCurrentHalfHourEntry; this.remainingPulsesInCurrentHalfHourEntry = remainingPulsesInCurrentHalfHourEntry; this.deliveryType = deliveryType; @@ -36,11 +39,11 @@ public final class ProgramInsulinCommand extends NonceEnabledCommand { } public short getLength() { - return (short) (max8HourInsulinProgramElements.size() * 6 + 10); + return (short) (insulinProgramElements.size() * 6 + 10); } public byte getBodyLength() { - return (byte) (max8HourInsulinProgramElements.size() * 6 + 8); + return (byte) (insulinProgramElements.size() * 6 + 8); } @Override public byte[] getEncoded() { @@ -49,39 +52,40 @@ public final class ProgramInsulinCommand extends NonceEnabledCommand { .put(getBodyLength()) // .putInt(nonce) // .put(deliveryType.getValue()) // - .putShort(createChecksum()) // + .putShort(checksum) // .put(currentHalfHourEntryIndex) // .putShort(remainingEighthSecondsInCurrentHalfHourEntry) // .putShort(remainingPulsesInCurrentHalfHourEntry); - for (InsulinProgramElement element : max8HourInsulinProgramElements) { + for (InsulinProgramElement element : insulinProgramElements) { commandBuffer.put(element.getEncoded()); } byte[] command = commandBuffer.array(); + byte[] interlock = interlockCommand.getEncoded(); + short totalLength = (short) (command.length + interlock.length + HEADER_LENGTH); - // TODO interlock and header - - return command; + return ByteBuffer.allocate(totalLength) // + .put(encodeHeader(address, sequenceNumber, totalLength, multiCommandFlag)) // + .put(command) // + .put(interlock) // + .array(); } - private short createChecksum() { - return 0; // TODO - } - - public static final class Builder extends NonceEnabledBuilder { - private List max8HourInsulinProgramElements; + public static final class Builder extends NonceEnabledCommandBuilder { + private List insulinProgramElements; private Byte currentHalfOurEntryIndex; + private Short checksum; private Short remainingEighthSecondsInCurrentHalfHourEntry; private Short remainingPulsesInCurrentHalfHourEntry; private DeliveryType deliveryType; private Command interlockCommand; - public Builder setMax8HourInsulinProgramElements(List max8HourInsulinProgramElements) { - if (max8HourInsulinProgramElements == null) { - throw new IllegalArgumentException("max8HourInsulinProgramElements can not be null"); + public Builder setInsulinProgramElements(List insulinProgramElements) { + if (insulinProgramElements == null) { + throw new IllegalArgumentException("insulinProgramElements can not be null"); } - this.max8HourInsulinProgramElements = new ArrayList<>(max8HourInsulinProgramElements); + this.insulinProgramElements = new ArrayList<>(insulinProgramElements); return this; } @@ -90,6 +94,11 @@ public final class ProgramInsulinCommand extends NonceEnabledCommand { return this; } + public Builder setChecksum(short checksum) { + this.checksum = checksum; + return this; + } + public Builder setRemainingEighthSecondsInCurrentHalfHourEntryIndex(short remainingEighthSecondsInCurrentHalfHourEntry) { this.remainingEighthSecondsInCurrentHalfHourEntry = remainingEighthSecondsInCurrentHalfHourEntry; return this; @@ -114,12 +123,15 @@ public final class ProgramInsulinCommand extends NonceEnabledCommand { } @Override protected final ProgramInsulinCommand buildCommand() { - if (max8HourInsulinProgramElements == null) { + if (insulinProgramElements == null) { throw new IllegalArgumentException("insulinProgramElements can not be null"); } if (currentHalfOurEntryIndex == null) { throw new IllegalArgumentException("currentHalfOurEntryIndex can not be null"); } + if (checksum == null) { + throw new IllegalArgumentException("checksum can not be null"); + } if (remainingEighthSecondsInCurrentHalfHourEntry == null) { throw new IllegalArgumentException("remainingEighthSecondsInCurrentHalfHourEntry can not be null"); } @@ -132,7 +144,7 @@ public final class ProgramInsulinCommand extends NonceEnabledCommand { if (interlockCommand == null) { throw new IllegalArgumentException("interlockCommand can not be null"); } - return new ProgramInsulinCommand(address, sequenceNumber, multiCommandFlag, nonce, max8HourInsulinProgramElements, currentHalfOurEntryIndex, remainingEighthSecondsInCurrentHalfHourEntry, remainingPulsesInCurrentHalfHourEntry, deliveryType, interlockCommand); + return new ProgramInsulinCommand(address, sequenceNumber, multiCommandFlag, nonce, insulinProgramElements, currentHalfOurEntryIndex, checksum, remainingEighthSecondsInCurrentHalfHourEntry, remainingPulsesInCurrentHalfHourEntry, deliveryType, interlockCommand); } } @@ -152,4 +164,43 @@ public final class ProgramInsulinCommand extends NonceEnabledCommand { } } + @Override public String toString() { + return "ProgramInsulinCommand{" + + "insulinProgramElements=" + insulinProgramElements + + ", currentHalfHourEntryIndex=" + currentHalfHourEntryIndex + + ", checksum=" + checksum + + ", remainingEighthSecondsInCurrentHalfHourEntry=" + remainingEighthSecondsInCurrentHalfHourEntry + + ", remainingPulsesInCurrentHalfHourEntry=" + remainingPulsesInCurrentHalfHourEntry + + ", deliveryType=" + deliveryType + + ", interlockCommand=" + interlockCommand + + ", nonce=" + nonce + + ", commandType=" + commandType + + ", address=" + address + + ", sequenceNumber=" + sequenceNumber + + ", multiCommandFlag=" + multiCommandFlag + + '}'; + } + + public static class InsulinProgramElement implements Encodable { + private final byte numberOfHalfOurEntries; // 4 bits + private final short numberOfPulsesPerHalfOurEntry; // 10 bits + private final boolean extraAlternatePulse; + + public InsulinProgramElement(byte numberOfHalfOurEntries, short numberOfPulsesPerHalfOurEntry, boolean extraAlternatePulse) { + this.numberOfHalfOurEntries = numberOfHalfOurEntries; + this.numberOfPulsesPerHalfOurEntry = numberOfPulsesPerHalfOurEntry; + this.extraAlternatePulse = extraAlternatePulse; + } + + @Override public byte[] getEncoded() { + byte firstByte = (byte) ((((numberOfHalfOurEntries - 1) & 0x0f) << 4) // + | ((extraAlternatePulse ? 1 : 0) << 3) // + | ((numberOfPulsesPerHalfOurEntry >>> 8) & 0x03)); + + return ByteBuffer.allocate(2) // + .put(firstByte) // + .put((byte) (numberOfPulsesPerHalfOurEntry & 0xff)) // + .array(); + } + } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/SetUniqueIdCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/SetUniqueIdCommand.java index 0a8d6086f4..7b36a22cd3 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/SetUniqueIdCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/SetUniqueIdCommand.java @@ -6,6 +6,7 @@ import java.util.Date; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.CommandType; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.HeaderEnabledCommand; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder.HeaderEnabledCommandBuilder; public final class SetUniqueIdCommand extends HeaderEnabledCommand { private static final int DEFAULT_ADDRESS = -1; @@ -62,7 +63,7 @@ public final class SetUniqueIdCommand extends HeaderEnabledCommand { '}'; } - public static final class Builder extends HeaderEnabledBuilder { + public static final class Builder extends HeaderEnabledCommandBuilder { private Integer lotNumber; private Integer podSequenceNumber; private Date initializationTime; diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/SilenceAlertsCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/SilenceAlertsCommand.java index 83e6e1370f..b818ff34ad 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/SilenceAlertsCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/SilenceAlertsCommand.java @@ -5,6 +5,7 @@ import java.util.BitSet; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.CommandType; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.NonceEnabledCommand; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder.NonceEnabledCommandBuilder; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.Encodable; public final class SilenceAlertsCommand extends NonceEnabledCommand { @@ -74,7 +75,7 @@ public final class SilenceAlertsCommand extends NonceEnabledCommand { } } - public static class Builder extends NonceEnabledBuilder { + public static class Builder extends NonceEnabledCommandBuilder { private boolean silenceAutoOffAlert; private boolean silenceMultiCommandAlert; private boolean silenceExpirationImminentAlert; diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/StopDeliveryCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/StopDeliveryCommand.java index 1f1ef1fdce..e4ea66a55f 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/StopDeliveryCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/StopDeliveryCommand.java @@ -4,8 +4,8 @@ import java.nio.ByteBuffer; import java.util.BitSet; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.CommandType; -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.HeaderEnabledCommand; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.NonceEnabledCommand; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder.NonceEnabledCommandBuilder; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BeepType; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.Encodable; @@ -68,7 +68,7 @@ public final class StopDeliveryCommand extends NonceEnabledCommand { } } - public static final class Builder extends NonceEnabledBuilder { + public static final class Builder extends NonceEnabledCommandBuilder { private DeliveryType deliveryType; private BeepType beepType = BeepType.LONG_SINGLE_BEEP; diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/HeaderEnabledCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/HeaderEnabledCommand.java index bad2de5be4..219dc48724 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/HeaderEnabledCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/HeaderEnabledCommand.java @@ -37,40 +37,4 @@ public abstract class HeaderEnabledCommand implements Command { .array(); } - protected static abstract class HeaderEnabledBuilder, R extends Command> implements Builder { - protected Integer address; - protected Short sequenceNumber; - protected boolean multiCommandFlag = false; - - public R build() { - if (address == null) { - throw new IllegalArgumentException("address can not be null"); - } - if (sequenceNumber == null) { - throw new IllegalArgumentException("sequenceNumber can not be null"); - } - return buildCommand(); - } - - public final T setAddress(int address) { - this.address = address; - return (T) this; - } - - public final T setSequenceNumber(short sequenceNumber) { - this.sequenceNumber = sequenceNumber; - return (T) this; - } - - public final T setMultiCommandFlag(boolean multiCommandFlag) { - this.multiCommandFlag = multiCommandFlag; - return (T) this; - } - - protected abstract R buildCommand(); - } - - protected interface Builder { - R build(); - } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/NonceEnabledCommand.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/NonceEnabledCommand.java index f485a495e1..16a136601b 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/NonceEnabledCommand.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/NonceEnabledCommand.java @@ -8,19 +8,4 @@ public abstract class NonceEnabledCommand extends HeaderEnabledCommand { this.nonce = nonce; } - protected static abstract class NonceEnabledBuilder, R extends Command> extends HeaderEnabledBuilder { - protected Integer nonce; - - public final R build() { - if (nonce == null) { - throw new IllegalArgumentException("nonce can not be null"); - } - return super.build(); - } - - public final T setNonce(int nonce) { - this.nonce = nonce; - return (T) this; - } - } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/CommandBuilder.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/CommandBuilder.java new file mode 100644 index 0000000000..3b4ca66ca0 --- /dev/null +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/CommandBuilder.java @@ -0,0 +1,7 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder; + +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command; + +public interface CommandBuilder { + R build(); +} diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/HeaderEnabledCommandBuilder.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/HeaderEnabledCommandBuilder.java new file mode 100644 index 0000000000..92d1f50c92 --- /dev/null +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/HeaderEnabledCommandBuilder.java @@ -0,0 +1,36 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder; + +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command; + +public abstract class HeaderEnabledCommandBuilder, R extends Command> implements CommandBuilder { + protected Integer address; + protected Short sequenceNumber; + protected boolean multiCommandFlag = false; + + public R build() { + if (address == null) { + throw new IllegalArgumentException("address can not be null"); + } + if (sequenceNumber == null) { + throw new IllegalArgumentException("sequenceNumber can not be null"); + } + return buildCommand(); + } + + public final T setAddress(int address) { + this.address = address; + return (T) this; + } + + public final T setSequenceNumber(short sequenceNumber) { + this.sequenceNumber = sequenceNumber; + return (T) this; + } + + public final T setMultiCommandFlag(boolean multiCommandFlag) { + this.multiCommandFlag = multiCommandFlag; + return (T) this; + } + + protected abstract R buildCommand(); +} diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/NonceEnabledCommandBuilder.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/NonceEnabledCommandBuilder.java new file mode 100644 index 0000000000..806eda8965 --- /dev/null +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/base/builder/NonceEnabledCommandBuilder.java @@ -0,0 +1,19 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.builder; + +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command; + +public abstract class NonceEnabledCommandBuilder, R extends Command> extends HeaderEnabledCommandBuilder { + protected Integer nonce; + + public final R build() { + if (nonce == null) { + throw new IllegalArgumentException("nonce can not be null"); + } + return super.build(); + } + + public final T setNonce(int nonce) { + this.nonce = nonce; + return (T) this; + } +} diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/program/InsulinProgramElement.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/program/InsulinProgramElement.java deleted file mode 100644 index 2707efdfcb..0000000000 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/program/InsulinProgramElement.java +++ /dev/null @@ -1,28 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.program; - -import java.nio.ByteBuffer; - -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.Encodable; - -public class InsulinProgramElement implements Encodable { - private final byte numberOfHalfOurEntries; // 4 bits - private final short numberOfPulsesPerHalfOurEntry; // 10 bits - private final boolean extraAlternatePulse; - - public InsulinProgramElement(byte numberOfHalfOurEntries, short numberOfPulsesPerHalfOurEntry, boolean extraAlternatePulse) { - this.numberOfHalfOurEntries = numberOfHalfOurEntries; - this.numberOfPulsesPerHalfOurEntry = numberOfPulsesPerHalfOurEntry; - this.extraAlternatePulse = extraAlternatePulse; - } - - @Override public byte[] getEncoded() { - byte firstByte = (byte) ((((numberOfHalfOurEntries - 1) & 0x0f) << 4) // - | ((extraAlternatePulse ? 1 : 0) << 3) // - | ((numberOfPulsesPerHalfOurEntry >>> 8) & 0x03)); - - return ByteBuffer.allocate(2) // - .put(firstByte) // - .put((byte) (numberOfPulsesPerHalfOurEntry & 0xff)) // - .array(); - } -}