FOR TIPS, gUIDES & TUTORIALS

subscribe to our Youtube

GO TO YOUTUBE

14455 questions

17168 answers

28195 comments

0 members

We are migrating to our new platform at https://community.teltonika.lt. Moving forward, you can continue discussions on this new platform. This current platform will be temporarily maintained for reference purposes.
0 votes
1,518 views 1 comments
by
# Python AVL data parser

self._codec_id = int.from_bytes(raw_packet[0:1], 'big')

self._number_of_data1 = int.from_bytes(raw_packet[1:2], 'big')

print(self._codec_id) # returns 8

print(self._number_of_data1) # returns 5

----------

# now we know that we have 5 number of data (records) in the received data, so we are gonna loop the range of "number_of_data".

# this is where the problem occurs, the IO element contains x bytes, the AVL data always start with the timestamp (8 bytes) on place 2:10 in the first run,

# how will i be able to find the end_offset of this record, we know that the IO element in the first run comes from raw_packet[26:], but we need to know the end_offset

# for the io element in this record so we can continue to next record of data.

----------

for i in range(self._number_of_data1):

    timestamp = datetime.datetime.fromtimestamp(

        (int.from_bytes(raw_packet[2:10], 'big') / 1000),

        datetime.timezone.utc

    )

    io_element = StreamingAVLDataPacket.parse_io_element(raw_packet[26:])

----------

# hope someone can help me, maybe i'am looking at this the wrong way.

1 Answer

+2 votes
by anonymous

Hello,

It might easier for you to simply read all received data byte-by-byte instead of trying to segment it in the first place.

For example:

 - At the beginning of received data your code will see "0805" numbers (Indicating Codec ID = 8 and that 5 data records is received)

 - Therefore you can start 5 cycles, but do not specify at which byte they should end. E.g. you can run infinite while cycle (which would be run 5 times in the row, for 5 records)

 - Inside the cycle, read each byte one by one and parse the data. At first that would be UTC timestamp and GPS information, then I/O element part, in which there will be information how many I/O element records are present in total and how many of them are 1/2/4/8 bytes long.

 - This means that when your application would reach end of 8 bytes I/O element records (e.g. read last 8 byte I/O records, or find out, that no 8 byte records are present), it would know that this is the end of first GPS data record (out of initial 5). At this point you can break/escape the cycle, so that cycle could repeat for next GPS data record.

Hope this explanation how everything could be achieved was understandable. You probably already have similar information, but if you do not, here you can find good example how to handle AVL data records: https://wiki.teltonika-networks.com/view/RUT955_Protocols#Example

Teltonika also have made AVL data reader/parser, which you can implement into your server (source codes are available), or use them as a reference point. Applications can be downloaded from here:
https://wiki.teltonika-networks.com/wikibase/images/c/c3/Teltonika_Parser_v1.6.zip

Best answer
by anonymous
Thanks, that was very helpful :)