From 38a91b14cfdf796bf10aeb1344ac3eadd724ba68 Mon Sep 17 00:00:00 2001 From: davidalo Date: Sat, 16 Mar 2024 11:31:15 +0100 Subject: [PATCH] Fix ROT decode rounding (#130) --- pyais/messages.py | 2 +- tests/test_decode.py | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/pyais/messages.py b/pyais/messages.py index c819129..3f6ef48 100644 --- a/pyais/messages.py +++ b/pyais/messages.py @@ -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: diff --git a/tests/test_decode.py b/tests/test_decode.py index 79cdf5a..7d8bb49 100644 --- a/tests/test_decode.py +++ b/tests/test_decode.py @@ -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:Q11Dd2L1<,0*0B").turn == 71.0 def test_get_sotdma_comm_state_utc_direct(self):