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-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  # 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 sls_header 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("sls_header:\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 continue 335 336 if key not in toPgFields and key not in fromPgFields: 337 if type(params[key])==Decimal: i.add(key,float(params[key])) 338 else: i.add(key,params[key]) 339 else: 340 if key in fromPgFields: 341 val = params[key] 342 # Had better be a WKT type like POINT(-88.1 30.321) 343 i.addPostGIS(key,val) 344 finished.append(key) 345 else: 346 # Need to construct the type. 347 pgName = toPgFields[key] 348 #valStr='GeomFromText(\''+pgTypes[pgName]+'(' 349 valStr=pgTypes[pgName]+'(' 350 vals = [] 351 for nonPgKey in fromPgFields[pgName]: 352 vals.append(str(params[nonPgKey])) 353 finished.append(nonPgKey) 354 valStr+=' '.join(vals)+')' 355 i.addPostGIS(pgName,valStr) 356 else: 357 for key in params: 358 if type(params[key])==Decimal: i.add(key,float(params[key])) 359 else: i.add(key,params[key]) 360 361 if None != extraParams: 362 for key in extraParams: 363 i.add(key,extraParams[key]) 364 365 return i
366 367 368 ###################################################################### 369 # UNIT TESTING 370 ###################################################################### 371 import unittest
372 -def testParams():
373 '''Return a params file base on the testvalue tags. 374 @rtype: dict 375 @return: params based on testvalue tags 376 ''' 377 params = {} 378 params['dac'] = 366 379 params['fid'] = 1 380 params['reserved'] = 0 381 params['MessageID'] = 3 382 params['BinaryData'] = BitVector(bitstring='0') 383 384 return params
385
386 -class Testsls_header(unittest.TestCase):
387 '''Use testvalue tag text from each type to build test case the sls_header message'''
388 - def testEncodeDecode(self):
389 390 params = testParams() 391 bits = encode(params) 392 r = decode(bits) 393 394 # Check that each parameter came through ok. 395 self.failUnlessEqual(r['dac'],params['dac']) 396 self.failUnlessEqual(r['fid'],params['fid']) 397 self.failUnlessEqual(r['reserved'],params['reserved']) 398 self.failUnlessEqual(r['MessageID'],params['MessageID']) 399 self.failUnlessEqual(r['BinaryData'],params['BinaryData'])
400
401 -def addMsgOptions(parser):
402 parser.add_option('-d','--decode',dest='doDecode',default=False,action='store_true', 403 help='decode a "sls_header" AIS message') 404 parser.add_option('-e','--encode',dest='doEncode',default=False,action='store_true', 405 help='encode a "sls_header" AIS message') 406 parser.add_option('--dac-field', dest='dacField',metavar='uint',type='int' 407 ,help='Field parameter value [default: %default]') 408 parser.add_option('--MessageID-field', dest='MessageIDField',metavar='uint',type='int' 409 ,help='Field parameter value [default: %default]') 410 parser.add_option('--BinaryData-field', dest='BinaryDataField',metavar='binary',type='string' 411 ,help='Field parameter value [default: %default]')
412 413 ############################################################ 414 if __name__=='__main__': 415 416 from optparse import OptionParser 417 parser = OptionParser(usage="%prog [options]", 418 version="%prog "+__version__) 419 420 parser.add_option('--doc-test',dest='doctest',default=False,action='store_true', 421 help='run the documentation tests') 422 parser.add_option('--unit-test',dest='unittest',default=False,action='store_true', 423 help='run the unit tests') 424 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', 425 help='Make the test output verbose') 426 427 # FIX: remove nmea from binary messages. No way to build the whole packet? 428 # FIX: or build the surrounding msg 8 for a broadcast? 429 typeChoices = ('binary','nmeapayload','nmea') # FIX: what about a USCG type message? 430 parser.add_option('-t','--type',choices=typeChoices,type='choice',dest='ioType' 431 ,default='nmeapayload' 432 ,help='What kind of string to write for encoding ('+', '.join(typeChoices)+') [default: %default]') 433 434 435 outputChoices = ('std','html','csv','sql' ) 436 parser.add_option('-T','--output-type',choices=outputChoices,type='choice',dest='outputType' 437 ,default='std' 438 ,help='What kind of string to output ('+', '.join(outputChoices)+') [default: %default]') 439 440 parser.add_option('-o','--output',dest='outputFileName',default=None, 441 help='Name of the python file to write [default: stdout]') 442 443 parser.add_option('-f','--fields',dest='fieldList',default=None, action='append', 444 choices=fieldList, 445 help='Which fields to include in the output. Currently only for csv output [default: all]') 446 447 parser.add_option('-p','--print-csv-field-list',dest='printCsvfieldList',default=False,action='store_true', 448 help='Print the field name for csv') 449 450 parser.add_option('-c','--sql-create',dest='sqlCreate',default=False,action='store_true', 451 help='Print out an sql create command for the table.') 452 453 dbChoices = ('sqlite','postgres') 454 parser.add_option('-D','--db-type',dest='dbType',default='postgres' 455 ,choices=dbChoices,type='choice' 456 ,help='What kind of database ('+', '.join(dbChoices)+') [default: %default]') 457 458 addMsgOptions(parser) 459 460 (options,args) = parser.parse_args() 461 success=True 462 463 if options.doctest: 464 import os; print os.path.basename(sys.argv[0]), 'doctests ...', 465 sys.argv= [sys.argv[0]] 466 if options.verbose: sys.argv.append('-v') 467 import doctest 468 numfail,numtests=doctest.testmod() 469 if numfail==0: print 'ok' 470 else: 471 print 'FAILED' 472 success=False 473 474 if not success: sys.exit('Something Failed') 475 del success # Hide success from epydoc 476 477 if options.unittest: 478 sys.argv = [sys.argv[0]] 479 if options.verbose: sys.argv.append('-v') 480 unittest.main() 481 482 outfile = sys.stdout 483 if None!=options.outputFileName: 484 outfile = file(options.outputFileName,'w') 485 486 487 if options.doEncode: 488 # First make sure all non required options are specified 489 if None==options.dacField: parser.error("missing value for dacField") 490 if None==options.MessageIDField: parser.error("missing value for MessageIDField") 491 if None==options.BinaryDataField: parser.error("missing value for BinaryDataField") 492 msgDict={ 493 'dac': options.dacField, 494 'fid': '1', 495 'reserved': '0', 496 'MessageID': options.MessageIDField, 497 'BinaryData': options.BinaryDataField, 498 } 499 500 bits = encode(msgDict) 501 if 'binary'==options.ioType: print str(bits) 502 elif 'nmeapayload'==options.ioType: 503 # FIX: figure out if this might be necessary at compile time 504 print "bitLen",len(bits) 505 bitLen=len(bits) 506 if bitLen%6!=0: 507 bits = bits + BitVector(size=(6 - (bitLen%6))) # Pad out to multiple of 6 508 print "result:",binary.bitvectoais6(bits)[0] 509 510 511 # FIX: Do not emit this option for the binary message payloads. Does not make sense. 512 elif 'nmea'==options.ioType: sys.exit("FIX: need to implement this capability") 513 else: sys.exit('ERROR: unknown ioType. Help!') 514 515 516 if options.sqlCreate: 517 sqlCreateStr(outfile,options.fieldList,dbType=options.dbType) 518 519 if options.printCsvfieldList: 520 # Make a csv separated list of fields that will be displayed for csv 521 if None == options.fieldList: options.fieldList = fieldList 522 import StringIO 523 buf = StringIO.StringIO() 524 for field in options.fieldList: 525 buf.write(field+',') 526 result = buf.getvalue() 527 if result[-1] == ',': print result[:-1] 528 else: print result 529 530 if options.doDecode: 531 for msg in args: 532 bv = None 533 534 if msg[0] in ('$','!') and msg[3:6] in ('VDM','VDO'): 535 # Found nmea 536 # FIX: do checksum 537 bv = binary.ais6tobitvec(msg.split(',')[5]) 538 else: # either binary or nmeapayload... expect mostly nmeapayloads 539 # assumes that an all 0 and 1 string can not be a nmeapayload 540 binaryMsg=True 541 for c in msg: 542 if c not in ('0','1'): 543 binaryMsg=False 544 break 545 if binaryMsg: 546 bv = BitVector(bitstring=msg) 547 else: # nmeapayload 548 bv = binary.ais6tobitvec(msg) 549 550 printFields(decode(bv) 551 ,out=outfile 552 ,format=options.outputType 553 ,fieldList=options.fieldList 554 ,dbType=options.dbType 555 ) 556