From 4e58f60724d28ec8e3ce95a95de80cd00a4c719c Mon Sep 17 00:00:00 2001 From: James Woglom Date: Wed, 25 Dec 2024 22:45:05 -0500 Subject: [PATCH] TempRateResponse: fix percentage parsing --- .../currentStatus/TempRateResponse.java | 5 ++- .../currentStatus/TempRateResponseTest.java | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/messages/src/main/java/com/jwoglom/pumpx2/pump/messages/response/currentStatus/TempRateResponse.java b/messages/src/main/java/com/jwoglom/pumpx2/pump/messages/response/currentStatus/TempRateResponse.java index cccfeebc..b12b2ff4 100644 --- a/messages/src/main/java/com/jwoglom/pumpx2/pump/messages/response/currentStatus/TempRateResponse.java +++ b/messages/src/main/java/com/jwoglom/pumpx2/pump/messages/response/currentStatus/TempRateResponse.java @@ -24,6 +24,9 @@ public class TempRateResponse extends Message { private long duration; public TempRateResponse() {} + public TempRateResponse(byte[] raw) { + parse(raw); + } public TempRateResponse(boolean active, int percentage, long startTimeRaw, long duration) { this.cargo = buildCargo(active, percentage, startTimeRaw, duration); @@ -38,7 +41,7 @@ public void parse(byte[] raw) { Preconditions.checkArgument(raw.length == props().size()); this.cargo = raw; this.active = raw[0] != 0; - this.percentage = raw[1]; + this.percentage = (int) (raw[1] & 255); this.startTimeRaw = Bytes.readUint32(raw, 2); this.duration = Bytes.readUint32(raw, 6); diff --git a/messages/src/test/java/com/jwoglom/pumpx2/pump/messages/response/currentStatus/TempRateResponseTest.java b/messages/src/test/java/com/jwoglom/pumpx2/pump/messages/response/currentStatus/TempRateResponseTest.java index bb922087..0e4f7e27 100644 --- a/messages/src/test/java/com/jwoglom/pumpx2/pump/messages/response/currentStatus/TempRateResponseTest.java +++ b/messages/src/test/java/com/jwoglom/pumpx2/pump/messages/response/currentStatus/TempRateResponseTest.java @@ -2,6 +2,9 @@ import static com.jwoglom.pumpx2.pump.messages.MessageTester.assertHexEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import com.jwoglom.pumpx2.pump.messages.MessageTester; import com.jwoglom.pumpx2.pump.messages.bluetooth.CharacteristicUUID; @@ -26,4 +29,36 @@ public void testTempRateResponseNotActive() throws DecoderException { assertHexEquals(expected.getCargo(), parsedRes.getCargo()); } + + @Test + public void testTempRateResponse_values() { + TempRateResponse r; + + r = new TempRateResponse( + new byte[]{1,-6,-38,0,-13,31,-124,3,0,0} + ); + assertTrue(r.getActive()); + assertEquals(900, r.getDuration()); + assertEquals(536019162, r.getStartTimeRaw()); + assertEquals(250, r.getPercentage()); + + r = new TempRateResponse( + new byte[]{1,-11,-99,5,-13,31,-124,3,0,0} + ); + assertTrue(r.getActive()); + assertEquals(900, r.getDuration()); + assertEquals(536020381, r.getStartTimeRaw()); + assertEquals(245, r.getPercentage()); + + + r = new TempRateResponse( + new byte[]{1,-56,57,5,-13,31,8,7,0,0} + ); + assertTrue(r.getActive()); + assertEquals(1800, r.getDuration()); + assertEquals(536020281, r.getStartTimeRaw()); + assertEquals(200, r.getPercentage()); + + + } } \ No newline at end of file