Skip to content

Commit

Permalink
QuickBolusSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoglom committed Dec 24, 2024
1 parent 33e0237 commit bfce4b1
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
import com.jwoglom.pumpx2.pump.messages.response.control.ChangeControlIQSettingsResponse;
import com.jwoglom.pumpx2.pump.messages.request.control.EnterFillTubingModeRequest;
import com.jwoglom.pumpx2.pump.messages.response.control.EnterFillTubingModeResponse;
import com.jwoglom.pumpx2.pump.messages.request.control.SetQuickBolusSettingsRequest;
import com.jwoglom.pumpx2.pump.messages.response.control.SetQuickBolusSettingsResponse;
// IMPORT_END
import com.jwoglom.pumpx2.shared.L;

Expand Down Expand Up @@ -244,6 +246,7 @@ public enum Messages {
RESUME_PUMPING(ResumePumpingRequest.class, ResumePumpingResponse.class),
CHANGE_CONTROL_IQ_SETTINGS(ChangeControlIQSettingsRequest.class, ChangeControlIQSettingsResponse.class),
ENTER_FILL_TUBING_MODE(EnterFillTubingModeRequest.class, EnterFillTubingModeResponse.class),
SET_QUICK_BOLUS_SETTINGS(SetQuickBolusSettingsRequest.class, SetQuickBolusSettingsResponse.class),
// MESSAGES_END
;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.jwoglom.pumpx2.pump.messages.request.control;

import com.google.common.base.Preconditions;
import com.jwoglom.pumpx2.pump.messages.bluetooth.Characteristic;
import com.jwoglom.pumpx2.pump.messages.helpers.Bytes;
import com.jwoglom.pumpx2.pump.messages.Message;
import com.jwoglom.pumpx2.pump.messages.MessageType;
import com.jwoglom.pumpx2.pump.messages.annotations.MessageProps;
import com.jwoglom.pumpx2.pump.messages.response.control.SetQuickBolusSettingsResponse;

import java.util.Arrays;

@MessageProps(
opCode=-46,
size=7,
type=MessageType.REQUEST,
characteristic=Characteristic.CONTROL,
signed=true,
response=SetQuickBolusSettingsResponse.class
)
public class SetQuickBolusSettingsRequest extends Message {

private boolean enabled;
private int modeRaw;
private byte[] magic;

public SetQuickBolusSettingsRequest() {
this.cargo = EMPTY;
}

public SetQuickBolusSettingsRequest(byte[] raw) {
parse(raw);
}

public SetQuickBolusSettingsRequest(boolean enabled, QuickBolusMode mode, QuickBolusIncrement increment) {
this.cargo = buildCargo(enabled, mode.getRaw(), increment.getMagic());
this.enabled = enabled;
this.modeRaw = mode.getRaw();
this.magic = increment.getMagic();
}

public void parse(byte[] raw) {
raw = this.removeSignedRequestHmacBytes(raw);
Preconditions.checkArgument(raw.length == props().size());
this.cargo = raw;
this.enabled = raw[0] == 1;
this.modeRaw = raw[1];
this.magic = Bytes.dropFirstN(raw, 2);
checkValidIncrement();
}

public static byte[] buildCargo(boolean enabled, int modeRaw, byte[] magic) {
Preconditions.checkArgument(magic.length == 5);
return Bytes.combine(
new byte[]{(byte) (enabled ? 1 : 0)},
new byte[]{(byte) modeRaw},
magic
);
}


public boolean isEnabled() {
return enabled;
}

public byte[] getMagic() {
return magic;
}

public enum QuickBolusIncrement {
UNITS_0_5(QuickBolusMode.UNITS, new byte[]{-12,1,-48,7,1}),
UNITS_1_0(QuickBolusMode.UNITS, new byte[]{-24,3,-48,7,4}),
UNITS_2_O(QuickBolusMode.UNITS, new byte[]{-48,7,-48,7,4}),
UNITS_5_0(QuickBolusMode.UNITS, new byte[]{-120,19,-48,7,4}),

CARBS_2G(QuickBolusMode.CARBS, new byte[]{-120,19,-48,7,8}),
CARBS_5G(QuickBolusMode.CARBS, new byte[]{-120,19,-120,19,8}),
CARBS_10G(QuickBolusMode.CARBS, new byte[]{-120,19,16,39,8}),
CARBS_15G(QuickBolusMode.CARBS, new byte[]{-120,19,-104,58,8}),
;

private final QuickBolusMode mode;
private final byte[] magic;
QuickBolusIncrement(QuickBolusMode mode, byte[] magic) {
this.mode = mode;
this.magic = magic;
}

public QuickBolusMode getMode() {
return mode;
}

public byte[] getMagic() {
return magic;
}

public static QuickBolusIncrement forMagic(byte[] magic) {
for (QuickBolusIncrement i : values()) {
if (Arrays.equals(i.magic, magic)) return i;
}
return null;
}
}

public QuickBolusIncrement getIncrement() {
checkValidIncrement();
return QuickBolusIncrement.forMagic(this.magic);
}

public int getModeRaw() {
return modeRaw;
}

public enum QuickBolusMode {
UNITS(0),
CARBS(1),

;

private final int raw;
QuickBolusMode(int raw) {
this.raw = raw;
}

public int getRaw() {
return raw;
}

public static QuickBolusMode forRaw(int raw) {
for (QuickBolusMode m : values()) {
if (m.raw == raw) return m;
}
return null;
}
}

public QuickBolusMode getMode() {
return QuickBolusMode.forRaw(this.modeRaw);
}

private void checkValidIncrement() {
QuickBolusIncrement increment = QuickBolusIncrement.forMagic(this.magic);
if (increment != null) {
Preconditions.checkState(increment.getMode().getRaw() == getModeRaw(),
"invalid mode selection: increment=" + increment + " mode=" + getMode());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.jwoglom.pumpx2.pump.messages.response.control;

import com.google.common.base.Preconditions;
import com.jwoglom.pumpx2.pump.messages.bluetooth.Characteristic;
import com.jwoglom.pumpx2.pump.messages.helpers.Bytes;
import com.jwoglom.pumpx2.pump.messages.Message;
import com.jwoglom.pumpx2.pump.messages.MessageType;
import com.jwoglom.pumpx2.pump.messages.annotations.MessageProps;
import com.jwoglom.pumpx2.pump.messages.request.control.SetQuickBolusSettingsRequest;

import java.math.BigInteger;

@MessageProps(
opCode=-45,
size=1,
type=MessageType.RESPONSE,
characteristic=Characteristic.CONTROL,
signed=true,
request=SetQuickBolusSettingsRequest.class
)
public class SetQuickBolusSettingsResponse extends Message {

private int status;


public SetQuickBolusSettingsResponse() {
this.cargo = EMPTY;

}

public SetQuickBolusSettingsResponse(byte[] raw) {
this.cargo = raw;
parse(raw);
}

public void parse(byte[] raw) {
raw = this.removeSignedRequestHmacBytes(raw);
Preconditions.checkArgument(raw.length == props().size());
this.cargo = raw;
this.status = raw[0]; // 0 = ok

}


public int getStatus() {
return status;
}
}
Loading

0 comments on commit bfce4b1

Please sign in to comment.