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

Source Code for Module ais.ais_msg_5

  1  #!/usr/bin/env python 
  2   
  3  __version__ = '$Revision: 4791 $'.split()[1] 
  4  __date__ = '$Date: 2007-01-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  ''' 
 34   
 35  import sys 
 36  from decimal import Decimal 
 37  from BitVector import BitVector 
 38   
 39  import binary, aisstring 
 40   
 41  # FIX: check to see if these will be needed 
 42  TrueBV  = BitVector(bitstring="1") 
 43  "Why always rebuild the True bit?  This should speed things up a bunch" 
 44  FalseBV = BitVector(bitstring="0") 
 45  "Why always rebuild the False bit?  This should speed things up a bunch" 
 46   
 47   
 48  fieldList = [ 
 49          'MessageID', 
 50          'RepeatIndicator', 
 51          'UserID', 
 52          'AISversion', 
 53          'IMOnumber', 
 54          'callsign', 
 55          'name', 
 56          'shipandcargo', 
 57          'dimA', 
 58          'dimB', 
 59          'dimC', 
 60          'dimD', 
 61          'fixtype', 
 62          'ETA', 
 63          'draught', 
 64          'destination', 
 65          'dte', 
 66          'Spare', 
 67  ] 
 68   
69 -def encode(params, validate=False):
70 '''Create a shipdata binary message payload to pack into an AIS Msg shipdata. 71 72 Fields in params: 73 - MessageID(uint): AIS message number. Must be 5 (field automatically set to "5") 74 - RepeatIndicator(uint): Indicated how many times a message has been repeated 75 - UserID(uint): Unique ship identification number (MMSI) 76 - AISversion(uint): Compliant with what edition. 0 is the first edition. 77 - IMOnumber(uint): vessel identification number (different than mmsi) 78 - callsign(aisstr6): Ship radio call sign 79 - name(aisstr6): Vessel name 80 - shipandcargo(uint): what 81 - dimA(uint): FIX: write a description 82 - dimB(uint): FIX: write a description 83 - dimC(uint): FIX: write a description 84 - dimD(uint): FIX: write a description 85 - fixtype(uint): Method used for positioning 86 - ETA(uint): FIX: break this out 87 - draught(udecimal): Maximum present static draught. 25.5 is 25.5 m or greater 88 - destination(aisstr6): Where is the vessel going 89 - dte(uint): Data terminal ready 90 - Spare(uint): Reserved for definition by a regional authority. (field automatically set to "0") 91 @param params: Dictionary of field names/values. Throws a ValueError exception if required is missing 92 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented. 93 @rtype: BitVector 94 @return: encoded binary message (for binary messages, this needs to be wrapped in a msg 8 95 @note: The returned bits may not be 6 bit aligned. It is up to you to pad out the bits. 96 ''' 97 98 bvList = [] 99 bvList.append(binary.setBitVectorSize(BitVector(intVal=5),6)) 100 if 'RepeatIndicator' in params: 101 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['RepeatIndicator']),2)) 102 else: 103 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),2)) 104 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['UserID']),30)) 105 if 'AISversion' in params: 106 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['AISversion']),2)) 107 else: 108 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),2)) 109 if 'IMOnumber' in params: 110 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['IMOnumber']),30)) 111 else: 112 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),30)) 113 if 'callsign' in params: 114 bvList.append(aisstring.encode(params['callsign'],42)) 115 else: 116 bvList.append(aisstring.encode('@@@@@@@',42)) 117 if 'name' in params: 118 bvList.append(aisstring.encode(params['name'],120)) 119 else: 120 bvList.append(aisstring.encode('@@@@@@@@@@@@@@@@@@@@',120)) 121 if 'shipandcargo' in params: 122 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['shipandcargo']),8)) 123 else: 124 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),8)) 125 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['dimA']),9)) 126 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['dimB']),9)) 127 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['dimC']),6)) 128 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['dimD']),6)) 129 if 'fixtype' in params: 130 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['fixtype']),4)) 131 else: 132 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),4)) 133 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['ETA']),20)) 134 if 'draught' in params: 135 bvList.append(binary.setBitVectorSize(BitVector(intVal=int((Decimal(params['draught'])*Decimal('10')))),8)) 136 else: 137 bvList.append(binary.setBitVectorSize(BitVector(intVal=int(0)),8)) 138 if 'destination' in params: 139 bvList.append(aisstring.encode(params['destination'],120)) 140 else: 141 bvList.append(aisstring.encode('@@@@@@@@@@@@@@@@@@@@',120)) 142 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['dte']),1)) 143 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),1)) 144 145 return binary.joinBV(bvList)
146
147 -def decode(bv, validate=False):
148 '''Unpack a shipdata message 149 150 Fields in params: 151 - MessageID(uint): AIS message number. Must be 5 (field automatically set to "5") 152 - RepeatIndicator(uint): Indicated how many times a message has been repeated 153 - UserID(uint): Unique ship identification number (MMSI) 154 - AISversion(uint): Compliant with what edition. 0 is the first edition. 155 - IMOnumber(uint): vessel identification number (different than mmsi) 156 - callsign(aisstr6): Ship radio call sign 157 - name(aisstr6): Vessel name 158 - shipandcargo(uint): what 159 - dimA(uint): FIX: write a description 160 - dimB(uint): FIX: write a description 161 - dimC(uint): FIX: write a description 162 - dimD(uint): FIX: write a description 163 - fixtype(uint): Method used for positioning 164 - ETA(uint): FIX: break this out 165 - draught(udecimal): Maximum present static draught. 25.5 is 25.5 m or greater 166 - destination(aisstr6): Where is the vessel going 167 - dte(uint): Data terminal ready 168 - Spare(uint): Reserved for definition by a regional authority. (field automatically set to "0") 169 @type bv: BitVector 170 @param bv: Bits defining a message 171 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented. 172 @rtype: dict 173 @return: params 174 ''' 175 176 #Would be nice to check the bit count here.. 177 #if validate: 178 # assert (len(bv)==FIX: SOME NUMBER) 179 r = {} 180 r['MessageID']=5 181 r['RepeatIndicator']=int(bv[6:8]) 182 r['UserID']=int(bv[8:38]) 183 r['AISversion']=int(bv[38:40]) 184 r['IMOnumber']=int(bv[40:70]) 185 r['callsign']=aisstring.decode(bv[70:112]) 186 r['name']=aisstring.decode(bv[112:232]) 187 r['shipandcargo']=int(bv[232:240]) 188 r['dimA']=int(bv[240:249]) 189 r['dimB']=int(bv[249:258]) 190 r['dimC']=int(bv[258:264]) 191 r['dimD']=int(bv[264:270]) 192 r['fixtype']=int(bv[270:274]) 193 r['ETA']=int(bv[274:294]) 194 r['draught']=Decimal(int(bv[294:302]))/Decimal('10') 195 r['destination']=aisstring.decode(bv[302:422]) 196 r['dte']=int(bv[422:423]) 197 r['Spare']=0 198 return r
199
200 -def decodeMessageID(bv, validate=False):
201 return 5
202
203 -def decodeRepeatIndicator(bv, validate=False):
204 return int(bv[6:8])
205
206 -def decodeUserID(bv, validate=False):
207 return int(bv[8:38])
208
209 -def decodeAISversion(bv, validate=False):
210 return int(bv[38:40])
211
212 -def decodeIMOnumber(bv, validate=False):
213 return int(bv[40:70])
214
215 -def decodecallsign(bv, validate=False):
216 return aisstring.decode(bv[70:112])
217
218 -def decodename(bv, validate=False):
219 return aisstring.decode(bv[112:232])
220
221 -def decodeshipandcargo(bv, validate=False):
222 return int(bv[232:240])
223
224 -def decodedimA(bv, validate=False):
225 return int(bv[240:249])
226
227 -def decodedimB(bv, validate=False):
228 return int(bv[249:258])
229
230 -def decodedimC(bv, validate=False):
231 return int(bv[258:264])
232
233 -def decodedimD(bv, validate=False):
234 return int(bv[264:270])
235
236 -def decodefixtype(bv, validate=False):
237 return int(bv[270:274])
238
239 -def decodeETA(bv, validate=False):
240 return int(bv[274:294])
241
242 -def decodedraught(bv, validate=False):
243 return Decimal(int(bv[294:302]))/Decimal('10')
244
245 -def decodedestination(bv, validate=False):
246 return aisstring.decode(bv[302:422])
247
248 -def decodedte(bv, validate=False):
249 return int(bv[422:423])
250
251 -def decodeSpare(bv, validate=False):
252 return 0
253 254
255 -def printHtml(params, out=sys.stdout):
256 out.write("<h3>shipdata<h3>\n") 257 out.write("<table border=\"1\">\n") 258 out.write("<tr bgcolor=\"orange\">\n") 259 out.write("<th align=\"left\">Field Name</th>\n") 260 out.write("<th align=\"left\">Type</th>\n") 261 out.write("<th align=\"left\">Value</th>\n") 262 out.write("<th align=\"left\">Value in Lookup Table</th>\n") 263 out.write("<th align=\"left\">Units</th>\n") 264 out.write("\n") 265 out.write("<tr>\n") 266 out.write("<td>MessageID</td>\n") 267 out.write("<td>uint</td>\n") 268 if 'MessageID' in params: 269 out.write(" <td>"+str(params['MessageID'])+"</td>\n") 270 out.write(" <td>"+str(params['MessageID'])+"</td>\n") 271 out.write("</tr>\n") 272 out.write("\n") 273 out.write("<tr>\n") 274 out.write("<td>RepeatIndicator</td>\n") 275 out.write("<td>uint</td>\n") 276 if 'RepeatIndicator' in params: 277 out.write(" <td>"+str(params['RepeatIndicator'])+"</td>\n") 278 if str(params['RepeatIndicator']) in RepeatIndicatorDecodeLut: 279 out.write("<td>"+RepeatIndicatorDecodeLut[str(params['RepeatIndicator'])]+"</td>") 280 else: 281 out.write("<td><i>Missing LUT entry</i></td>") 282 out.write("</tr>\n") 283 out.write("\n") 284 out.write("<tr>\n") 285 out.write("<td>UserID</td>\n") 286 out.write("<td>uint</td>\n") 287 if 'UserID' in params: 288 out.write(" <td>"+str(params['UserID'])+"</td>\n") 289 out.write(" <td>"+str(params['UserID'])+"</td>\n") 290 out.write("</tr>\n") 291 out.write("\n") 292 out.write("<tr>\n") 293 out.write("<td>AISversion</td>\n") 294 out.write("<td>uint</td>\n") 295 if 'AISversion' in params: 296 out.write(" <td>"+str(params['AISversion'])+"</td>\n") 297 out.write(" <td>"+str(params['AISversion'])+"</td>\n") 298 out.write("</tr>\n") 299 out.write("\n") 300 out.write("<tr>\n") 301 out.write("<td>IMOnumber</td>\n") 302 out.write("<td>uint</td>\n") 303 if 'IMOnumber' in params: 304 out.write(" <td>"+str(params['IMOnumber'])+"</td>\n") 305 out.write(" <td>"+str(params['IMOnumber'])+"</td>\n") 306 out.write("</tr>\n") 307 out.write("\n") 308 out.write("<tr>\n") 309 out.write("<td>callsign</td>\n") 310 out.write("<td>aisstr6</td>\n") 311 if 'callsign' in params: 312 out.write(" <td>"+str(params['callsign'])+"</td>\n") 313 out.write(" <td>"+str(params['callsign'])+"</td>\n") 314 out.write("</tr>\n") 315 out.write("\n") 316 out.write("<tr>\n") 317 out.write("<td>name</td>\n") 318 out.write("<td>aisstr6</td>\n") 319 if 'name' in params: 320 out.write(" <td>"+str(params['name'])+"</td>\n") 321 out.write(" <td>"+str(params['name'])+"</td>\n") 322 out.write("</tr>\n") 323 out.write("\n") 324 out.write("<tr>\n") 325 out.write("<td>shipandcargo</td>\n") 326 out.write("<td>uint</td>\n") 327 if 'shipandcargo' in params: 328 out.write(" <td>"+str(params['shipandcargo'])+"</td>\n") 329 out.write(" <td>"+str(params['shipandcargo'])+"</td>\n") 330 out.write("</tr>\n") 331 out.write("\n") 332 out.write("<tr>\n") 333 out.write("<td>dimA</td>\n") 334 out.write("<td>uint</td>\n") 335 if 'dimA' in params: 336 out.write(" <td>"+str(params['dimA'])+"</td>\n") 337 out.write(" <td>"+str(params['dimA'])+"</td>\n") 338 out.write("<td>m</td>\n") 339 out.write("</tr>\n") 340 out.write("\n") 341 out.write("<tr>\n") 342 out.write("<td>dimB</td>\n") 343 out.write("<td>uint</td>\n") 344 if 'dimB' in params: 345 out.write(" <td>"+str(params['dimB'])+"</td>\n") 346 out.write(" <td>"+str(params['dimB'])+"</td>\n") 347 out.write("<td>m</td>\n") 348 out.write("</tr>\n") 349 out.write("\n") 350 out.write("<tr>\n") 351 out.write("<td>dimC</td>\n") 352 out.write("<td>uint</td>\n") 353 if 'dimC' in params: 354 out.write(" <td>"+str(params['dimC'])+"</td>\n") 355 out.write(" <td>"+str(params['dimC'])+"</td>\n") 356 out.write("<td>m</td>\n") 357 out.write("</tr>\n") 358 out.write("\n") 359 out.write("<tr>\n") 360 out.write("<td>dimD</td>\n") 361 out.write("<td>uint</td>\n") 362 if 'dimD' in params: 363 out.write(" <td>"+str(params['dimD'])+"</td>\n") 364 out.write(" <td>"+str(params['dimD'])+"</td>\n") 365 out.write("<td>m</td>\n") 366 out.write("</tr>\n") 367 out.write("\n") 368 out.write("<tr>\n") 369 out.write("<td>fixtype</td>\n") 370 out.write("<td>uint</td>\n") 371 if 'fixtype' in params: 372 out.write(" <td>"+str(params['fixtype'])+"</td>\n") 373 if str(params['fixtype']) in fixtypeDecodeLut: 374 out.write("<td>"+fixtypeDecodeLut[str(params['fixtype'])]+"</td>") 375 else: 376 out.write("<td><i>Missing LUT entry</i></td>") 377 out.write("</tr>\n") 378 out.write("\n") 379 out.write("<tr>\n") 380 out.write("<td>ETA</td>\n") 381 out.write("<td>uint</td>\n") 382 if 'ETA' in params: 383 out.write(" <td>"+str(params['ETA'])+"</td>\n") 384 out.write(" <td>"+str(params['ETA'])+"</td>\n") 385 out.write("</tr>\n") 386 out.write("\n") 387 out.write("<tr>\n") 388 out.write("<td>draught</td>\n") 389 out.write("<td>udecimal</td>\n") 390 if 'draught' in params: 391 out.write(" <td>"+str(params['draught'])+"</td>\n") 392 out.write(" <td>"+str(params['draught'])+"</td>\n") 393 out.write("<td>m</td>\n") 394 out.write("</tr>\n") 395 out.write("\n") 396 out.write("<tr>\n") 397 out.write("<td>destination</td>\n") 398 out.write("<td>aisstr6</td>\n") 399 if 'destination' in params: 400 out.write(" <td>"+str(params['destination'])+"</td>\n") 401 out.write(" <td>"+str(params['destination'])+"</td>\n") 402 out.write("</tr>\n") 403 out.write("\n") 404 out.write("<tr>\n") 405 out.write("<td>dte</td>\n") 406 out.write("<td>uint</td>\n") 407 if 'dte' in params: 408 out.write(" <td>"+str(params['dte'])+"</td>\n") 409 if str(params['dte']) in dteDecodeLut: 410 out.write("<td>"+dteDecodeLut[str(params['dte'])]+"</td>") 411 else: 412 out.write("<td><i>Missing LUT entry</i></td>") 413 out.write("</tr>\n") 414 out.write("\n") 415 out.write("<tr>\n") 416 out.write("<td>Spare</td>\n") 417 out.write("<td>uint</td>\n") 418 if 'Spare' in params: 419 out.write(" <td>"+str(params['Spare'])+"</td>\n") 420 out.write(" <td>"+str(params['Spare'])+"</td>\n") 421 out.write("</tr>\n") 422 out.write("</table>\n")
423
424 -def printFields(params, out=sys.stdout, format='std', fieldList=None):
425 '''Print a Spare message to stdout. 426 427 Fields in params: 428 - MessageID(uint): AIS message number. Must be 5 (field automatically set to "5") 429 - RepeatIndicator(uint): Indicated how many times a message has been repeated 430 - UserID(uint): Unique ship identification number (MMSI) 431 - AISversion(uint): Compliant with what edition. 0 is the first edition. 432 - IMOnumber(uint): vessel identification number (different than mmsi) 433 - callsign(aisstr6): Ship radio call sign 434 - name(aisstr6): Vessel name 435 - shipandcargo(uint): what 436 - dimA(uint): FIX: write a description 437 - dimB(uint): FIX: write a description 438 - dimC(uint): FIX: write a description 439 - dimD(uint): FIX: write a description 440 - fixtype(uint): Method used for positioning 441 - ETA(uint): FIX: break this out 442 - draught(udecimal): Maximum present static draught. 25.5 is 25.5 m or greater 443 - destination(aisstr6): Where is the vessel going 444 - dte(uint): Data terminal ready 445 - Spare(uint): Reserved for definition by a regional authority. (field automatically set to "0") 446 @param params: Dictionary of field names/values. 447 @param out: File like object to write to 448 @rtype: stdout 449 @return: text to out 450 ''' 451 452 if 'std'==format: 453 out.write("Spare:\n") 454 if 'MessageID' in params: out.write(" MessageID: "+str(params['MessageID'])+"\n") 455 if 'RepeatIndicator' in params: out.write(" RepeatIndicator: "+str(params['RepeatIndicator'])+"\n") 456 if 'UserID' in params: out.write(" UserID: "+str(params['UserID'])+"\n") 457 if 'AISversion' in params: out.write(" AISversion: "+str(params['AISversion'])+"\n") 458 if 'IMOnumber' in params: out.write(" IMOnumber: "+str(params['IMOnumber'])+"\n") 459 if 'callsign' in params: out.write(" callsign: "+str(params['callsign'])+"\n") 460 if 'name' in params: out.write(" name: "+str(params['name'])+"\n") 461 if 'shipandcargo' in params: out.write(" shipandcargo: "+str(params['shipandcargo'])+"\n") 462 if 'dimA' in params: out.write(" dimA: "+str(params['dimA'])+"\n") 463 if 'dimB' in params: out.write(" dimB: "+str(params['dimB'])+"\n") 464 if 'dimC' in params: out.write(" dimC: "+str(params['dimC'])+"\n") 465 if 'dimD' in params: out.write(" dimD: "+str(params['dimD'])+"\n") 466 if 'fixtype' in params: out.write(" fixtype: "+str(params['fixtype'])+"\n") 467 if 'ETA' in params: out.write(" ETA: "+str(params['ETA'])+"\n") 468 if 'draught' in params: out.write(" draught: "+str(params['draught'])+"\n") 469 if 'destination' in params: out.write(" destination: "+str(params['destination'])+"\n") 470 if 'dte' in params: out.write(" dte: "+str(params['dte'])+"\n") 471 if 'Spare' in params: out.write(" Spare: "+str(params['Spare'])+"\n") 472 elif 'csv'==format: 473 if None == options.fieldList: 474 options.fieldList = fieldList 475 needComma = False; 476 for field in fieldList: 477 if needComma: out.write(',') 478 needComma = True 479 if field in params: 480 out.write(str(params[field])) 481 # else: leave it empty 482 out.write("\n") 483 elif 'html'==format: 484 printHtml(params,out) 485 else: 486 print "ERROR: unknown format:",format 487 assert False 488 489 return # Nothing to return
490 491 RepeatIndicatorEncodeLut = { 492 'default':'0', 493 'do not repeat any more':'3', 494 } #RepeatIndicatorEncodeLut 495 496 RepeatIndicatorDecodeLut = { 497 '0':'default', 498 '3':'do not repeat any more', 499 } # RepeatIndicatorEncodeLut 500 501 fixtypeEncodeLut = { 502 'undefined':'0', 503 'GPS':'1', 504 'GLONASS':'2', 505 'combined GPS/GLONASS':'3', 506 'Loran-C':'4', 507 'Chayka':'5', 508 'integrated navigation system':'6', 509 'surveyed':'7', 510 } #fixtypeEncodeLut 511 512 fixtypeDecodeLut = { 513 '0':'undefined', 514 '1':'GPS', 515 '2':'GLONASS', 516 '3':'combined GPS/GLONASS', 517 '4':'Loran-C', 518 '5':'Chayka', 519 '6':'integrated navigation system', 520 '7':'surveyed', 521 } # fixtypeEncodeLut 522 523 dteEncodeLut = { 524 'available':'0', 525 'not available':'1', 526 } #dteEncodeLut 527 528 dteDecodeLut = { 529 '0':'available', 530 '1':'not available', 531 } # dteEncodeLut 532 533 534 535 ###################################################################### 536 # UNIT TESTING 537 ###################################################################### 538 import unittest
539 -def testParams():
540 '''Return a params file base on the testvalue tags. 541 @rtype: dict 542 @return: params based on testvalue tags 543 ''' 544 params = {} 545 params['MessageID'] = 5 546 params['RepeatIndicator'] = 1 547 params['UserID'] = 1193046 548 params['AISversion'] = 0 549 params['IMOnumber'] = 3210 550 params['callsign'] = 'PIRATE1' 551 params['name'] = 'BLACK PEARL@@@@@@@@@' 552 params['shipandcargo'] = 55 553 params['dimA'] = 10 554 params['dimB'] = 11 555 params['dimC'] = 12 556 params['dimD'] = 13 557 params['fixtype'] = 1 558 params['ETA'] = 0 559 params['draught'] = Decimal('21.1') 560 params['destination'] = 'NOWHERE@@@@@@@@@@@@@' 561 params['dte'] = 0 562 params['Spare'] = 0 563 564 return params
565
566 -class Testshipdata(unittest.TestCase):
567 '''Use testvalue tag text from each type to build test case the shipdata message'''
568 - def testEncodeDecode(self):
569 570 params = testParams() 571 bits = encode(params) 572 r = decode(bits) 573 574 # Check that each parameter came through ok. 575 self.failUnlessEqual(r['MessageID'],params['MessageID']) 576 self.failUnlessEqual(r['RepeatIndicator'],params['RepeatIndicator']) 577 self.failUnlessEqual(r['UserID'],params['UserID']) 578 self.failUnlessEqual(r['AISversion'],params['AISversion']) 579 self.failUnlessEqual(r['IMOnumber'],params['IMOnumber']) 580 self.failUnlessEqual(r['callsign'],params['callsign']) 581 self.failUnlessEqual(r['name'],params['name']) 582 self.failUnlessEqual(r['shipandcargo'],params['shipandcargo']) 583 self.failUnlessEqual(r['dimA'],params['dimA']) 584 self.failUnlessEqual(r['dimB'],params['dimB']) 585 self.failUnlessEqual(r['dimC'],params['dimC']) 586 self.failUnlessEqual(r['dimD'],params['dimD']) 587 self.failUnlessEqual(r['fixtype'],params['fixtype']) 588 self.failUnlessEqual(r['ETA'],params['ETA']) 589 self.failUnlessAlmostEqual(r['draught'],params['draught'],3) 590 self.failUnlessEqual(r['destination'],params['destination']) 591 self.failUnlessEqual(r['dte'],params['dte']) 592 self.failUnlessEqual(r['Spare'],params['Spare'])
593
594 -def addMsgOptions(parser):
595 parser.add_option('-d','--decode',dest='doDecode',default=False,action='store_true', 596 help='decode a "shipdata" AIS message') 597 parser.add_option('-e','--encode',dest='doEncode',default=False,action='store_true', 598 help='encode a "shipdata" AIS message') 599 parser.add_option('--RepeatIndicator-field', dest='RepeatIndicatorField',default=0,metavar='uint',type='int' 600 ,help='Field parameter value [default: %default]') 601 parser.add_option('--UserID-field', dest='UserIDField',metavar='uint',type='int' 602 ,help='Field parameter value [default: %default]') 603 parser.add_option('--AISversion-field', dest='AISversionField',default=0,metavar='uint',type='int' 604 ,help='Field parameter value [default: %default]') 605 parser.add_option('--IMOnumber-field', dest='IMOnumberField',default=0,metavar='uint',type='int' 606 ,help='Field parameter value [default: %default]') 607 parser.add_option('--callsign-field', dest='callsignField',default='@@@@@@@',metavar='aisstr6',type='string' 608 ,help='Field parameter value [default: %default]') 609 parser.add_option('--name-field', dest='nameField',default='@@@@@@@@@@@@@@@@@@@@',metavar='aisstr6',type='string' 610 ,help='Field parameter value [default: %default]') 611 parser.add_option('--shipandcargo-field', dest='shipandcargoField',default=0,metavar='uint',type='int' 612 ,help='Field parameter value [default: %default]') 613 parser.add_option('--dimA-field', dest='dimAField',metavar='uint',type='int' 614 ,help='Field parameter value [default: %default]') 615 parser.add_option('--dimB-field', dest='dimBField',metavar='uint',type='int' 616 ,help='Field parameter value [default: %default]') 617 parser.add_option('--dimC-field', dest='dimCField',metavar='uint',type='int' 618 ,help='Field parameter value [default: %default]') 619 parser.add_option('--dimD-field', dest='dimDField',metavar='uint',type='int' 620 ,help='Field parameter value [default: %default]') 621 parser.add_option('--fixtype-field', dest='fixtypeField',default=0,metavar='uint',type='int' 622 ,help='Field parameter value [default: %default]') 623 parser.add_option('--ETA-field', dest='ETAField',metavar='uint',type='int' 624 ,help='Field parameter value [default: %default]') 625 parser.add_option('--draught-field', dest='draughtField',default=Decimal('0'),metavar='udecimal',type='string' 626 ,help='Field parameter value [default: %default]') 627 parser.add_option('--destination-field', dest='destinationField',default='@@@@@@@@@@@@@@@@@@@@',metavar='aisstr6',type='string' 628 ,help='Field parameter value [default: %default]') 629 parser.add_option('--dte-field', dest='dteField',metavar='uint',type='int' 630 ,help='Field parameter value [default: %default]')
631 632 ############################################################ 633 if __name__=='__main__': 634 635 from optparse import OptionParser 636 parser = OptionParser(usage="%prog [options]", 637 version="%prog "+__version__) 638 639 parser.add_option('--doc-test',dest='doctest',default=False,action='store_true', 640 help='run the documentation tests') 641 parser.add_option('--unit-test',dest='unittest',default=False,action='store_true', 642 help='run the unit tests') 643 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', 644 help='Make the test output verbose') 645 646 # FIX: remove nmea from binary messages. No way to build the whole packet? 647 # FIX: or build the surrounding msg 8 for a broadcast? 648 typeChoices = ('binary','nmeapayload','nmea') # FIX: what about a USCG type message? 649 parser.add_option('-t','--type',choices=typeChoices,type='choice',dest='ioType' 650 ,default='nmeapayload' 651 ,help='What kind of string to expect ('+', '.join(typeChoices)+') [default: %default]') 652 653 654 outputChoices = ('std','html','csv' ) 655 parser.add_option('-T','--output-type',choices=outputChoices,type='choice',dest='outputType' 656 ,default='std' 657 ,help='What kind of string to output ('+', '.join(outputChoices)+') [default: %default]') 658 659 parser.add_option('-o','--output',dest='outputFileName',default=None, 660 help='Name of the python file to write [default: stdout]') 661 662 parser.add_option('-f','--fields',dest='fieldList',default=None, action='append', 663 choices=fieldList, 664 help='Which fields to include in the output. Currently only for csv output [default: all]') 665 666 parser.add_option('-p','--print-csv-field-list',dest='printCsvfieldList',default=False,action='store_true', 667 help='Print the field name for csv') 668 669 addMsgOptions(parser) 670 671 (options,args) = parser.parse_args() 672 success=True 673 674 if options.doctest: 675 import os; print os.path.basename(sys.argv[0]), 'doctests ...', 676 sys.argv= [sys.argv[0]] 677 if options.verbose: sys.argv.append('-v') 678 import doctest 679 numfail,numtests=doctest.testmod() 680 if numfail==0: print 'ok' 681 else: 682 print 'FAILED' 683 success=False 684 685 if not success: sys.exit('Something Failed') 686 del success # Hide success from epydoc 687 688 if options.unittest: 689 sys.argv = [sys.argv[0]] 690 if options.verbose: sys.argv.append('-v') 691 unittest.main() 692 693 outfile = sys.stdout 694 if None!=options.outputFileName: 695 outfile = file(options.outputFileName,'w') 696 697 698 if options.doEncode: 699 # First make sure all non required options are specified 700 if None==options.RepeatIndicatorField: parser.error("missing value for RepeatIndicatorField") 701 if None==options.UserIDField: parser.error("missing value for UserIDField") 702 if None==options.AISversionField: parser.error("missing value for AISversionField") 703 if None==options.IMOnumberField: parser.error("missing value for IMOnumberField") 704 if None==options.callsignField: parser.error("missing value for callsignField") 705 if None==options.nameField: parser.error("missing value for nameField") 706 if None==options.shipandcargoField: parser.error("missing value for shipandcargoField") 707 if None==options.dimAField: parser.error("missing value for dimAField") 708 if None==options.dimBField: parser.error("missing value for dimBField") 709 if None==options.dimCField: parser.error("missing value for dimCField") 710 if None==options.dimDField: parser.error("missing value for dimDField") 711 if None==options.fixtypeField: parser.error("missing value for fixtypeField") 712 if None==options.ETAField: parser.error("missing value for ETAField") 713 if None==options.draughtField: parser.error("missing value for draughtField") 714 if None==options.destinationField: parser.error("missing value for destinationField") 715 if None==options.dteField: parser.error("missing value for dteField") 716 msgDict={ 717 'MessageID': '5', 718 'RepeatIndicator': options.RepeatIndicatorField, 719 'UserID': options.UserIDField, 720 'AISversion': options.AISversionField, 721 'IMOnumber': options.IMOnumberField, 722 'callsign': options.callsignField, 723 'name': options.nameField, 724 'shipandcargo': options.shipandcargoField, 725 'dimA': options.dimAField, 726 'dimB': options.dimBField, 727 'dimC': options.dimCField, 728 'dimD': options.dimDField, 729 'fixtype': options.fixtypeField, 730 'ETA': options.ETAField, 731 'draught': options.draughtField, 732 'destination': options.destinationField, 733 'dte': options.dteField, 734 'Spare': '0', 735 } 736 737 bits = encode(msgDict) 738 if 'binary'==options.ioType: print str(bits) 739 elif 'nmeapayload'==options.ioType: 740 # FIX: figure out if this might be necessary at compile time 741 print "bitLen",len(bits) 742 bitLen=len(bits) 743 if bitLen%6!=0: 744 bits = bits + BitVector(size=(6 - (bitLen%6))) # Pad out to multiple of 6 745 print "result:",binary.bitvectoais6(bits)[0] 746 747 748 # FIX: Do not emit this option for the binary message payloads. Does not make sense. 749 elif 'nmea'==options.ioType: sys.exit("FIX: need to implement this capability") 750 else: sys.exit('ERROR: unknown ioType. Help!') 751 752 if options.printCsvfieldList: 753 # Make a csv separated list of fields that will be displayed for csv 754 if None == options.fieldList: options.fieldList = fieldList 755 import StringIO 756 buf = StringIO.StringIO() 757 for field in options.fieldList: 758 buf.write(field+',') 759 result = buf.getvalue() 760 if result[-1] == ',': print result[:-1] 761 else: print result 762 763 if options.doDecode: 764 for msg in args: 765 bv = None 766 if 'binary' == options.ioType: bv = BitVector(bitstring=msg) 767 elif 'nmeapayload'== options.ioType: bv = binary.ais6tobitvec(msg) 768 elif 'nmea' == options.ioType: bv = binary.ais6tobitvec(msg.split(',')[5]) 769 else: sys.exit('ERROR: unknown ioType. Help!') 770 771 printFields(decode(bv),out=outfile,format=options.outputType,fieldList=options.fieldList) 772