From d1bf9cefad6fbe168d8cde6c7f21cefa0dbd101b Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Fri, 12 Feb 2021 21:47:16 +0100 Subject: [PATCH] Add Encodable interface --- .../pump/omnipod/dash/driver/pod/command/Command.java | 6 +++--- .../dash/driver/pod/command/SilenceAlertsCommand.java | 7 +++++-- .../dash/driver/pod/command/StopDeliveryCommand.java | 9 +++++---- .../driver/pod/command/interlock/BasalInterlock.java | 7 ++++++- .../dash/driver/pod/definition/AlertConfiguration.java | 4 ++-- .../omnipod/dash/driver/pod/definition/Encodable.java | 5 +++++ .../dash/driver/pod/definition/ProgramReminder.java | 8 ++++---- .../pod/definition/program/InsulinProgramElement.java | 6 ++++-- 8 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/Encodable.java diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/Command.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/Command.java index b05534135e..50f013c280 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/Command.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/Command.java @@ -1,7 +1,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command; -public interface Command { - CommandType getCommandType(); +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.Encodable; - byte[] getEncoded(); +public interface Command extends Encodable { + CommandType getCommandType(); } 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 dd8f03fc97..bbf9d16b4d 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 @@ -3,6 +3,8 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command; import java.nio.ByteBuffer; import java.util.BitSet; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.Encodable; + public final class SilenceAlertsCommand extends CommandBase { private static final short LENGTH = (short) 7; private static final byte BODY_LENGTH = (byte) 5; @@ -34,7 +36,7 @@ public final class SilenceAlertsCommand extends CommandBase { '}'; } - private static final class SilenceAlertCommandParameters { + private static final class SilenceAlertCommandParameters implements Encodable { private final boolean silenceAutoOffAlert; private final boolean silenceMultiCommandAlert; private final boolean silenceExpirationImminentAlert; @@ -55,7 +57,8 @@ public final class SilenceAlertsCommand extends CommandBase { this.silencePodExpirationAlert = silencePodExpirationAlert; } - private byte[] getEncoded() { + @Override + public byte[] getEncoded() { BitSet bitSet = new BitSet(8); bitSet.set(0, this.silenceAutoOffAlert); bitSet.set(1, this.silenceMultiCommandAlert); 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 698163a2cf..df8ba3e3db 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,6 +4,7 @@ import java.nio.ByteBuffer; import java.util.BitSet; import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BeepType; +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.Encodable; public final class StopDeliveryCommand extends CommandBase { private static final short LENGTH = 7; @@ -24,7 +25,7 @@ public final class StopDeliveryCommand extends CommandBase { .put(commandType.getValue()) // .put(BODY_LENGTH) // .putInt(1229869870) // FIXME ?? was: byte array of int 777211465 converted to little endian - .put((byte) ((beepType.getValue() << 4) | deliveryType.getEncoded())) // + .put((byte) ((beepType.getValue() << 4) | deliveryType.getEncoded()[0])) // .array()); } @@ -39,7 +40,7 @@ public final class StopDeliveryCommand extends CommandBase { '}'; } - public enum DeliveryType { + public enum DeliveryType implements Encodable { BASAL(true, false, false), TEMP_BASAL(false, true, false), BOLUS(false, false, true), @@ -55,12 +56,12 @@ public final class StopDeliveryCommand extends CommandBase { this.bolus = bolus; } - public byte getEncoded() { + @Override public byte[] getEncoded() { BitSet bitSet = new BitSet(8); bitSet.set(0, this.basal); bitSet.set(1, this.tempBasal); bitSet.set(2, this.bolus); - return bitSet.toByteArray()[0]; + return bitSet.toByteArray(); } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/interlock/BasalInterlock.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/interlock/BasalInterlock.java index 32c5c00fd6..1a76987ac8 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/interlock/BasalInterlock.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/command/interlock/BasalInterlock.java @@ -1,4 +1,9 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.interlock; -public class BasalInterlock { +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.Encodable; + +public class BasalInterlock implements Encodable { + @Override public byte[] getEncoded() { + return new byte[0]; + } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/AlertConfiguration.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/AlertConfiguration.java index f620df086d..8a52836d58 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/AlertConfiguration.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/AlertConfiguration.java @@ -2,7 +2,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definiti import java.nio.ByteBuffer; -public class AlertConfiguration { +public class AlertConfiguration implements Encodable { private AlertSlot slot; private boolean enabled; private short durationInMinutes; @@ -23,7 +23,7 @@ public class AlertConfiguration { this.beepRepetition = beepRepetition; } - public byte[] getEncoded() { + @Override public byte[] getEncoded() { byte firstByte = (byte) (slot.getValue() << 4); if (enabled) { firstByte |= 1 << 3; diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/Encodable.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/Encodable.java new file mode 100644 index 0000000000..d17d42050f --- /dev/null +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/Encodable.java @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition; + +public interface Encodable { + byte[] getEncoded(); +} diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/ProgramReminder.java b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/ProgramReminder.java index 8daf6fa609..22b0402e4d 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/ProgramReminder.java +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/ProgramReminder.java @@ -1,6 +1,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition; -public class ProgramReminder { +public class ProgramReminder implements Encodable { private final boolean atStart; private final boolean atEnd; private final byte atInterval; @@ -11,9 +11,9 @@ public class ProgramReminder { this.atInterval = atIntervalInMinutes; } - public byte getEncoded() { - return (byte) (((this.atStart ? 0 : 1) << 7) + @Override public byte[] getEncoded() { + return new byte[]{(byte) (((this.atStart ? 0 : 1) << 7) | ((this.atEnd ? 0 : 1) << 6) - | (this.atInterval & 0x3f)); + | (this.atInterval & 0x3f))}; } } 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 index 2ffea46ec1..57f197c4d4 100644 --- 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 @@ -2,7 +2,9 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definiti import java.nio.ByteBuffer; -public class InsulinProgramElement { +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; @@ -13,7 +15,7 @@ public class InsulinProgramElement { this.extraAlternatePulse = extraAlternatePulse; } - public byte[] getEncoded() { + @Override public byte[] getEncoded() { byte firstByte = (byte) ((((numberOfHalfOurEntries - 1) & 0x0f) << 4) | ((extraAlternatePulse ? 1 : 0) << 3) | ((numberOfPulsesPerHalfOurEntry >>> 8) & 0x03));