This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
examples.py
81 lines (71 loc) · 2.72 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import socket
import pyAISm
def decode_file_example():
"""
Example for decoding a file
:return:
"""
with open("ais.exploratorium.edu", "r") as file:
for aline in file:
try:
msg = aline.rstrip("\n")
ais_data = pyAISm.decod_ais(msg) # Return a dictionnary
ais_format = pyAISm.format_ais(ais_data) # A more human readable dictionnary
print(ais_format) # Accessing the value of the key
except pyAISm.UnrecognizedNMEAMessageError as e:
print e
except pyAISm.BadChecksumError as e:
print e
except Exception as e:
print e
print('End of file')
######################################################################################
def count_message_types_example():
"""
Example for decoding a file and counting the number of message per type
:return:
"""
with open("ais.exploratorium.edu", "r") as file:
message_types = dict()
for aline in file:
try:
msg = aline.rstrip("\n")
ais_data = pyAISm.decod_ais(msg) # Return a dictionnary
if 'type' in ais_data:
if ais_data['type']:
msg_type = ais_data['type']
if msg_type in message_types:
message_types[msg_type] += 1
else:
message_types[msg_type] = 1
except pyAISm.UnrecognizedNMEAMessageError as e:
pass
except pyAISm.BadChecksumError as e:
pass
except Exception as e:
print e
print('End of file')
print(message_types)
######################################################################################
def decode_stream_example():
"""
Example for decoding an online data stream
:return:
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("ais.exploratorium.edu", 80))
s.sendall("GET / HTTP/1.1\r\nHost: www.cnn.com\r\n\r\n".encode())
while (True):
msg = (s.recv(4096).decode('utf-8')).splitlines()
for m in msg:
try:
msg = m.rstrip("\n")
ais_data = pyAISm.decod_ais(msg) # Return a dictionnary
ais_format = pyAISm.format_ais(ais_data) # A more human readable dictionnary
print(ais_format) # Accessing the value of the key
except pyAISm.UnrecognizedNMEAMessageError as e:
print e
except pyAISm.BadChecksumError as e:
print e
except Exception as e:
print e