WIP on ProgramBasalCommand

This commit is contained in:
Bart Sopers 2021-02-12 21:06:10 +01:00
parent e2e2c579d4
commit d944df725f

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command; package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -8,22 +9,50 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definitio
public final class ProgramBasalCommand extends CommandBase { public final class ProgramBasalCommand extends CommandBase {
private final List<InsulinProgramElement> insulinProgramElements; private final List<InsulinProgramElement> insulinProgramElements;
private final ProgramReminder programReminder;
private final byte currentHalfOurEntryIndex;
private final short remainingPulsesInCurrentHalfHourEntry;
private final int delayUntilNextTenthPulseInUsec;
private ProgramBasalCommand(int address, short sequenceNumber, boolean multiCommandFlag, ProgramReminder programReminder, List<InsulinProgramElement> insulinProgramElements) { private ProgramBasalCommand(int address, short sequenceNumber, boolean multiCommandFlag, ProgramReminder programReminder, List<InsulinProgramElement> insulinProgramElements, byte currentHalfOurEntryIndex, short remainingPulsesInCurrentHalfHourEntry, int delayUntilNextTenthPulseInUsec) {
super(CommandType.PROGRAM_BASAL, address, sequenceNumber, multiCommandFlag); super(CommandType.PROGRAM_BASAL, address, sequenceNumber, multiCommandFlag);
this.insulinProgramElements = new ArrayList<>(insulinProgramElements); this.insulinProgramElements = new ArrayList<>(insulinProgramElements);
this.programReminder = programReminder; this.programReminder = programReminder;
this.currentHalfOurEntryIndex = currentHalfOurEntryIndex;
this.remainingPulsesInCurrentHalfHourEntry = remainingPulsesInCurrentHalfHourEntry;
this.delayUntilNextTenthPulseInUsec = delayUntilNextTenthPulseInUsec;
} }
private final ProgramReminder programReminder; public byte getBodyLength() {
return (byte) (insulinProgramElements.size() * 6 + 8);
}
@Override public byte[] getEncoded() { @Override public byte[] getEncoded() {
return new byte[0]; ByteBuffer basalCommandBuffer = ByteBuffer.allocate(this.getBodyLength()) //
.put(commandType.getValue()) //
.put(getBodyLength()) //
.put(programReminder.getEncoded()) //
.put(currentHalfOurEntryIndex) //
.putShort(remainingPulsesInCurrentHalfHourEntry) //
.putInt(delayUntilNextTenthPulseInUsec);
for (InsulinProgramElement element : insulinProgramElements) {
basalCommandBuffer.put(element.getEncoded());
}
byte[] basalCommand = basalCommandBuffer.array();
// TODO basal interlock and header
return basalCommand;
} }
public static final class Builder extends CommandBase.Builder<Builder, ProgramBasalCommand> { public static final class Builder extends CommandBase.Builder<Builder, ProgramBasalCommand> {
private List<InsulinProgramElement> insulinProgramElements; private List<InsulinProgramElement> insulinProgramElements;
private ProgramReminder programReminder; private ProgramReminder programReminder;
private Byte currentHalfOurEntryIndex;
private Short remainingPulsesInCurrentHalfHourEntry;
private Integer delayUntilNextTenthPulseInUsec;
public Builder setInsulinProgramElements(List<InsulinProgramElement> insulinProgramElements) { public Builder setInsulinProgramElements(List<InsulinProgramElement> insulinProgramElements) {
if (insulinProgramElements == null) { if (insulinProgramElements == null) {
@ -38,6 +67,21 @@ public final class ProgramBasalCommand extends CommandBase {
return this; return this;
} }
public Builder setCurrentHalfOurEntryIndex(byte currentHalfOurEntryIndex) {
this.currentHalfOurEntryIndex = currentHalfOurEntryIndex;
return this;
}
public Builder setRemainingPulsesInCurrentHalfHourEntry(short remainingPulsesInCurrentHalfHourEntry) {
this.remainingPulsesInCurrentHalfHourEntry = remainingPulsesInCurrentHalfHourEntry;
return this;
}
public Builder setDelayUntilNextTenthPulseInUsec(Integer delayUntilNextTenthPulseInUsec) {
this.delayUntilNextTenthPulseInUsec = delayUntilNextTenthPulseInUsec;
return this;
}
@Override final ProgramBasalCommand buildCommand() { @Override final ProgramBasalCommand buildCommand() {
if (insulinProgramElements == null) { if (insulinProgramElements == null) {
throw new IllegalArgumentException("insulinProgramElements can not be null"); throw new IllegalArgumentException("insulinProgramElements can not be null");
@ -45,7 +89,16 @@ public final class ProgramBasalCommand extends CommandBase {
if (programReminder == null) { if (programReminder == null) {
throw new IllegalArgumentException("programReminder can not be null"); throw new IllegalArgumentException("programReminder can not be null");
} }
return new ProgramBasalCommand(address, sequenceNumber, multiCommandFlag, programReminder, insulinProgramElements); if (currentHalfOurEntryIndex == null) {
throw new IllegalArgumentException("currentHalfOurEntryIndex can not be null");
}
if (remainingPulsesInCurrentHalfHourEntry == null) {
throw new IllegalArgumentException("remainingPulsesInCurrentHalfHourEntry can not be null");
}
if (delayUntilNextTenthPulseInUsec == null) {
throw new IllegalArgumentException("durationUntilNextTenthPulseInUsec can not be null");
}
return new ProgramBasalCommand(address, sequenceNumber, multiCommandFlag, programReminder, insulinProgramElements, currentHalfOurEntryIndex, remainingPulsesInCurrentHalfHourEntry, delayUntilNextTenthPulseInUsec);
} }
} }