-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
...n/java/com/jwoglom/pumpx2/pump/messages/request/control/SetQuickBolusSettingsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...java/com/jwoglom/pumpx2/pump/messages/response/control/SetQuickBolusSettingsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.