Skip to content

Commit

Permalink
TempRateResponse: fix percentage parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoglom committed Dec 26, 2024
1 parent 8752d21 commit 4e58f60
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());


}
}

0 comments on commit 4e58f60

Please sign in to comment.