Skip to content

Commit

Permalink
Merge pull request #62 from GlobalFishingWatch/PIPELINE-1942
Browse files Browse the repository at this point in the history
Don't fail on type 5 messages with bad padding value
pwoods25443 authored Apr 17, 2024
2 parents a618029 + 2e71c44 commit d834a28
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ais_tools/ais.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ais_tools.transcode import DecodeError
from ais_tools.transcode import ASCII8toAIS6
from ais_tools import ais5
from ais_tools import ais8
from ais_tools import ais9
from ais_tools import ais18
@@ -18,6 +19,7 @@
}

decode_fn = {
5: ais5.ais5_decode,
8: ais8.ais8_decode,
9: ais9.ais9_decode,
18: ais18.ais18_decode,
10 changes: 10 additions & 0 deletions ais_tools/ais5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ais as libais
from ais import DecodeError


def ais5_decode(body, pad):
try:
return libais.decode(body, 2)
except DecodeError as e:
raise DecodeError(f'TYPE 5 LIBAIS ERR: {str(e)}')

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
name = "ais-tools"
description = "Tools for managing AIS messages"
readme = "README.md"
version = "v0.1.6.dev9"
version = "v0.1.7"
license = {file = "LICENSE"}
authors = [
{name = "Paul Woods", email = "paul@globalfishingwatch.org"},
24 changes: 24 additions & 0 deletions tests/test_ais.py
Original file line number Diff line number Diff line change
@@ -81,6 +81,30 @@ def test_ais8_wrong_pad():
assert ('83am8S@j<d8dtfMEuj9loFOM6@0', 2) == AISMessageTranscoder.encode_nmea(msg)


@pytest.mark.parametrize("body,pad,expected", [
# incorrect padding
('56:=31`000008QaF220QD60`T4pN3N2222222216>pN5@50e0ES2@C`6EC`1hCQp8888880', 0,
{'name': 'HUA JIANG 7 '}),
# correct padding
('56:=31`000008QaF220QD60`T4pN3N2222222216>pN5@50e0ES2@C`6EC`1hCQp8888880', 2,
{'name': 'HUA JIANG 7 '})
])
def test_ais5(body, pad, expected):
msg = AISMessageTranscoder.decode_nmea(body, pad)
actual = {k: v for k, v in msg.items() if k in expected}
assert actual == expected


@pytest.mark.parametrize("body,pad,expected", [
# incorrect padding
('56:=31`000008QaF220QD60`T4pN3N2222222216>pN5@50e0ES2@C`6EC`1hCQp88888809999', 0,
'TYPE 5 LIBAIS ERR: Ais5: AIS_ERR_BAD_BIT_COUNT'),
])
def test_ais5_fail(body, pad, expected):
with pytest.raises(DecodeError, match=expected):
_ = AISMessageTranscoder.decode_nmea(body, pad)


@pytest.mark.parametrize("body,pad,expected", [
('9001?BP=h:qJ9vb;:f7EN1h240Rb', 0, {'mmsi': 20298, 'alt': 55, 'sog': 10}),
('90009C3dRIM1QSsjSPAa1;h200T4', 0, {'mmsi': 2380, 'alt': 946, 'alt_sensor': 0}),

0 comments on commit d834a28

Please sign in to comment.