Skip to content

Commit

Permalink
DisconnectPump
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoglom committed Dec 26, 2024
1 parent 6beb481 commit 8752d21
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@
import com.jwoglom.pumpx2.pump.messages.response.control.StopG6SensorSessionResponse;
import com.jwoglom.pumpx2.pump.messages.request.control.PlaySoundRequest;
import com.jwoglom.pumpx2.pump.messages.response.control.PlaySoundResponse;
import com.jwoglom.pumpx2.pump.messages.request.control.DisconnectPumpRequest;
import com.jwoglom.pumpx2.pump.messages.response.control.DisconnectPumpResponse;
// IMPORT_END
import com.jwoglom.pumpx2.shared.L;

Expand Down Expand Up @@ -265,6 +267,7 @@ public enum Messages {
START_G6_SENSOR_SESSION(StartG6SensorSessionRequest.class, StartG6SensorSessionResponse.class),
STOP_G6_SENSOR_SESSION(StopG6SensorSessionRequest.class, StopG6SensorSessionResponse.class),
PLAY_SOUND(PlaySoundRequest.class, PlaySoundResponse.class),
DISCONNECT_PUMP(DisconnectPumpRequest.class, DisconnectPumpResponse.class),
// MESSAGES_END
;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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.models.KnownApiVersion;
import com.jwoglom.pumpx2.pump.messages.response.control.DisconnectPumpResponse;

/*
* I don't actually think calling this command is required... since an app can just disconnect
* from the device over Bluetooth and forget the pairing key, and the user needs to re-trigger
* a pair in order to connect again anyway (either via menu on tslim X2 or entering "pairing mode"
* on the Mobi)
*/
@MessageProps(
opCode=-66,
size=0,
type=MessageType.REQUEST,
characteristic=Characteristic.CONTROL,
signed=true,
response=DisconnectPumpResponse.class,
minApi=KnownApiVersion.MOBI_API_V3_5
)
public class DisconnectPumpRequest extends Message {
public DisconnectPumpRequest() {
this.cargo = EMPTY;
}

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

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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.models.KnownApiVersion;
import com.jwoglom.pumpx2.pump.messages.request.control.DisconnectPumpRequest;

import java.math.BigInteger;

@MessageProps(
opCode=-65,
size=1,
type=MessageType.RESPONSE,
characteristic=Characteristic.CONTROL,
signed=true,
request=DisconnectPumpRequest.class,
minApi=KnownApiVersion.MOBI_API_V3_5
)
public class DisconnectPumpResponse extends Message {

private int status;

public DisconnectPumpResponse() {}

public DisconnectPumpResponse(int status) {
this.cargo = buildCargo(status);
this.status = status;

}

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

}


public static byte[] buildCargo(int status) {
return Bytes.combine(
new byte[]{ (byte) status });
}

public int getStatus() {
return status;
}

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

import static com.jwoglom.pumpx2.pump.messages.MessageTester.assertHexEquals;
import static com.jwoglom.pumpx2.pump.messages.MessageTester.initPumpState;

import com.jwoglom.pumpx2.pump.messages.MessageTester;
import com.jwoglom.pumpx2.pump.messages.bluetooth.CharacteristicUUID;
import com.jwoglom.pumpx2.pump.messages.request.control.DisconnectPumpRequest;

import org.apache.commons.codec.DecoderException;
import org.junit.Test;

public class DisconnectPumpRequestTest {
@Test
public void testDisconnectPumpRequest() throws DecoderException {
initPumpState("authenticationKey", 0L);

// empty cargo
DisconnectPumpRequest expected = new DisconnectPumpRequest();

DisconnectPumpRequest parsedReq = (DisconnectPumpRequest) MessageTester.test(
"01debede18da38f31f38413194c36c036e110c25",
-34,
1,
CharacteristicUUID.CONTROL_CHARACTERISTICS,
expected,
"00deff81b3f10f7af28dcfb8fc"
);

assertHexEquals(expected.getCargo(), parsedReq.getCargo());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.jwoglom.pumpx2.pump.messages.response.control;

import static com.jwoglom.pumpx2.pump.messages.MessageTester.assertHexEquals;
import static com.jwoglom.pumpx2.pump.messages.MessageTester.initPumpState;

import com.jwoglom.pumpx2.pump.messages.MessageTester;
import com.jwoglom.pumpx2.pump.messages.bluetooth.CharacteristicUUID;

import org.apache.commons.codec.DecoderException;
import org.junit.Test;

public class DisconnectPumpResponseTest {
@Test
public void testDisconnectPumpResponse() throws DecoderException {
initPumpState("authenticationKey", 0L);

DisconnectPumpResponse expected = new DisconnectPumpResponse(
0
);

DisconnectPumpResponse parsedRes = (DisconnectPumpResponse) MessageTester.test(
"00debfde1900000000003fad35823cf021e2dad26d3f485cd5b4557dfe88ef21",
-34,
1,
CharacteristicUUID.CONTROL_CHARACTERISTICS,
expected
);

assertHexEquals(expected.getCargo(), parsedRes.getCargo());
}
}

0 comments on commit 8752d21

Please sign in to comment.