1
2
3 __version__ = '$Revision: 4791 $'.split()[1]
4 __date__ = '$Date: 2007-03-18 $'.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 outfile.write(str(sqlCreate(fields,extraFields,addCoastGuardFields,dbType=dbType)))
416
417 -def sqlCreate(fields=None, extraFields=None, addCoastGuardFields=True, dbType='postgres'):
418 '''
419 Return the sqlhelp object to create the table.
420
421 @param fields: which fields to put in the create. Defaults to all.
422 @param extraFields: A sequence of tuples containing (name,sql type) for additional fields
423 @param addCoastGuardFields: Add the extra fields that come after the NMEA check some from the USCG N-AIS format
424 @type addCoastGuardFields: bool
425 @param dbType: Which flavor of database we are using so that the create is tailored ('sqlite' or 'postgres')
426 @return: An object that can be used to generate a return
427 @rtype: sqlhelp.create
428 '''
429 if None == fields: fields = fieldList
430 import sqlhelp
431 c = sqlhelp.create('binack',dbType=dbType)
432 c.addPrimaryKey()
433 if 'MessageID' in fields: c.addInt ('MessageID')
434 if 'RepeatIndicator' in fields: c.addInt ('RepeatIndicator')
435 if 'UserID' in fields: c.addInt ('UserID')
436 if 'Spare' in fields: c.addInt ('Spare')
437 if 'DestID1' in fields: c.addInt ('DestID1')
438 if 'SeqID1' in fields: c.addInt ('SeqID1')
439 if 'DestID2' in fields: c.addInt ('DestID2')
440 if 'SeqID2' in fields: c.addInt ('SeqID2')
441 if 'DestID3' in fields: c.addInt ('DestID3')
442 if 'SeqID3' in fields: c.addInt ('SeqID3')
443 if 'DestID4' in fields: c.addInt ('DestID4')
444 if 'SeqID4' in fields: c.addInt ('SeqID4')
445
446 if addCoastGuardFields:
447
448
449
450
451
452 c.addVarChar('cg_r',15)
453 c.addInt('cg_sec')
454
455 c.addTimestamp('cg_timestamp')
456
457 return c
458
459 -def sqlInsertStr(params, outfile=sys.stdout, extraParams=None, dbType='postgres'):
460 '''
461 Return the SQL INSERT command for this message type
462 @param params: dictionary of values keyed by field name
463 @param outfile: file like object to print to.
464 @param extraParams: A sequence of tuples containing (name,sql type) for additional fields
465 @return: sql create string
466 @rtype: str
467
468 @see: sqlCreate
469 '''
470 outfile.write(str(sqlInsert(params,extraParams,dbType=dbType)))
471
472
473 -def sqlInsert(params,extraParams=None,dbType='postgres'):
474 '''
475 Give the SQL INSERT statement
476 @param params: dict keyed by field name of values
477 @param extraParams: any extra fields that you have created beyond the normal ais message fields
478 @rtype: sqlhelp.insert
479 @return: insert class instance
480 @todo: allow optional type checking of params?
481 @warning: this will take invalid keys happily and do what???
482 '''
483 import sqlhelp
484 i = sqlhelp.insert('binack',dbType=dbType)
485
486 if dbType=='postgres':
487 finished = []
488 for key in params:
489 if key in finished:
490 continue
491
492 if key not in toPgFields and key not in fromPgFields:
493 if type(params[key])==Decimal: i.add(key,float(params[key]))
494 else: i.add(key,params[key])
495 else:
496 if key in fromPgFields:
497 val = params[key]
498
499 i.addPostGIS(key,val)
500 finished.append(key)
501 else:
502
503 pgName = toPgFields[key]
504
505 valStr=pgTypes[pgName]+'('
506 vals = []
507 for nonPgKey in fromPgFields[pgName]:
508 vals.append(str(params[nonPgKey]))
509 finished.append(nonPgKey)
510 valStr+=' '.join(vals)+')'
511 i.addPostGIS(pgName,valStr)
512 else:
513 for key in params:
514 if type(params[key])==Decimal: i.add(key,float(params[key]))
515 else: i.add(key,params[key])
516
517 if None != extraParams:
518 for key in extraParams:
519 i.add(key,extraParams[key])
520
521 return i
522
523
524
525
526
527 import unittest
529 '''Return a params file base on the testvalue tags.
530 @rtype: dict
531 @return: params based on testvalue tags
532 '''
533 params = {}
534 params['MessageID'] = 7
535 params['RepeatIndicator'] = 1
536 params['UserID'] = 1193046
537 params['Spare'] = 0
538 params['DestID1'] = 1193001
539 params['SeqID1'] = 1
540 params['DestID2'] = 1193002
541 params['SeqID2'] = 2
542 params['DestID3'] = 1193003
543 params['SeqID3'] = 3
544 params['DestID4'] = 1193004
545 params['SeqID4'] = 0
546
547 return params
548
550 '''Use testvalue tag text from each type to build test case the binack message'''
552
553 params = testParams()
554 bits = encode(params)
555 r = decode(bits)
556
557
558 self.failUnlessEqual(r['MessageID'],params['MessageID'])
559 self.failUnlessEqual(r['RepeatIndicator'],params['RepeatIndicator'])
560 self.failUnlessEqual(r['UserID'],params['UserID'])
561 self.failUnlessEqual(r['Spare'],params['Spare'])
562 self.failUnlessEqual(r['DestID1'],params['DestID1'])
563 self.failUnlessEqual(r['SeqID1'],params['SeqID1'])
564 self.failUnlessEqual(r['DestID2'],params['DestID2'])
565 self.failUnlessEqual(r['SeqID2'],params['SeqID2'])
566 self.failUnlessEqual(r['DestID3'],params['DestID3'])
567 self.failUnlessEqual(r['SeqID3'],params['SeqID3'])
568 self.failUnlessEqual(r['DestID4'],params['DestID4'])
569 self.failUnlessEqual(r['SeqID4'],params['SeqID4'])
570
572 parser.add_option('-d','--decode',dest='doDecode',default=False,action='store_true',
573 help='decode a "binack" AIS message')
574 parser.add_option('-e','--encode',dest='doEncode',default=False,action='store_true',
575 help='encode a "binack" AIS message')
576 parser.add_option('--RepeatIndicator-field', dest='RepeatIndicatorField',default=0,metavar='uint',type='int'
577 ,help='Field parameter value [default: %default]')
578 parser.add_option('--UserID-field', dest='UserIDField',metavar='uint',type='int'
579 ,help='Field parameter value [default: %default]')
580 parser.add_option('--DestID1-field', dest='DestID1Field',metavar='uint',type='int'
581 ,help='Field parameter value [default: %default]')
582 parser.add_option('--SeqID1-field', dest='SeqID1Field',metavar='uint',type='int'
583 ,help='Field parameter value [default: %default]')
584 parser.add_option('--DestID2-field', dest='DestID2Field',metavar='uint',type='int'
585 ,help='Field parameter value [default: %default]')
586 parser.add_option('--SeqID2-field', dest='SeqID2Field',metavar='uint',type='int'
587 ,help='Field parameter value [default: %default]')
588 parser.add_option('--DestID3-field', dest='DestID3Field',metavar='uint',type='int'
589 ,help='Field parameter value [default: %default]')
590 parser.add_option('--SeqID3-field', dest='SeqID3Field',metavar='uint',type='int'
591 ,help='Field parameter value [default: %default]')
592 parser.add_option('--DestID4-field', dest='DestID4Field',metavar='uint',type='int'
593 ,help='Field parameter value [default: %default]')
594 parser.add_option('--SeqID4-field', dest='SeqID4Field',metavar='uint',type='int'
595 ,help='Field parameter value [default: %default]')
596
597
598 if __name__=='__main__':
599
600 from optparse import OptionParser
601 parser = OptionParser(usage="%prog [options]",
602 version="%prog "+__version__)
603
604 parser.add_option('--doc-test',dest='doctest',default=False,action='store_true',
605 help='run the documentation tests')
606 parser.add_option('--unit-test',dest='unittest',default=False,action='store_true',
607 help='run the unit tests')
608 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true',
609 help='Make the test output verbose')
610
611
612
613 typeChoices = ('binary','nmeapayload','nmea')
614 parser.add_option('-t','--type',choices=typeChoices,type='choice',dest='ioType'
615 ,default='nmeapayload'
616 ,help='What kind of string to write for encoding ('+', '.join(typeChoices)+') [default: %default]')
617
618
619 outputChoices = ('std','html','csv','sql' )
620 parser.add_option('-T','--output-type',choices=outputChoices,type='choice',dest='outputType'
621 ,default='std'
622 ,help='What kind of string to output ('+', '.join(outputChoices)+') [default: %default]')
623
624 parser.add_option('-o','--output',dest='outputFileName',default=None,
625 help='Name of the python file to write [default: stdout]')
626
627 parser.add_option('-f','--fields',dest='fieldList',default=None, action='append',
628 choices=fieldList,
629 help='Which fields to include in the output. Currently only for csv output [default: all]')
630
631 parser.add_option('-p','--print-csv-field-list',dest='printCsvfieldList',default=False,action='store_true',
632 help='Print the field name for csv')
633
634 parser.add_option('-c','--sql-create',dest='sqlCreate',default=False,action='store_true',
635 help='Print out an sql create command for the table.')
636
637 dbChoices = ('sqlite','postgres')
638 parser.add_option('-D','--db-type',dest='dbType',default='postgres'
639 ,choices=dbChoices,type='choice'
640 ,help='What kind of database ('+', '.join(dbChoices)+') [default: %default]')
641
642 addMsgOptions(parser)
643
644 (options,args) = parser.parse_args()
645 success=True
646
647 if options.doctest:
648 import os; print os.path.basename(sys.argv[0]), 'doctests ...',
649 sys.argv= [sys.argv[0]]
650 if options.verbose: sys.argv.append('-v')
651 import doctest
652 numfail,numtests=doctest.testmod()
653 if numfail==0: print 'ok'
654 else:
655 print 'FAILED'
656 success=False
657
658 if not success: sys.exit('Something Failed')
659 del success
660
661 if options.unittest:
662 sys.argv = [sys.argv[0]]
663 if options.verbose: sys.argv.append('-v')
664 unittest.main()
665
666 outfile = sys.stdout
667 if None!=options.outputFileName:
668 outfile = file(options.outputFileName,'w')
669
670
671 if options.doEncode:
672
673 if None==options.RepeatIndicatorField: parser.error("missing value for RepeatIndicatorField")
674 if None==options.UserIDField: parser.error("missing value for UserIDField")
675 if None==options.DestID1Field: parser.error("missing value for DestID1Field")
676 if None==options.SeqID1Field: parser.error("missing value for SeqID1Field")
677 if None==options.DestID2Field: parser.error("missing value for DestID2Field")
678 if None==options.SeqID2Field: parser.error("missing value for SeqID2Field")
679 if None==options.DestID3Field: parser.error("missing value for DestID3Field")
680 if None==options.SeqID3Field: parser.error("missing value for SeqID3Field")
681 if None==options.DestID4Field: parser.error("missing value for DestID4Field")
682 if None==options.SeqID4Field: parser.error("missing value for SeqID4Field")
683 msgDict={
684 'MessageID': '7',
685 'RepeatIndicator': options.RepeatIndicatorField,
686 'UserID': options.UserIDField,
687 'Spare': '0',
688 'DestID1': options.DestID1Field,
689 'SeqID1': options.SeqID1Field,
690 'DestID2': options.DestID2Field,
691 'SeqID2': options.SeqID2Field,
692 'DestID3': options.DestID3Field,
693 'SeqID3': options.SeqID3Field,
694 'DestID4': options.DestID4Field,
695 'SeqID4': options.SeqID4Field,
696 }
697
698 bits = encode(msgDict)
699 if 'binary'==options.ioType: print str(bits)
700 elif 'nmeapayload'==options.ioType:
701
702 print "bitLen",len(bits)
703 bitLen=len(bits)
704 if bitLen%6!=0:
705 bits = bits + BitVector(size=(6 - (bitLen%6)))
706 print "result:",binary.bitvectoais6(bits)[0]
707
708
709
710 elif 'nmea'==options.ioType: sys.exit("FIX: need to implement this capability")
711 else: sys.exit('ERROR: unknown ioType. Help!')
712
713
714 if options.sqlCreate:
715 sqlCreateStr(outfile,options.fieldList,dbType=options.dbType)
716
717 if options.printCsvfieldList:
718
719 if None == options.fieldList: options.fieldList = fieldList
720 import StringIO
721 buf = StringIO.StringIO()
722 for field in options.fieldList:
723 buf.write(field+',')
724 result = buf.getvalue()
725 if result[-1] == ',': print result[:-1]
726 else: print result
727
728 if options.doDecode:
729 for msg in args:
730 bv = None
731
732 if msg[0] in ('$','!') and msg[3:6] in ('VDM','VDO'):
733
734
735 bv = binary.ais6tobitvec(msg.split(',')[5])
736 else:
737
738 binaryMsg=True
739 for c in msg:
740 if c not in ('0','1'):
741 binaryMsg=False
742 break
743 if binaryMsg:
744 bv = BitVector(bitstring=msg)
745 else:
746 bv = binary.ais6tobitvec(msg)
747
748 printFields(decode(bv)
749 ,out=outfile
750 ,format=options.outputType
751 ,fieldList=options.fieldList
752 ,dbType=options.dbType
753 )
754