M0r13n / pyais

AIS message decoding and encoding in Python (AIVDM/AIVDO)
MIT License
176 stars 61 forks source link

Timestamp for message type 1 and others #139

Closed hghayoom closed 2 months ago

hghayoom commented 3 months ago

Hi, thanks for your efforts in writing these.

I have streams like this: !AIVDM,2,2,6,A,H88888888888880,252,1630526404,1630526406 !ANVDM,1,1,,B,H5NG9mQ<PDp4p@t4R2222222220,21D,1630526405,1630526406 !ANVDM,1,1,1,A,H5O2hWPpu9@PE8r1=Dq8U<D0000,272,1630526405,1630526406 !AIVDM,1,1,,B,H5NCqDP@DhUHE84p<D000000000,232,1630526405,1630526406 !SAVDM,1,1,0,B,15NdWlgP00rUvQhFa=F>4?v62<0H,01A,1630526405,1630526406 !SAVDM,1,1,6,A,H5NTjuQ`uLDP000000000000000,25C,1630526405,1630526406

but your code does not save the timestamps.

Is it correct or I am messing something?

M0r13n commented 2 months ago

Hey @hghayoom,

you are right: pyais does not natively support the format that you provided. This is intended behavior because there are dozens of different formats that differ ever so slightly. Supporting all of these variations would be quite cumbersome.

However, pyais is built in such a way that it is easy for developers to adapt its behavior to their requirements. In your case, a simple method suffices to get both timestamps:

from pyais.messages import NMEAMessage
from pyais.stream import FileReaderStream

def get_ts(msg: NMEAMessage):
    ts1, ts2 = msg.raw.split(b',')[7:9]
    return int(ts1), int(ts2)

for msg in FileReaderStream('./examples/test.nmea'):
    print(get_ts(msg), msg.decode().mmsi)