Skip to content

Commit

Permalink
Fix ROT decode rounding (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidalo authored Mar 16, 2024
1 parent 06bd464 commit 38a91b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyais/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ def to_turn(turn: typing.Union[int, float]) -> typing.Union[float, TurnRate]:
elif abs(turn) == 128:
return TurnRate.NO_TI_DEFAULT

return math.copysign(int((turn / 4.733) ** 2), turn)
return math.copysign(round((turn / 4.733) ** 2), turn)


def from_turn(turn: typing.Optional[typing.Union[int, float, TurnRate]]) -> int:
Expand Down
21 changes: 17 additions & 4 deletions tests/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,12 +1364,25 @@ def test_rot_encode_yields_expected_values(self):
encoded = encode_dict({"msg_type": 1, "mmsi": 123, "turn": 64.0})[0]
assert encoded == "!AIVDO,1,1,,A,10000Nh9P0000000000000000000,0*6A"

def test_rot_encode_decode(self):
encoded = encode_dict({"msg_type": 1, "mmsi": 123, "turn": 2.0})[0]
assert decode(encoded).turn == 2.0

encoded = encode_dict({"msg_type": 1, "mmsi": 123, "turn": 3.0})[0]
assert decode(encoded).turn == 3.0

encoded = encode_dict({"msg_type": 1, "mmsi": 123, "turn": 4.0})[0]
assert decode(encoded).turn == 4.0

encoded = encode_dict({"msg_type": 1, "mmsi": 123, "turn": 5.0})[0]
assert decode(encoded).turn == 5.0

def test_rot_decode_yields_expected_values(self):
assert decode(b"!AIVDM,1,1,,A,14QIG<5620KF@Gl:L9DI4o8N0P00,0*28").turn == 25.0
assert decode(b"!AIVDM,1,1,,A,14QIG<5620KF@Gl:L9DI4o8N0P00,0*28").turn == 26.0
assert decode(b"!AIVDM,1,1,,B,13u><=gsQj0mQW:Q1<wRL28P0@:4,0*32").turn == -14.0
assert decode(b"!AIVDM,1,1,,A,14SSRt021O0?bK@MO7H6QUA600Rg,0*12").turn == 2.0
assert decode(b"!AIVDM,1,1,,2,13aB:Hhuh0PHjEFNKJg@11sH08J=,0*1E").turn == -3.0
assert decode(b"!AIVDM,1,1,,A,16:VFv0k0I`KQPpFATG4SgvT40:v,0*7B").turn == -120.0
assert decode(b"!AIVDM,1,1,,A,14SSRt021O0?bK@MO7H6QUA600Rg,0*12").turn == 3.0
assert decode(b"!AIVDM,1,1,,2,13aB:Hhuh0PHjEFNKJg@11sH08J=,0*1E").turn == -4.0
assert decode(b"!AIVDM,1,1,,A,16:VFv0k0I`KQPpFATG4SgvT40:v,0*7B").turn == -121.0
assert decode(b"!AIVDM,1,1,,B,16:D3F0:15`5ogh<O?bk>1Dd2L1<,0*0B").turn == 71.0

def test_get_sotdma_comm_state_utc_direct(self):
Expand Down

0 comments on commit 38a91b1

Please sign in to comment.