Package ais :: Module sls_header
[hide private]
[frames] | no frames]

Source Code for Module ais.sls_header

  1  #!/usr/bin/env python 
  2   
  3  __version__ = '$Revision: 4791 $'.split()[1] 
  4  __date__ = '$Date: 2007-02-16 $'.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  # FIX: check to see if these will be needed 
 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          'dac', 
 51          'fid', 
 52          'reserved', 
 53          'MessageID', 
 54          'BinaryData', 
 55  ) 
 56   
 57  fieldListPostgres = ( 
 58          'dac', 
 59          'fid', 
 60          'reserved', 
 61          'MessageID', 
 62          'BinaryData', 
 63  ) 
 64   
 65  toPgFields = { 
 66  } 
 67  ''' 
 68  Go to the Postgis field names from the straight field name 
 69  ''' 
 70   
 71  fromPgFields = { 
 72  } 
 73  ''' 
 74  Go from the Postgis field names to the straight field name 
 75  ''' 
 76   
 77  pgTypes = { 
 78  } 
 79  ''' 
 80  Lookup table for each postgis field name to get its type. 
 81  ''' 
 82   
83 -def encode(params, validate=False):
84 '''Create a sls_header binary message payload to pack into an AIS Msg sls_header. 85 86 Fields in params: 87 - dac(uint): Designated Area Code 366 for US 88 - fid(uint): Functional Id (field automatically set to "1") 89 - reserved(uint): say what? (field automatically set to "0") 90 - MessageID(uint): Binary message indentifier 91 - BinaryData(binary): FIX: make this consume the rest! 92 @param params: Dictionary of field names/values. Throws a ValueError exception if required is missing 93 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented. 94 @rtype: BitVector 95 @return: encoded binary message (for binary messages, this needs to be wrapped in a msg 8 96 @note: The returned bits may not be 6 bit aligned. It is up to you to pad out the bits. 97 ''' 98 99 bvList = [] 100 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['dac']),10)) 101 bvList.append(binary.setBitVectorSize(BitVector(intVal=1),6)) 102 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),2)) 103 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['MessageID']),6)) 104 bvList.append(params['BinaryData']) 105 106 return binary.joinBV(bvList)
107
108 -def decode(bv, validate=False):
109 '''Unpack a sls_header message 110 111 Fields in params: 112 - dac(uint): Designated Area Code 366 for US 113 - fid(uint): Functional Id (field automatically set to "1") 114 - reserved(uint): say what? (field automatically set to "0") 115 - MessageID(uint): Binary message indentifier 116 - BinaryData(binary): FIX: make this consume the rest! 117 @type bv: BitVector 118 @param bv: Bits defining a message 119 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented. 120 @rtype: dict 121 @return: params 122 ''' 123 124 #Would be nice to check the bit count here.. 125 #if validate: 126 # assert (len(bv)==FIX: SOME NUMBER) 127 r = {} 128 r['dac']=int(bv[0:10]) 129 r['fid']=1 130 r['reserved']=0 131 r['MessageID']=int(bv[18:24]) 132 r['BinaryData']=bv[24:] 133 return r
134
135 -def decodedac(bv, validate=False):
136 return int(bv[0:10])
137
138 -def decodefid(bv, validate=False):
139 return 1
140
141 -def decodereserved(bv, validate=False):
142 return 0
143
144 -def decodeMessageID(bv, validate=False):
145 return int(bv[18:24])
146
147 -def decodeBinaryData(bv, validate=False):
148 return bv[24:]
149 150
151 -def printHtml(params, out=sys.stdout):
152 out.write("<h3>sls_header<h3>\n") 153 out.write("<table border=\"1\">\n") 154 out.write("<tr bgcolor=\"orange\">\n") 155 out.write("<th align=\"left\">Field Name</th>\n") 156 out.write("<th align=\"left\">Type</th>\n") 157 out.write("<th align=\"left\">Value</th>\n") 158 out.write("<th align=\"left\">Value in Lookup Table</th>\n") 159 out.write("<th align=\"left\">Units</th>\n") 160 out.write("\n") 161 out.write("<tr>\n") 162 out.write("<td>dac</td>\n") 163 out.write("<td>uint</td>\n") 164 if 'dac' in params: 165 out.write(" <td>"+str(params['dac'])+"</td>\n") 166 out.write(" <td>"+str(params['dac'])+"</td>\n") 167 out.write("</tr>\n") 168 out.write("\n") 169 out.write("<tr>\n") 170 out.write("<td>fid</td>\n") 171 out.write("<td>uint</td>\n") 172 if 'fid' in params: 173 out.write(" <td>"+str(params['fid'])+"</td>\n") 174 out.write(" <td>"+str(params['fid'])+"</td>\n") 175 out.write("</tr>\n") 176 out.write("\n") 177 out.write("<tr>\n") 178 out.write("<td>reserved</td>\n") 179 out.write("<td>uint</td>\n") 180 if 'reserved' in params: 181 out.write(" <td>"+str(params['reserved'])+"</td>\n") 182 out.write(" <td>"+str(params['reserved'])+"</td>\n") 183 out.write("</tr>\n") 184 out.write("\n") 185 out.write("<tr>\n") 186 out.write("<td>MessageID</td>\n") 187 out.write("<td>uint</td>\n") 188 if 'MessageID' in params: 189 out.write(" <td>"+str(params['MessageID'])+"</td>\n") 190 out.write(" <td>"+str(params['MessageID'])+"</td>\n") 191 out.write("</tr>\n") 192 out.write("\n") 193 out.write("<tr>\n") 194 out.write("<td>BinaryData</td>\n") 195 out.write("<td>binary</td>\n") 196 if 'BinaryData' in params: 197 out.write(" <td>"+str(params['BinaryData'])+"</td>\n") 198 out.write(" <td>"+str(params['BinaryData'])+"</td>\n") 199 out.write("</tr>\n") 200 out.write("</table>\n")
201
202 -def printFields(params, out=sys.stdout, format='std', fieldList=None, dbType='postgres'):
203 '''Print a BinaryData message to stdout. 204 205 Fields in params: 206 - dac(uint): Designated Area Code 366 for US 207 - fid(uint): Functional Id (field automatically set to "1") 208 - reserved(uint): say what? (field automatically set to "0") 209 - MessageID(uint): Binary message indentifier 210 - BinaryData(binary): FIX: make this consume the rest! 211 @param params: Dictionary of field names/values. 212 @param out: File like object to write to 213 @rtype: stdout 214 @return: text to out 215 ''' 216 217 if 'std'==format: 218 out.write("BinaryData:\n") 219 if 'dac' in params: out.write(" dac: "+str(params['dac'])+"\n") 220 if 'fid' in params: out.write(" fid: "+str(params['fid'])+"\n") 221 if 'reserved' in params: out.write(" reserved: "+str(params['reserved'])+"\n") 222 if 'MessageID' in params: out.write(" MessageID: "+str(params['MessageID'])+"\n") 223 if 'BinaryData' in params: out.write(" BinaryData: "+str(params['BinaryData'])+"\n") 224 elif 'csv'==format: 225 if None == options.fieldList: 226 options.fieldList = fieldList 227 needComma = False; 228 for field in fieldList: 229 if needComma: out.write(',') 230 needComma = True 231 if field in params: 232 out.write(str(params[field])) 233 # else: leave it empty 234 out.write("\n") 235 elif 'html'==format: 236 printHtml(params,out) 237 elif 'sql'==format: 238 sqlInsertStr(params,out,dbType=dbType) 239 else: 240 print "ERROR: unknown format:",format 241 assert False 242 243 return # Nothing to return
244 245 ###################################################################### 246 # SQL SUPPORT 247 ###################################################################### 248
249 -def sqlCreateStr(outfile=sys.stdout, fields=None, extraFields=None 250 ,addCoastGuardFields=True 251 ,dbType='postgres' 252 ):
253 ''' 254 Return the SQL CREATE command for this message type 255 @param outfile: file like object to print to. 256 @param fields: which fields to put in the create. Defaults to all. 257 @param extraFields: A sequence of tuples containing (name,sql type) for additional fields 258 @param addCoastGuardFields: Add the extra fields that come after the NMEA check some from the USCG N-AIS format 259 @param dbType: Which flavor of database we are using so that the create is tailored ('sqlite' or 'postgres') 260 @type addCoastGuardFields: bool 261 @return: sql create string 262 @rtype: str 263 264 @see: sqlCreate 265 ''' 266 outfile.write(str(sqlCreate(fields,extraFields,addCoastGuardFields,dbType=dbType)))
267
268 -def sqlCreate(fields=None, extraFields=None, addCoastGuardFields=True, dbType='postgres'):
269 ''' 270 Return the sqlhelp object to create the table. 271 272 @param fields: which fields to put in the create. Defaults to all. 273 @param extraFields: A sequence of tuples containing (name,sql type) for additional fields 274 @param addCoastGuardFields: Add the extra fields that come after the NMEA check some from the USCG N-AIS format 275 @type addCoastGuardFields: bool 276 @param dbType: Which flavor of database we are using so that the create is tailored ('sqlite' or 'postgres') 277 @return: An object that can be used to generate a return 278 @rtype: sqlhelp.create 279 ''' 280 if None == fields: fields = fieldList 281 import sqlhelp 282 c = sqlhelp.create('sls_header',dbType=dbType) 283 c.addPrimaryKey() 284 if 'dac' in fields: c.addInt ('dac') 285 if 'fid' in fields: c.addInt ('fid') 286 if 'reserved' in fields: c.addInt ('reserved') 287 if 'MessageID' in fields: c.addInt ('MessageID') 288 if 'BinaryData' in fields: c.addBitVarying('BinaryData',1024) 289 290 if addCoastGuardFields: 291 # c.addInt('cg_rssi') # Relative signal strength indicator 292 # c.addInt('cg_d') # dBm receive strength 293 # c.addInt('cg_T') # Receive timestamp from the AIS equipment 294 # c.addInt('cg_S') # Slot received in 295 # c.addVarChar('cg_x',10) # Idonno 296 c.addVarChar('cg_r',15) # Receiver station ID - should usually be an MMSI, but sometimes is a string 297 c.addInt('cg_sec') # UTC seconds since the epoch 298 299 c.addTimestamp('cg_timestamp') # UTC decoded cg_sec - not actually in the data stream 300 301 return c
302
303 -def sqlInsertStr(params, outfile=sys.stdout, extraParams=None, dbType='postgres'):
304 ''' 305 Return the SQL INSERT command for this message type 306 @param params: dictionary of values keyed by field name 307 @param outfile: file like object to print to. 308 @param extraParams: A sequence of tuples containing (name,sql type) for additional fields 309 @return: sql create string 310 @rtype: str 311 312 @see: sqlCreate 313 ''' 314 outfile.write(str(sqlInsert(params,extraParams,dbType=dbType)))
315 316
317 -def sqlInsert(params,extraParams=None,dbType='postgres'):
318 ''' 319 Give the SQL INSERT statement 320 @param params: dict keyed by field name of values 321 @param extraParams: any extra fields that you have created beyond the normal ais message fields 322 @rtype: sqlhelp.insert 323 @return: insert class instance 324 @todo: allow optional type checking of params? 325 @warning: this will take invalid keys happily and do what??? 326 ''' 327 import sqlhelp 328 i = sqlhelp.insert('sls_header',dbType=dbType) 329 330 if dbType=='postgres': 331 finished = [] 332 for key in params: 333 if key in finished: 334 #print 'already did key',key 335 continue 336 337 if key not in toPgFields and key not in fromPgFields: 338 if type(params[key])==Decimal: i.add(key,float(params[key])) 339 else: i.add(key,params[key]) 340 else: 341 if key in fromPgFields: 342 val = params[key] 343 # Had better be a WKT type like POINT(-88.1 30.321) 344 i.addPostGIS(key,val) 345 finished.append(key) 346 else: 347 # Need to construct the type. 348 pgName = toPgFields[key] 349 #valStr='GeomFromText(\''+pgTypes[pgName]+'(' 350 valStr=pgTypes[pgName]+'(' 351 vals = [] 352 for nonPgKey in fromPgFields[pgName]: 353 vals.append(str(params[nonPgKey])) 354 finished.append(nonPgKey) 355 valStr+=' '.join(vals)+')' 356 i.addPostGIS(pgName,valStr) 357 else: 358 for key in params: 359 if type(params[key])==Decimal: i.add(key,float(params[key])) 360 else: i.add(key,params[key]) 361 362 if None != extraParams: 363 for key in extraParams: 364 i.add(key,extraParams[key]) 365 366 return i
367 368 369 ###################################################################### 370 # UNIT TESTING 371 ###################################################################### 372 import unittest
373 -def testParams():
374 '''Return a params file base on the testvalue tags. 375 @rtype: dict 376 @return: params based on testvalue tags 377 ''' 378 params = {} 379 params['dac'] = 366 380 params['fid'] = 1 381 params['reserved'] = 0 382 params['MessageID'] = 3 383 params['BinaryData'] = BitVector(bitstring='0') 384 385 return params
386
387 -class Testsls_header(unittest.TestCase):
388 '''Use testvalue tag text from each type to build test case the sls_header message'''
389 - def testEncodeDecode(self):
390 391 params = testParams() 392 bits = encode(params) 393 r = decode(bits) 394 395 # Check that each parameter came through ok. 396 self.failUnlessEqual(r['dac'],params['dac']) 397 self.failUnlessEqual(r['fid'],params['fid']) 398 self.failUnlessEqual(r['reserved'],params['reserved']) 399 self.failUnlessEqual(r['MessageID'],params['MessageID']) 400 self.failUnlessEqual(r['BinaryData'],params['BinaryData'])
401
402 -def addMsgOptions(parser):
403 parser.add_option('-d','--decode',dest='doDecode',default=False,action='store_true', 404 help='decode a "sls_header" AIS message') 405 parser.add_option('-e','--encode',dest='doEncode',default=False,action='store_true', 406 help='encode a "sls_header" AIS message') 407 parser.add_option('--dac-field', dest='dacField',metavar='uint',type='int' 408 ,help='Field parameter value [default: %default]') 409 parser.add_option('--MessageID-field', dest='MessageIDField',metavar='uint',type='int' 410 ,help='Field parameter value [default: %default]') 411 parser.add_option('--BinaryData-field', dest='BinaryDataField',metavar='binary',type='string' 412 ,help='Field parameter value [default: %default]')
413 414 ############################################################ 415 if __name__=='__main__': 416 417 from optparse import OptionParser 418 parser = OptionParser(usage="%prog [options]", 419 version="%prog "+__version__) 420 421 parser.add_option('--doc-test',dest='doctest',default=False,action='store_true', 422 help='run the documentation tests') 423 parser.add_option('--unit-test',dest='unittest',default=False,action='store_true', 424 help='run the unit tests') 425 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', 426 help='Make the test output verbose') 427 428 # FIX: remove nmea from binary messages. No way to build the whole packet? 429 # FIX: or build the surrounding msg 8 for a broadcast? 430 typeChoices = ('binary','nmeapayload','nmea') # FIX: what about a USCG type message? 431 parser.add_option('-t','--type',choices=typeChoices,type='choice',dest='ioType' 432 ,default='nmeapayload' 433 ,help='What kind of string to write for encoding ('+', '.join(typeChoices)+') [default: %default]') 434 435 436 outputChoices = ('std','html','csv','sql' ) 437 parser.add_option('-T','--output-type',choices=outputChoices,type='choice',dest='outputType' 438 ,default='std' 439 ,help='What kind of string to output ('+', '.join(outputChoices)+') [default: %default]') 440 441 parser.add_option('-o','--output',dest='outputFileName',default=None, 442 help='Name of the python file to write [default: stdout]') 443 444 parser.add_option('-f','--fields',dest='fieldList',default=None, action='append', 445 choices=fieldList, 446 help='Which fields to include in the output. Currently only for csv output [default: all]') 447 448 parser.add_option('-p','--print-csv-field-list',dest='printCsvfieldList',default=False,action='store_true', 449 help='Print the field name for csv') 450 451 parser.add_option('-c','--sql-create',dest='sqlCreate',default=False,action='store_true', 452 help='Print out an sql create command for the table.') 453 454 dbChoices = ('sqlite','postgres') 455 parser.add_option('-D','--db-type',dest='dbType',default='postgres' 456 ,choices=dbChoices,type='choice' 457 ,help='What kind of database ('+', '.join(dbChoices)+') [default: %default]') 458 459 addMsgOptions(parser) 460 461 (options,args) = parser.parse_args() 462 success=True 463 464 if options.doctest: 465 import os; print os.path.basename(sys.argv[0]), 'doctests ...', 466 sys.argv= [sys.argv[0]] 467 if options.verbose: sys.argv.append('-v') 468 import doctest 469 numfail,numtests=doctest.testmod() 470 if numfail==0: print 'ok' 471 else: 472 print 'FAILED' 473 success=False 474 475 if not success: sys.exit('Something Failed') 476 del success # Hide success from epydoc 477 478 if options.unittest: 479 sys.argv = [sys.argv[0]] 480 if options.verbose: sys.argv.append('-v') 481 unittest.main() 482 483 outfile = sys.stdout 484 if None!=options.outputFileName: 485 outfile = file(options.outputFileName,'w') 486 487 488 if options.doEncode: 489 # First make sure all non required options are specified 490 if None==options.dacField: parser.error("missing value for dacField") 491 if None==options.MessageIDField: parser.error("missing value for MessageIDField") 492 if None==options.BinaryDataField: parser.error("missing value for BinaryDataField") 493 msgDict={ 494 'dac': options.dacField, 495 'fid': '1', 496 'reserved': '0', 497 'MessageID': options.MessageIDField, 498 'BinaryData': options.BinaryDataField, 499 } 500 501 bits = encode(msgDict) 502 if 'binary'==options.ioType: print str(bits) 503 elif 'nmeapayload'==options.ioType: 504 # FIX: figure out if this might be necessary at compile time 505 print "bitLen",len(bits) 506 bitLen=len(bits) 507 if bitLen%6!=0: 508 bits = bits + BitVector(size=(6 - (bitLen%6))) # Pad out to multiple of 6 509 print "result:",binary.bitvectoais6(bits)[0] 510 511 512 # FIX: Do not emit this option for the binary message payloads. Does not make sense. 513 elif 'nmea'==options.ioType: sys.exit("FIX: need to implement this capability") 514 else: sys.exit('ERROR: unknown ioType. Help!') 515 516 517 if options.sqlCreate: 518 sqlCreateStr(outfile,options.fieldList,dbType=options.dbType) 519 520 if options.printCsvfieldList: 521 # Make a csv separated list of fields that will be displayed for csv 522 if None == options.fieldList: options.fieldList = fieldList 523 import StringIO 524 buf = StringIO.StringIO() 525 for field in options.fieldList: 526 buf.write(field+',') 527 result = buf.getvalue() 528 if result[-1] == ',': print result[:-1] 529 else: print result 530 531 if options.doDecode: 532 for msg in args: 533 bv = None 534 535 if msg[0] in ('$','!') and msg[3:6] in ('VDM','VDO'): 536 # Found nmea 537 # FIX: do checksum 538 bv = binary.ais6tobitvec(msg.split(',')[5]) 539 else: # either binary or nmeapayload... expect mostly nmeapayloads 540 # assumes that an all 0 and 1 string can not be a nmeapayload 541 binaryMsg=True 542 for c in msg: 543 if c not in ('0','1'): 544 binaryMsg=False 545 break 546 if binaryMsg: 547 bv = BitVector(bitstring=msg) 548 else: # nmeapayload 549 bv = binary.ais6tobitvec(msg) 550 551 printFields(decode(bv) 552 ,out=outfile 553 ,format=options.outputType 554 ,fieldList=options.fieldList 555 ,dbType=options.dbType 556 ) 557