1
2
3 __version__ = '$Revision: 4791 $'.split()[1]
4 __date__ = '$Date: 2007-12-04 $'.split()[1]
5 __author__ = 'xmlbinmsg'
6
7 __doc__='''
8
9 Autogenerated python functions to serialize/deserialize binary messages.
10
11 Generated by: ./aisxmlbinmsg2py.py
12
13 Need to then wrap these functions with the outer AIS packet and then
14 convert the whole binary blob to a NMEA string. Those functions are
15 not currently provided in this file.
16
17 serialize: python to ais binary
18 deserialize: ais binary to python
19
20 The generated code uses translators.py, binary.py, and aisstring.py
21 which should be packaged with the resulting files.
22
23
24 @requires: U{epydoc<http://epydoc.sourceforge.net/>} > 3.0alpha3
25 @requires: U{BitVector<http://cheeseshop.python.org/pypi/BitVector>}
26
27 @author: '''+__author__+'''
28 @version: ''' + __version__ +'''
29 @var __date__: Date of last svn commit
30 @undocumented: __version__ __author__ __doc__ parser
31 @status: under development
32 @license: Generated code has no license
33 @todo: FIX: put in a description of the message here with fields and types.
34 '''
35
36 import sys
37 from decimal import Decimal
38 from BitVector import BitVector
39
40 import binary, aisstring
41
42
43 TrueBV = BitVector(bitstring="1")
44 "Why always rebuild the True bit? This should speed things up a bunch"
45 FalseBV = BitVector(bitstring="0")
46 "Why always rebuild the False bit? This should speed things up a bunch"
47
48
49 fieldList = (
50 'MessageID',
51 'RepeatIndicator',
52 'UserID',
53 'Spare',
54 'DestID1',
55 'SeqID1',
56 'DestID2',
57 'SeqID2',
58 'DestID3',
59 'SeqID3',
60 'DestID4',
61 'SeqID4',
62 )
63
64 fieldListPostgres = (
65 'MessageID',
66 'RepeatIndicator',
67 'UserID',
68 'Spare',
69 'DestID1',
70 'SeqID1',
71 'DestID2',
72 'SeqID2',
73 'DestID3',
74 'SeqID3',
75 'DestID4',
76 'SeqID4',
77 )
78
79 toPgFields = {
80 }
81 '''
82 Go to the Postgis field names from the straight field name
83 '''
84
85 fromPgFields = {
86 }
87 '''
88 Go from the Postgis field names to the straight field name
89 '''
90
91 pgTypes = {
92 }
93 '''
94 Lookup table for each postgis field name to get its type.
95 '''
96
97 -def encode(params, validate=False):
98 '''Create a binack binary message payload to pack into an AIS Msg binack.
99
100 Fields in params:
101 - MessageID(uint): AIS message number. Must be 7 (field automatically set to "7")
102 - RepeatIndicator(uint): Indicated how many times a message has been repeated
103 - UserID(uint): Unique ship identification number (MMSI). Also known as the Source ID
104 - Spare(uint): Not used. Should be set to zero. (field automatically set to "0")
105 - DestID1(uint): MMSI destication to ACK
106 - SeqID1(uint): Sequence ID of the message to be acknowledged
107 - DestID2(uint): MMSI destication to ACK
108 - SeqID2(uint): Sequence ID of the message to be acknowledged
109 - DestID3(uint): MMSI destication to ACK
110 - SeqID3(uint): Sequence ID of the message to be acknowledged
111 - DestID4(uint): MMSI destication to ACK
112 - SeqID4(uint): Sequence ID of the message to be acknowledged
113 @param params: Dictionary of field names/values. Throws a ValueError exception if required is missing
114 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented.
115 @rtype: BitVector
116 @return: encoded binary message (for binary messages, this needs to be wrapped in a msg 8
117 @note: The returned bits may not be 6 bit aligned. It is up to you to pad out the bits.
118 '''
119
120 bvList = []
121 bvList.append(binary.setBitVectorSize(BitVector(intVal=7),6))
122 if 'RepeatIndicator' in params:
123 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['RepeatIndicator']),2))
124 else:
125 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),2))
126 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['UserID']),30))
127 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),1))
128 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['DestID1']),30))
129 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['SeqID1']),2))
130 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['DestID2']),30))
131 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['SeqID2']),2))
132 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['DestID3']),30))
133 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['SeqID3']),2))
134 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['DestID4']),30))
135 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['SeqID4']),2))
136
137 return binary.joinBV(bvList)
138
139 -def decode(bv, validate=False):
140 '''Unpack a binack message
141
142 Fields in params:
143 - MessageID(uint): AIS message number. Must be 7 (field automatically set to "7")
144 - RepeatIndicator(uint): Indicated how many times a message has been repeated
145 - UserID(uint): Unique ship identification number (MMSI). Also known as the Source ID
146 - Spare(uint): Not used. Should be set to zero. (field automatically set to "0")
147 - DestID1(uint): MMSI destication to ACK
148 - SeqID1(uint): Sequence ID of the message to be acknowledged
149 - DestID2(uint): MMSI destication to ACK
150 - SeqID2(uint): Sequence ID of the message to be acknowledged
151 - DestID3(uint): MMSI destication to ACK
152 - SeqID3(uint): Sequence ID of the message to be acknowledged
153 - DestID4(uint): MMSI destication to ACK
154 - SeqID4(uint): Sequence ID of the message to be acknowledged
155 @type bv: BitVector
156 @param bv: Bits defining a message
157 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented.
158 @rtype: dict
159 @return: params
160 '''
161
162
163
164
165 r = {}
166 r['MessageID']=7
167 r['RepeatIndicator']=int(bv[6:8])
168 r['UserID']=int(bv[8:38])
169 r['Spare']=0
170 r['DestID1']=int(bv[39:69])
171 r['SeqID1']=int(bv[69:71])
172 r['DestID2']=int(bv[71:101])
173 r['SeqID2']=int(bv[101:103])
174 r['DestID3']=int(bv[103:133])
175 r['SeqID3']=int(bv[133:135])
176 r['DestID4']=int(bv[135:165])
177 r['SeqID4']=int(bv[165:167])
178 return r
179
182
185
188
191
193 return int(bv[39:69])
194
196 return int(bv[69:71])
197
199 return int(bv[71:101])
200
202 return int(bv[101:103])
203
205 return int(bv[103:133])
206
208 return int(bv[133:135])
209
211 return int(bv[135:165])
212
214 return int(bv[165:167])
215
216
218 out.write("<h3>binack</h3>\n")
219 out.write("<table border=\"1\">\n")
220 out.write("<tr bgcolor=\"orange\">\n")
221 out.write("<th align=\"left\">Field Name</th>\n")
222 out.write("<th align=\"left\">Type</th>\n")
223 out.write("<th align=\"left\">Value</th>\n")
224 out.write("<th align=\"left\">Value in Lookup Table</th>\n")
225 out.write("<th align=\"left\">Units</th>\n")
226 out.write("\n")
227 out.write("<tr>\n")
228 out.write("<td>MessageID</td>\n")
229 out.write("<td>uint</td>\n")
230 if 'MessageID' in params:
231 out.write(" <td>"+str(params['MessageID'])+"</td>\n")
232 out.write(" <td>"+str(params['MessageID'])+"</td>\n")
233 out.write("</tr>\n")
234 out.write("\n")
235 out.write("<tr>\n")
236 out.write("<td>RepeatIndicator</td>\n")
237 out.write("<td>uint</td>\n")
238 if 'RepeatIndicator' in params:
239 out.write(" <td>"+str(params['RepeatIndicator'])+"</td>\n")
240 if str(params['RepeatIndicator']) in RepeatIndicatorDecodeLut:
241 out.write("<td>"+RepeatIndicatorDecodeLut[str(params['RepeatIndicator'])]+"</td>")
242 else:
243 out.write("<td><i>Missing LUT entry</i></td>")
244 out.write("</tr>\n")
245 out.write("\n")
246 out.write("<tr>\n")
247 out.write("<td>UserID</td>\n")
248 out.write("<td>uint</td>\n")
249 if 'UserID' in params:
250 out.write(" <td>"+str(params['UserID'])+"</td>\n")
251 out.write(" <td>"+str(params['UserID'])+"</td>\n")
252 out.write("</tr>\n")
253 out.write("\n")
254 out.write("<tr>\n")
255 out.write("<td>Spare</td>\n")
256 out.write("<td>uint</td>\n")
257 if 'Spare' in params:
258 out.write(" <td>"+str(params['Spare'])+"</td>\n")
259 out.write(" <td>"+str(params['Spare'])+"</td>\n")
260 out.write("</tr>\n")
261 out.write("\n")
262 out.write("<tr>\n")
263 out.write("<td>DestID1</td>\n")
264 out.write("<td>uint</td>\n")
265 if 'DestID1' in params:
266 out.write(" <td>"+str(params['DestID1'])+"</td>\n")
267 out.write(" <td>"+str(params['DestID1'])+"</td>\n")
268 out.write("</tr>\n")
269 out.write("\n")
270 out.write("<tr>\n")
271 out.write("<td>SeqID1</td>\n")
272 out.write("<td>uint</td>\n")
273 if 'SeqID1' in params:
274 out.write(" <td>"+str(params['SeqID1'])+"</td>\n")
275 out.write(" <td>"+str(params['SeqID1'])+"</td>\n")
276 out.write("</tr>\n")
277 out.write("\n")
278 out.write("<tr>\n")
279 out.write("<td>DestID2</td>\n")
280 out.write("<td>uint</td>\n")
281 if 'DestID2' in params:
282 out.write(" <td>"+str(params['DestID2'])+"</td>\n")
283 out.write(" <td>"+str(params['DestID2'])+"</td>\n")
284 out.write("</tr>\n")
285 out.write("\n")
286 out.write("<tr>\n")
287 out.write("<td>SeqID2</td>\n")
288 out.write("<td>uint</td>\n")
289 if 'SeqID2' in params:
290 out.write(" <td>"+str(params['SeqID2'])+"</td>\n")
291 out.write(" <td>"+str(params['SeqID2'])+"</td>\n")
292 out.write("</tr>\n")
293 out.write("\n")
294 out.write("<tr>\n")
295 out.write("<td>DestID3</td>\n")
296 out.write("<td>uint</td>\n")
297 if 'DestID3' in params:
298 out.write(" <td>"+str(params['DestID3'])+"</td>\n")
299 out.write(" <td>"+str(params['DestID3'])+"</td>\n")
300 out.write("</tr>\n")
301 out.write("\n")
302 out.write("<tr>\n")
303 out.write("<td>SeqID3</td>\n")
304 out.write("<td>uint</td>\n")
305 if 'SeqID3' in params:
306 out.write(" <td>"+str(params['SeqID3'])+"</td>\n")
307 out.write(" <td>"+str(params['SeqID3'])+"</td>\n")
308 out.write("</tr>\n")
309 out.write("\n")
310 out.write("<tr>\n")
311 out.write("<td>DestID4</td>\n")
312 out.write("<td>uint</td>\n")
313 if 'DestID4' in params:
314 out.write(" <td>"+str(params['DestID4'])+"</td>\n")
315 out.write(" <td>"+str(params['DestID4'])+"</td>\n")
316 out.write("</tr>\n")
317 out.write("\n")
318 out.write("<tr>\n")
319 out.write("<td>SeqID4</td>\n")
320 out.write("<td>uint</td>\n")
321 if 'SeqID4' in params:
322 out.write(" <td>"+str(params['SeqID4'])+"</td>\n")
323 out.write(" <td>"+str(params['SeqID4'])+"</td>\n")
324 out.write("</tr>\n")
325 out.write("</table>\n")
326
327 -def printFields(params, out=sys.stdout, format='std', fieldList=None, dbType='postgres'):
328 '''Print a binack message to stdout.
329
330 Fields in params:
331 - MessageID(uint): AIS message number. Must be 7 (field automatically set to "7")
332 - RepeatIndicator(uint): Indicated how many times a message has been repeated
333 - UserID(uint): Unique ship identification number (MMSI). Also known as the Source ID
334 - Spare(uint): Not used. Should be set to zero. (field automatically set to "0")
335 - DestID1(uint): MMSI destication to ACK
336 - SeqID1(uint): Sequence ID of the message to be acknowledged
337 - DestID2(uint): MMSI destication to ACK
338 - SeqID2(uint): Sequence ID of the message to be acknowledged
339 - DestID3(uint): MMSI destication to ACK
340 - SeqID3(uint): Sequence ID of the message to be acknowledged
341 - DestID4(uint): MMSI destication to ACK
342 - SeqID4(uint): Sequence ID of the message to be acknowledged
343 @param params: Dictionary of field names/values.
344 @param out: File like object to write to
345 @rtype: stdout
346 @return: text to out
347 '''
348
349 if 'std'==format:
350 out.write("binack:\n")
351 if 'MessageID' in params: out.write(" MessageID: "+str(params['MessageID'])+"\n")
352 if 'RepeatIndicator' in params: out.write(" RepeatIndicator: "+str(params['RepeatIndicator'])+"\n")
353 if 'UserID' in params: out.write(" UserID: "+str(params['UserID'])+"\n")
354 if 'Spare' in params: out.write(" Spare: "+str(params['Spare'])+"\n")
355 if 'DestID1' in params: out.write(" DestID1: "+str(params['DestID1'])+"\n")
356 if 'SeqID1' in params: out.write(" SeqID1: "+str(params['SeqID1'])+"\n")
357 if 'DestID2' in params: out.write(" DestID2: "+str(params['DestID2'])+"\n")
358 if 'SeqID2' in params: out.write(" SeqID2: "+str(params['SeqID2'])+"\n")
359 if 'DestID3' in params: out.write(" DestID3: "+str(params['DestID3'])+"\n")
360 if 'SeqID3' in params: out.write(" SeqID3: "+str(params['SeqID3'])+"\n")
361 if 'DestID4' in params: out.write(" DestID4: "+str(params['DestID4'])+"\n")
362 if 'SeqID4' in params: out.write(" SeqID4: "+str(params['SeqID4'])+"\n")
363 elif 'csv'==format:
364 if None == options.fieldList:
365 options.fieldList = fieldList
366 needComma = False;
367 for field in fieldList:
368 if needComma: out.write(',')
369 needComma = True
370 if field in params:
371 out.write(str(params[field]))
372
373 out.write("\n")
374 elif 'html'==format:
375 printHtml(params,out)
376 elif 'sql'==format:
377 sqlInsertStr(params,out,dbType=dbType)
378 else:
379 print "ERROR: unknown format:",format
380 assert False
381
382 return
383
384 RepeatIndicatorEncodeLut = {
385 'default':'0',
386 'do not repeat any more':'3',
387 }
388
389 RepeatIndicatorDecodeLut = {
390 '0':'default',
391 '3':'do not repeat any more',
392 }
393
394
395
396
397
398 -def sqlCreateStr(outfile=sys.stdout, fields=None, extraFields=None
399 ,addCoastGuardFields=True
400 ,dbType='postgres'
401 ):
402 '''
403 Return the SQL CREATE command for this message type
404 @param outfile: file like object to print to.
405 @param fields: which fields to put in the create. Defaults to all.
406 @param extraFields: A sequence of tuples containing (name,sql type) for additional fields
407 @param addCoastGuardFields: Add the extra fields that come after the NMEA check some from the USCG N-AIS format
408 @param dbType: Which flavor of database we are using so that the create is tailored ('sqlite' or 'postgres')
409 @type addCoastGuardFields: bool
410 @return: sql create string
411 @rtype: str
412
413 @see: sqlCreate
414 '''
415
416 outfile.write(str(sqlCreate(fields,extraFields,addCoastGuardFields,dbType=dbType)))
417
418 -def sqlCreate(fields=None, extraFields=None, addCoastGuardFields=True, dbType='postgres'):
419 '''
420 Return the sqlhelp object to create the table.
421
422 @param fields: which fields to put in the create. Defaults to all.
423 @param extraFields: A sequence of tuples containing (name,sql type) for additional fields
424 @param addCoastGuardFields: Add the extra fields that come after the NMEA check some from the USCG N-AIS format
425 @type addCoastGuardFields: bool
426 @param dbType: Which flavor of database we are using so that the create is tailored ('sqlite' or 'postgres')
427 @return: An object that can be used to generate a return
428 @rtype: sqlhelp.create
429 '''
430 if None == fields: fields = fieldList
431 import sqlhelp
432 c = sqlhelp.create('binack',dbType=dbType)
433 c.addPrimaryKey()
434 if 'MessageID' in fields: c.addInt ('MessageID')
435 if 'RepeatIndicator' in fields: c.addInt ('RepeatIndicator')
436 if 'UserID' in fields: c.addInt ('UserID')
437 if 'Spare' in fields: c.addInt ('Spare')
438 if 'DestID1' in fields: c.addInt ('DestID1')
439 if 'SeqID1' in fields: c.addInt ('SeqID1')
440 if 'DestID2' in fields: c.addInt ('DestID2')
441 if 'SeqID2' in fields: c.addInt ('SeqID2')
442 if 'DestID3' in fields: c.addInt ('DestID3')
443 if 'SeqID3' in fields: c.addInt ('SeqID3')
444 if 'DestID4' in fields: c.addInt ('DestID4')
445 if 'SeqID4' in fields: c.addInt ('SeqID4')
446
447 if addCoastGuardFields:
448
449
450
451
452
453 c.addVarChar('cg_r',15)
454 c.addInt('cg_sec')
455
456 c.addTimestamp('cg_timestamp')
457
458 return c
459
460 -def sqlInsertStr(params, outfile=sys.stdout, extraParams=None, dbType='postgres'):
461 '''
462 Return the SQL INSERT command for this message type
463 @param params: dictionary of values keyed by field name
464 @param outfile: file like object to print to.
465 @param extraParams: A sequence of tuples containing (name,sql type) for additional fields
466 @return: sql create string
467 @rtype: str
468
469 @see: sqlCreate
470 '''
471 outfile.write(str(sqlInsert(params,extraParams,dbType=dbType)))
472
473
474 -def sqlInsert(params,extraParams=None,dbType='postgres'):
475 '''
476 Give the SQL INSERT statement
477 @param params: dict keyed by field name of values
478 @param extraParams: any extra fields that you have created beyond the normal ais message fields
479 @rtype: sqlhelp.insert
480 @return: insert class instance
481 @todo: allow optional type checking of params?
482 @warning: this will take invalid keys happily and do what???
483 '''
484 import sqlhelp
485 i = sqlhelp.insert('binack',dbType=dbType)
486
487 if dbType=='postgres':
488 finished = []
489 for key in params:
490 if key in finished:
491 continue
492
493 if key not in toPgFields and key not in fromPgFields:
494 if type(params[key])==Decimal: i.add(key,float(params[key]))
495 else: i.add(key,params[key])
496 else:
497 if key in fromPgFields:
498 val = params[key]
499
500 i.addPostGIS(key,val)
501 finished.append(key)
502 else:
503
504 pgName = toPgFields[key]
505
506 valStr=pgTypes[pgName]+'('
507 vals = []
508 for nonPgKey in fromPgFields[pgName]:
509 vals.append(str(params[nonPgKey]))
510 finished.append(nonPgKey)
511 valStr+=' '.join(vals)+')'
512 i.addPostGIS(pgName,valStr)
513 else:
514 for key in params:
515 if type(params[key])==Decimal: i.add(key,float(params[key]))
516 else: i.add(key,params[key])
517
518 if None != extraParams:
519 for key in extraParams:
520 i.add(key,extraParams[key])
521
522 return i
523
524
525
526
527
530 '''
531 Return the LaTeX definition table for this message type
532 @param outfile: file like object to print to.
533 @type outfile: file obj
534 @return: LaTeX table string via the outfile
535 @rtype: str
536
537 '''
538 o = outfile
539
540 o.write('''
541 \\begin{table}%[htb]
542 \\centering
543 \\begin{tabular}{|l|c|l|}
544 \\hline
545 Parameter & Number of bits & Description
546 \\\\ \\hline\\hline
547 MessageID & 6 & AIS message number. Must be 7 \\\\ \hline
548 RepeatIndicator & 2 & Indicated how many times a message has been repeated \\\\ \hline
549 UserID & 30 & Unique ship identification number (MMSI). Also known as the Source ID \\\\ \hline
550 Spare & 1 & Not used. Should be set to zero. \\\\ \hline
551 DestID1 & 30 & MMSI destication to ACK \\\\ \hline
552 SeqID1 & 2 & Sequence ID of the message to be acknowledged \\\\ \hline
553 DestID2 & 30 & MMSI destication to ACK \\\\ \hline
554 SeqID2 & 2 & Sequence ID of the message to be acknowledged \\\\ \hline
555 DestID3 & 30 & MMSI destication to ACK \\\\ \hline
556 SeqID3 & 2 & Sequence ID of the message to be acknowledged \\\\ \hline
557 DestID4 & 30 & MMSI destication to ACK \\\\ \hline
558 SeqID4 & 2 & Sequence ID of the message to be acknowledged\\\\ \\hline \\hline
559 Total bits & 167 & Appears to take 1 slot with 1 pad bits to fill the last slot \\\\ \\hline
560 \\end{tabular}
561 \\caption{AIS message number 8: Binary acknowledgement of addressed message}
562 \\label{tab:binack}
563 \\end{table}
564 ''')
565
566
567
568
569
570 -def textDefinitionTable(outfile=sys.stdout
571 ,delim='\t'
572 ):
573 '''
574 Return the text definition table for this message type
575 @param outfile: file like object to print to.
576 @type outfile: file obj
577 @return: text table string via the outfile
578 @rtype: str
579
580 '''
581 o = outfile
582 o.write('''Parameter'''+delim+'Number of bits'''+delim+'''Description
583 MessageID'''+delim+'''6'''+delim+'''AIS message number. Must be 7
584 RepeatIndicator'''+delim+'''2'''+delim+'''Indicated how many times a message has been repeated
585 UserID'''+delim+'''30'''+delim+'''Unique ship identification number (MMSI). Also known as the Source ID
586 Spare'''+delim+'''1'''+delim+'''Not used. Should be set to zero.
587 DestID1'''+delim+'''30'''+delim+'''MMSI destication to ACK
588 SeqID1'''+delim+'''2'''+delim+'''Sequence ID of the message to be acknowledged
589 DestID2'''+delim+'''30'''+delim+'''MMSI destication to ACK
590 SeqID2'''+delim+'''2'''+delim+'''Sequence ID of the message to be acknowledged
591 DestID3'''+delim+'''30'''+delim+'''MMSI destication to ACK
592 SeqID3'''+delim+'''2'''+delim+'''Sequence ID of the message to be acknowledged
593 DestID4'''+delim+'''30'''+delim+'''MMSI destication to ACK
594 SeqID4'''+delim+'''2'''+delim+'''Sequence ID of the message to be acknowledged
595 Total bits'''+delim+'''167'''+delim+'''Appears to take 1 slot with 1 pad bits to fill the last slot''')
596
597
598
599
600
601 import unittest
603 '''Return a params file base on the testvalue tags.
604 @rtype: dict
605 @return: params based on testvalue tags
606 '''
607 params = {}
608 params['MessageID'] = 7
609 params['RepeatIndicator'] = 1
610 params['UserID'] = 1193046
611 params['Spare'] = 0
612 params['DestID1'] = 1193001
613 params['SeqID1'] = 1
614 params['DestID2'] = 1193002
615 params['SeqID2'] = 2
616 params['DestID3'] = 1193003
617 params['SeqID3'] = 3
618 params['DestID4'] = 1193004
619 params['SeqID4'] = 0
620
621 return params
622
624 '''Use testvalue tag text from each type to build test case the binack message'''
626
627 params = testParams()
628 bits = encode(params)
629 r = decode(bits)
630
631
632 self.failUnlessEqual(r['MessageID'],params['MessageID'])
633 self.failUnlessEqual(r['RepeatIndicator'],params['RepeatIndicator'])
634 self.failUnlessEqual(r['UserID'],params['UserID'])
635 self.failUnlessEqual(r['Spare'],params['Spare'])
636 self.failUnlessEqual(r['DestID1'],params['DestID1'])
637 self.failUnlessEqual(r['SeqID1'],params['SeqID1'])
638 self.failUnlessEqual(r['DestID2'],params['DestID2'])
639 self.failUnlessEqual(r['SeqID2'],params['SeqID2'])
640 self.failUnlessEqual(r['DestID3'],params['DestID3'])
641 self.failUnlessEqual(r['SeqID3'],params['SeqID3'])
642 self.failUnlessEqual(r['DestID4'],params['DestID4'])
643 self.failUnlessEqual(r['SeqID4'],params['SeqID4'])
644
646 parser.add_option('-d','--decode',dest='doDecode',default=False,action='store_true',
647 help='decode a "binack" AIS message')
648 parser.add_option('-e','--encode',dest='doEncode',default=False,action='store_true',
649 help='encode a "binack" AIS message')
650 parser.add_option('--RepeatIndicator-field', dest='RepeatIndicatorField',default=0,metavar='uint',type='int'
651 ,help='Field parameter value [default: %default]')
652 parser.add_option('--UserID-field', dest='UserIDField',metavar='uint',type='int'
653 ,help='Field parameter value [default: %default]')
654 parser.add_option('--DestID1-field', dest='DestID1Field',metavar='uint',type='int'
655 ,help='Field parameter value [default: %default]')
656 parser.add_option('--SeqID1-field', dest='SeqID1Field',metavar='uint',type='int'
657 ,help='Field parameter value [default: %default]')
658 parser.add_option('--DestID2-field', dest='DestID2Field',metavar='uint',type='int'
659 ,help='Field parameter value [default: %default]')
660 parser.add_option('--SeqID2-field', dest='SeqID2Field',metavar='uint',type='int'
661 ,help='Field parameter value [default: %default]')
662 parser.add_option('--DestID3-field', dest='DestID3Field',metavar='uint',type='int'
663 ,help='Field parameter value [default: %default]')
664 parser.add_option('--SeqID3-field', dest='SeqID3Field',metavar='uint',type='int'
665 ,help='Field parameter value [default: %default]')
666 parser.add_option('--DestID4-field', dest='DestID4Field',metavar='uint',type='int'
667 ,help='Field parameter value [default: %default]')
668 parser.add_option('--SeqID4-field', dest='SeqID4Field',metavar='uint',type='int'
669 ,help='Field parameter value [default: %default]')
670
671
672 if __name__=='__main__':
673
674 from optparse import OptionParser
675 parser = OptionParser(usage="%prog [options]",
676 version="%prog "+__version__)
677
678 parser.add_option('--doc-test',dest='doctest',default=False,action='store_true',
679 help='run the documentation tests')
680 parser.add_option('--unit-test',dest='unittest',default=False,action='store_true',
681 help='run the unit tests')
682 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true',
683 help='Make the test output verbose')
684
685
686
687 typeChoices = ('binary','nmeapayload','nmea')
688 parser.add_option('-t','--type',choices=typeChoices,type='choice',dest='ioType'
689 ,default='nmeapayload'
690 ,help='What kind of string to write for encoding ('+', '.join(typeChoices)+') [default: %default]')
691
692
693 outputChoices = ('std','html','csv','sql' )
694 parser.add_option('-T','--output-type',choices=outputChoices,type='choice',dest='outputType'
695 ,default='std'
696 ,help='What kind of string to output ('+', '.join(outputChoices)+') [default: %default]')
697
698 parser.add_option('-o','--output',dest='outputFileName',default=None,
699 help='Name of the python file to write [default: stdout]')
700
701 parser.add_option('-f','--fields',dest='fieldList',default=None, action='append',
702 choices=fieldList,
703 help='Which fields to include in the output. Currently only for csv output [default: all]')
704
705 parser.add_option('-p','--print-csv-field-list',dest='printCsvfieldList',default=False,action='store_true',
706 help='Print the field name for csv')
707
708 parser.add_option('-c','--sql-create',dest='sqlCreate',default=False,action='store_true',
709 help='Print out an sql create command for the table.')
710
711 parser.add_option('--latex-table',dest='latexDefinitionTable',default=False,action='store_true',
712 help='Print a LaTeX table of the type')
713
714 parser.add_option('--text-table',dest='textDefinitionTable',default=False,action='store_true',
715 help='Print delimited table of the type (for Word table importing)')
716 parser.add_option('--delimt-text-table',dest='delimTextDefinitionTable',default='\t'
717 ,help='Delimiter for text table [default: \'%default\'](for Word table importing)')
718
719
720 dbChoices = ('sqlite','postgres')
721 parser.add_option('-D','--db-type',dest='dbType',default='postgres'
722 ,choices=dbChoices,type='choice'
723 ,help='What kind of database ('+', '.join(dbChoices)+') [default: %default]')
724
725 addMsgOptions(parser)
726
727 (options,args) = parser.parse_args()
728 success=True
729
730 if options.doctest:
731 import os; print os.path.basename(sys.argv[0]), 'doctests ...',
732 sys.argv= [sys.argv[0]]
733 if options.verbose: sys.argv.append('-v')
734 import doctest
735 numfail,numtests=doctest.testmod()
736 if numfail==0: print 'ok'
737 else:
738 print 'FAILED'
739 success=False
740
741 if not success: sys.exit('Something Failed')
742 del success
743
744 if options.unittest:
745 sys.argv = [sys.argv[0]]
746 if options.verbose: sys.argv.append('-v')
747 unittest.main()
748
749 outfile = sys.stdout
750 if None!=options.outputFileName:
751 outfile = file(options.outputFileName,'w')
752
753
754 if options.doEncode:
755
756 if None==options.RepeatIndicatorField: parser.error("missing value for RepeatIndicatorField")
757 if None==options.UserIDField: parser.error("missing value for UserIDField")
758 if None==options.DestID1Field: parser.error("missing value for DestID1Field")
759 if None==options.SeqID1Field: parser.error("missing value for SeqID1Field")
760 if None==options.DestID2Field: parser.error("missing value for DestID2Field")
761 if None==options.SeqID2Field: parser.error("missing value for SeqID2Field")
762 if None==options.DestID3Field: parser.error("missing value for DestID3Field")
763 if None==options.SeqID3Field: parser.error("missing value for SeqID3Field")
764 if None==options.DestID4Field: parser.error("missing value for DestID4Field")
765 if None==options.SeqID4Field: parser.error("missing value for SeqID4Field")
766 msgDict={
767 'MessageID': '7',
768 'RepeatIndicator': options.RepeatIndicatorField,
769 'UserID': options.UserIDField,
770 'Spare': '0',
771 'DestID1': options.DestID1Field,
772 'SeqID1': options.SeqID1Field,
773 'DestID2': options.DestID2Field,
774 'SeqID2': options.SeqID2Field,
775 'DestID3': options.DestID3Field,
776 'SeqID3': options.SeqID3Field,
777 'DestID4': options.DestID4Field,
778 'SeqID4': options.SeqID4Field,
779 }
780
781 bits = encode(msgDict)
782 if 'binary'==options.ioType: print str(bits)
783 elif 'nmeapayload'==options.ioType:
784
785 print "bitLen",len(bits)
786 bitLen=len(bits)
787 if bitLen%6!=0:
788 bits = bits + BitVector(size=(6 - (bitLen%6)))
789 print "result:",binary.bitvectoais6(bits)[0]
790
791
792
793 elif 'nmea'==options.ioType: sys.exit("FIX: need to implement this capability")
794 else: sys.exit('ERROR: unknown ioType. Help!')
795
796
797 if options.sqlCreate:
798 sqlCreateStr(outfile,options.fieldList,dbType=options.dbType)
799
800 if options.latexDefinitionTable:
801 latexDefinitionTable(outfile)
802
803
804 if options.textDefinitionTable:
805 textDefinitionTable(outfile,options.delimTextDefinitionTable)
806
807 if options.printCsvfieldList:
808
809 if None == options.fieldList: options.fieldList = fieldList
810 import StringIO
811 buf = StringIO.StringIO()
812 for field in options.fieldList:
813 buf.write(field+',')
814 result = buf.getvalue()
815 if result[-1] == ',': print result[:-1]
816 else: print result
817
818 if options.doDecode:
819 if len(args)==0: args = sys.stdin
820 for msg in args:
821 bv = None
822
823 if msg[0] in ('$','!') and msg[3:6] in ('VDM','VDO'):
824
825
826 bv = binary.ais6tobitvec(msg.split(',')[5])
827 else:
828
829 binaryMsg=True
830 for c in msg:
831 if c not in ('0','1'):
832 binaryMsg=False
833 break
834 if binaryMsg:
835 bv = BitVector(bitstring=msg)
836 else:
837 bv = binary.ais6tobitvec(msg)
838
839 printFields(decode(bv)
840 ,out=outfile
841 ,format=options.outputType
842 ,fieldList=options.fieldList
843 ,dbType=options.dbType
844 )
845