Debian Codegraph @

ais/nmea_queue.py

raw symbols SCIP
1 """Manage a stream of NMEA messages with optional metadata.
2
3 TODO(schwehr): Add support for decoding non-AIS NMEA messages (e.g. ZDA).
4 """
5
6 from ais import nmea
7 from ais import tag_block
8 from ais import uscg
9 from ais import vdm
10 import six.moves.queue as Queue
11
12
13 class Error(Exception):
14 pass
15
16
17 def GetOrNone(queue):
18 try:
19 return queue.get(block=False)
20 except Queue.Empty:
21 return
22
23
24 class NmeaQueue(Queue.Queue):
25 # pylint: disable=line-too-long
26 r"""Process mixed text, bare NMEA or NMEA with TAG BLOCK or USCG metadata.
27
28 Raw text lines are passed through without a decode stage. "Bare"
29 AIS NMEA lines are decoded and passed through without a decode or
30 time field in resulting decodes. The USCG and TAG Block metadata
31 messages will get a list of times and a "decoded" field. The
32 decoded field contains the AIS message contents. The extra metadata is
33 expected to be accessed through the matches field.
34
35 Example usage:
36
37 q = nmea_queue.NmeaQueue()
38 q.qsize() # Returns 0
39
40 # Raw text.
41 q.put('Hello world')
42 q.qsize() # Returns 1
43 q.get() # Returns a dictionary with lines containing the input.
44 q.qsize() # Returns 0
45
46 # Bare NMEA AIS VDM.
47 q.put('!SAVDM,2,1,9,B,55NG9T000001L@GKCSPLDlTpV22222222222220t2PI5540Ht00`88888888,0*33')
48 q.put('!SAVDM,2,2,9,B,88888888880,2*34')
49 q.get() # Returns a dictionary with no times or extra metadata in matches.
50
51 # USCG NMEA AIS VDM.
52 q.put('!SAVDM,2,1,9,B,55NG9T000001L@GKCSPLDlTpV22222222222220t2PI5540Ht00`88888888,0*33,b003669709,1428944320')
53 q.put('!SAVDM,2,2,9,B,88888888880,2*34,b003669709,1428944320')
54 q.get() # Returns a dictionary with times and extra metadata.
55
56 # TAG Block NMEA AIS VDM.
57 q.put('\g:1-3-144865,n:325372,s:r11CSDO1,c:1428944580*6C\!AIVDM,2,1,9,A,55MkD8000001L@7?37L4AHDqAE8F0Pu8p8huLE8t28P,0*1E')
58 q.put('\g:2-3-144865,n:325373*2D\!AIVDM,2,2,9,A,666v<04ThC`12AAkp0hH88888880,2*7A')
59 q.get() # Return a dictionary with times and extra metadata.
60
61
62 This sample line is a single line TAG Block position report intended
63 for a satellite:
64
65 \n:80677,s:b003669952,c:1428884269*2A\!SAVDM,1,1,,B,K8VSqb9LdU28WP8<,0*17
66
67 A queue get() returns:
68
69 {'decoded': {
70 'cog': 131,
71 'gnss': True,
72 'id': 27,
73 'md5': '50898a3435865cf76f1b502b2821672b',
74 'mmsi': 577305000,
75 'nav_status': 5,
76 'position_accuracy': 1,
77 'raim': False,
78 'repeat_indicator': 0,
79 'sog': 0,
80 'spare': 0,
81 'x': -90.2066650390625,
82 'y': 29.145000457763672},
83 'line_nums': [9],
84 'line_type': ('TAGB',),
85 'lines': [
86 '\\n:80677,s:b003669952,c:1428884269*2A'
87 '\\!SAVDM,1,1,,B,K8VSqb9LdU28WP8<,0*17'],
88 'matches': [{
89 'dest': None,
90 'group': None,
91 'group_id': None,
92 'line_num': '80677',
93 'metadata': 'n:80677,s:b003669952,c:1428884269*2A',
94 'payload': '!SAVDM,1,1,,B,K8VSqb9LdU28WP8<,0*17',
95 'quality': None,
96 'rcvr': 'b003669952',
97 'rel_time': None,
98 'sentence_num': None,
99 'sentence_tot': None,
100 'tag_checksum': '2A',
101 'text': None,
102 'text_date': None,
103 'time': '1428884269'}],
104 'times': [1428884269]})
105 """
106
107 def __init__(self):
108 self.bare_queue = vdm.BareQueue()
109 self.tagb_queue = tag_block.TagQueue()
110 self.uscg_queue = uscg.UscgQueue()
111 self.line_num = 0
112 Queue.Queue.__init__(self)
113
114 def put(self, line, line_num=None):
115 """Add a line to the queue.
116
117 Args:
118 line: str, Text to add to the queue.
119 line_num: int, Optional line number override.
120 """
121 if line_num is not None:
122 self.line_num = line_num
123 else:
124 self.line_num += 1
125 line_num = self.line_num
126
127 line = line.rstrip()
128 line_type = nmea.LineType(line)
129
130 msg = None
131
132 if line_type == nmea.TEXT:
133 msg = {'line_nums': [self.line_num], 'lines': [line]}
134 elif line_type == nmea.BARE:
135 self.bare_queue.put(line, self.line_num)
136 msg = GetOrNone(self.bare_queue)
137 elif line_type == nmea.TAGB:
138 self.tagb_queue.put(line, self.line_num)
139 msg = GetOrNone(self.tagb_queue)
140 elif line_type == nmea.USCG:
141 self.uscg_queue.put(line, self.line_num)
142 msg = GetOrNone(self.uscg_queue)
143 else:
144 assert False # Should never reach here.
145
146 if msg:
147 msg['line_type'] = line_type
148 Queue.Queue.put(self, msg)
149
150 def GetOrNone(self):
151 try:
152 return self.get(block=False)
153 except Queue.Empty:
154 return