1
2
3 __version__ = '$Revision: 4791 $'.split()[1]
4 __date__ = '$Date: 2006-12-12 $'.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__ myparser
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 TrueBV = BitVector(bitstring="1")
42 "Why always rebuild the True bit? This should speed things up a bunch"
43 FalseBV = BitVector(bitstring="0")
44 "Why always rebuild the False bit? This should speed things up a bunch"
45
46
48 '''Create a position binary message payload to pack into an AIS Msg position.
49
50 Fields in params:
51 - MessageID(uint): AIS message number. Must be 1 (field automatically set to "1")
52 - RepeatIndicator(uint): Indicated how many times a message has been repeated
53 - UserID(uint): Unique ship identification number (MMSI)
54 - NavigationStatus(uint): What is the vessel doing
55 - ROT(int): RateOfTurn
56 - SOG(udecimal): Speed over ground
57 - PositionAccuracy(uint): Accuracy of positioning fixes
58 - Position_longitude(decimal): Location of the vessel East West location
59 - Position_latitude(decimal): Location of the vessel North South location
60 - COG(udecimal): Course over ground
61 - TrueHeading(uint): True heading (relative to true North)
62 - TimeStamp(uint): UTC second when the report was generated
63 - RegionalReserved(uint): Reserved for definition by a regional authority. (field automatically set to "0")
64 - Spare(uint): Reserved for definition by a regional authority. (field automatically set to "0")
65 - RAIM(bool): Receiver autonomous integrity monitoring flag
66 - syncstate(uint): Sycronization state
67 - slotoffset(uint): In what slot will the next transmission occur. BROKEN
68 @param params: Dictionary of field names/values. Throws a ValueError exception if required is missing
69 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented.
70 @rtype: BitVector
71 @return: encoded binary message (for binary messages, this needs to be wrapped in a msg 8
72 '''
73
74 bvList = []
75 bvList.append(binary.setBitVectorSize(BitVector(intVal=1),6))
76 if 'RepeatIndicator' in params:
77 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['RepeatIndicator']),2))
78 else:
79 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),2))
80 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['UserID']),30))
81 if 'NavigationStatus' in params:
82 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['NavigationStatus']),4))
83 else:
84 bvList.append(binary.setBitVectorSize(BitVector(intVal=15),4))
85 if 'ROT' in params:
86 bvList.append(binary.bvFromSignedInt(params['ROT'],8))
87 else:
88 bvList.append(binary.bvFromSignedInt(-128,8))
89 if 'SOG' in params:
90 bvList.append(binary.setBitVectorSize(BitVector(intVal=int((Decimal(params['SOG'])*Decimal('10')))),10))
91 else:
92 bvList.append(binary.setBitVectorSize(BitVector(intVal=int(1023)),10))
93 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['PositionAccuracy']),1))
94 if 'Position_longitude' in params:
95 bvList.append(binary.bvFromSignedInt(int(Decimal(params['Position_longitude'])*Decimal('600000')),28))
96 else:
97 bvList.append(binary.bvFromSignedInt(108600000,28))
98 if 'Position_latitude' in params:
99 bvList.append(binary.bvFromSignedInt(int(Decimal(params['Position_latitude'])*Decimal('600000')),27))
100 else:
101 bvList.append(binary.bvFromSignedInt(54600000,27))
102 if 'COG' in params:
103 bvList.append(binary.setBitVectorSize(BitVector(intVal=int((Decimal(params['COG'])*Decimal('10')))),12))
104 else:
105 bvList.append(binary.setBitVectorSize(BitVector(intVal=int(3600)),12))
106 if 'TrueHeading' in params:
107 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['TrueHeading']),9))
108 else:
109 bvList.append(binary.setBitVectorSize(BitVector(intVal=511),9))
110 if 'TimeStamp' in params:
111 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['TimeStamp']),6))
112 else:
113 bvList.append(binary.setBitVectorSize(BitVector(intVal=60),6))
114 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),4))
115 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),1))
116 if params["RAIM"]: bvList.append(TrueBV)
117 else: bvList.append(FalseBV)
118 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['syncstate']),2))
119 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['slotoffset']),14))
120
121 return binary.joinBV(bvList)
122
124 '''Unpack a position message
125
126 Fields in params:
127 - MessageID(uint): AIS message number. Must be 1 (field automatically set to "1")
128 - RepeatIndicator(uint): Indicated how many times a message has been repeated
129 - UserID(uint): Unique ship identification number (MMSI)
130 - NavigationStatus(uint): What is the vessel doing
131 - ROT(int): RateOfTurn
132 - SOG(udecimal): Speed over ground
133 - PositionAccuracy(uint): Accuracy of positioning fixes
134 - Position_longitude(decimal): Location of the vessel East West location
135 - Position_latitude(decimal): Location of the vessel North South location
136 - COG(udecimal): Course over ground
137 - TrueHeading(uint): True heading (relative to true North)
138 - TimeStamp(uint): UTC second when the report was generated
139 - RegionalReserved(uint): Reserved for definition by a regional authority. (field automatically set to "0")
140 - Spare(uint): Reserved for definition by a regional authority. (field automatically set to "0")
141 - RAIM(bool): Receiver autonomous integrity monitoring flag
142 - syncstate(uint): Sycronization state
143 - slotoffset(uint): In what slot will the next transmission occur. BROKEN
144 @type bv: BitVector
145 @param bv: Bits defining a message
146 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented.
147 @rtype: dict
148 @return: params
149 '''
150
151
152
153
154 r = {}
155 r['MessageID']=1
156 r['RepeatIndicator']=int(bv[6:8])
157 r['UserID']=int(bv[8:38])
158 r['NavigationStatus']=int(bv[38:42])
159 r['ROT']=binary.signedIntFromBV(bv[42:50])
160 r['SOG']=Decimal(int(bv[50:60]))/Decimal('10')
161 r['PositionAccuracy']=int(bv[60:61])
162 r['Position_longitude']=Decimal(binary.signedIntFromBV(bv[61:89]))/Decimal('600000')
163 r['Position_latitude']=Decimal(binary.signedIntFromBV(bv[89:116]))/Decimal('600000')
164 r['COG']=Decimal(int(bv[116:128]))/Decimal('10')
165 r['TrueHeading']=int(bv[128:137])
166 r['TimeStamp']=int(bv[137:143])
167 r['RegionalReserved']=0
168 r['Spare']=0
169 r['RAIM']=bool(int(bv[148:149]))
170 r['syncstate']=int(bv[149:151])
171 r['slotoffset']=int(bv[151:165])
172 return r
173
175 '''Print a position message to stdout.
176
177 Fields in params:
178 - MessageID(uint): AIS message number. Must be 1 (field automatically set to "1")
179 - RepeatIndicator(uint): Indicated how many times a message has been repeated
180 - UserID(uint): Unique ship identification number (MMSI)
181 - NavigationStatus(uint): What is the vessel doing
182 - ROT(int): RateOfTurn
183 - SOG(udecimal): Speed over ground
184 - PositionAccuracy(uint): Accuracy of positioning fixes
185 - Position_longitude(decimal): Location of the vessel East West location
186 - Position_latitude(decimal): Location of the vessel North South location
187 - COG(udecimal): Course over ground
188 - TrueHeading(uint): True heading (relative to true North)
189 - TimeStamp(uint): UTC second when the report was generated
190 - RegionalReserved(uint): Reserved for definition by a regional authority. (field automatically set to "0")
191 - Spare(uint): Reserved for definition by a regional authority. (field automatically set to "0")
192 - RAIM(bool): Receiver autonomous integrity monitoring flag
193 - syncstate(uint): Sycronization state
194 - slotoffset(uint): In what slot will the next transmission occur. BROKEN
195 @param params: Dictionary of field names/values.
196 @param out: File like object to write to
197 @rtype: stdout
198 @return: text to out
199 '''
200
201 out.write("position:\n")
202 out.write(" MessageID: "+str(+params['MessageID'])+"\n")
203 out.write(" RepeatIndicator: "+str(+params['RepeatIndicator'])+"\n")
204 out.write(" UserID: "+str(+params['UserID'])+"\n")
205 out.write(" NavigationStatus: "+str(+params['NavigationStatus'])+"\n")
206 out.write(" ROT: "+str(+params['ROT'])+"\n")
207 out.write(" SOG: "+str(+params['SOG'])+"\n")
208 out.write(" PositionAccuracy: "+str(+params['PositionAccuracy'])+"\n")
209 out.write(" Position_longitude: "+str(+params['Position_longitude'])+"\n")
210 out.write(" Position_latitude: "+str(+params['Position_latitude'])+"\n")
211 out.write(" COG: "+str(+params['COG'])+"\n")
212 out.write(" TrueHeading: "+str(+params['TrueHeading'])+"\n")
213 out.write(" TimeStamp: "+str(+params['TimeStamp'])+"\n")
214 out.write(" RegionalReserved: "+str(+params['RegionalReserved'])+"\n")
215 out.write(" Spare: "+str(+params['Spare'])+"\n")
216 out.write(" RAIM: "+str(+params['RAIM'])+"\n")
217 out.write(" syncstate: "+str(+params['syncstate'])+"\n")
218 out.write(" slotoffset: "+str(+params['slotoffset'])+"\n")
219
220 return
221
222
223
224
225
226
227 import unittest
229 '''Return a params file base on the testvalue tags.
230 @rtype: dict
231 @return: params based on testvalue tags
232 '''
233 params = {}
234 params['MessageID'] = 1
235 params['RepeatIndicator'] = 1
236 params['UserID'] = 1193046
237 params['NavigationStatus'] = 3
238 params['ROT'] = -2
239 params['SOG'] = Decimal('101.9')
240 params['PositionAccuracy'] = 1
241 params['Position_longitude'] = Decimal('-122.16328055555556')
242 params['Position_latitude'] = Decimal('37.424458333333334')
243 params['COG'] = Decimal('34.5')
244 params['TrueHeading'] = 41
245 params['TimeStamp'] = 35
246 params['RegionalReserved'] = 0
247 params['Spare'] = 0
248 params['RAIM'] = False
249 params['syncstate'] = 2
250 params['slotoffset'] = 1221
251
252 return params
253
255 '''Uses the testvalue tag text from each type to build a test case for the position message'''
257
258 params = positionTestParams()
259 bits = positionEncode(params)
260 r = positionDecode(bits)
261
262
263 self.failUnlessEqual(r['MessageID'],params['MessageID'])
264 self.failUnlessEqual(r['RepeatIndicator'],params['RepeatIndicator'])
265 self.failUnlessEqual(r['UserID'],params['UserID'])
266 self.failUnlessEqual(r['NavigationStatus'],params['NavigationStatus'])
267 self.failUnlessEqual(r['ROT'],params['ROT'])
268 self.failUnlessAlmostEqual(r['SOG'],params['SOG'],1)
269 self.failUnlessEqual(r['PositionAccuracy'],params['PositionAccuracy'])
270 self.failUnlessAlmostEqual(r['Position_longitude'],params['Position_longitude'],5)
271 self.failUnlessAlmostEqual(r['Position_latitude'],params['Position_latitude'],5)
272 self.failUnlessAlmostEqual(r['COG'],params['COG'],7)
273 self.failUnlessEqual(r['TrueHeading'],params['TrueHeading'])
274 self.failUnlessEqual(r['TimeStamp'],params['TimeStamp'])
275 self.failUnlessEqual(r['RegionalReserved'],params['RegionalReserved'])
276 self.failUnlessEqual(r['Spare'],params['Spare'])
277 self.failUnlessEqual(r['RAIM'],params['RAIM'])
278 self.failUnlessEqual(r['syncstate'],params['syncstate'])
279 self.failUnlessEqual(r['slotoffset'],params['slotoffset'])
280
281
282 if __name__=='__main__':
283
284 from optparse import OptionParser
285 myparser = OptionParser(usage="%prog [options]",
286 version="%prog "+__version__)
287
288
289 myparser.add_option('--doc-test',dest='doctest',default=False,action='store_true',
290 help='run the documentation tests')
291 myparser.add_option('--unit-test',dest='unittest',default=False,action='store_true',
292 help='run the unit tests')
293 myparser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true',
294 help='Make the test output verbose')
295
296 (options,args) = myparser.parse_args()
297 success=True
298
299 if options.doctest:
300 import os; print os.path.basename(sys.argv[0]), 'doctests ...',
301 sys.argv= [sys.argv[0]]
302 if options.verbose: sys.argv.append('-v')
303 import doctest
304 numfail,numtests=doctest.testmod()
305 if numfail==0: print 'ok'
306 else:
307 print 'FAILED'
308 success=False
309
310 if not success:
311 sys.exit('Something Failed')
312
313 del success
314
315 if options.unittest:
316 sys.argv = [sys.argv[0]]
317 if options.verbose: sys.argv.append('-v')
318 unittest.main()
319