<s> """<STR_LIT>""" <EOL> import operator <EOL> from twisted . protocols import basic <EOL> from twisted . python . compat import reduce <EOL> POSFIX_INVALID , POSFIX_SPS , POSFIX_DGPS , POSFIX_PPS = <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> <EOL> MODE_AUTO , MODE_FORCED = '<STR_LIT:A>' , '<STR_LIT:M>' <EOL> MODE_NOFIX , MODE_2D , MODE_3D = <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> <EOL> class InvalidSentence ( Exception ) : <EOL> pass <EOL> class InvalidChecksum ( Exception ) : <EOL> pass <EOL> class NMEAReceiver ( basic . LineReceiver ) : <EOL> """<STR_LIT>""" <EOL> delimiter = '<STR_LIT:\r\n>' <EOL> dispatch = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:time>' , <EOL> } <EOL> ignore_invalid_sentence = <NUM_LIT:1> <EOL> ignore_checksum_mismatch = <NUM_LIT:0> <EOL> ignore_unknown_sentencetypes = <NUM_LIT:0> <EOL> convert_dates_before_y2k = <NUM_LIT:1> <EOL> def lineReceived ( self , line ) : <EOL> if not line . startswith ( '<STR_LIT:$>' ) : <EOL> if self . ignore_invalid_sentence : <EOL> return <EOL> raise InvalidSentence ( "<STR_LIT>" % ( line , ) ) <EOL> strmessage , checksum = line [ <NUM_LIT:1> : ] . strip ( ) . split ( '<STR_LIT:*>' ) <EOL> message = strmessage . split ( '<STR_LIT:U+002C>' ) <EOL> sentencetype , message = message [ <NUM_LIT:0> ] , message [ <NUM_LIT:1> : ] <EOL> dispatch = self . dispatch . get ( sentencetype , None ) <EOL> if ( not dispatch ) and ( not self . ignore_unknown_sentencetypes ) : <EOL> raise InvalidSentence ( "<STR_LIT>" % ( sentencetype , ) ) <EOL> if not self . ignore_checksum_mismatch : <EOL> checksum , calculated_checksum = int ( checksum , <NUM_LIT:16> ) , reduce ( operator . xor , map ( ord , strmessage ) ) <EOL> if checksum != calculated_checksum : <EOL> raise InvalidChecksum ( "<STR_LIT>" % ( checksum , calculated_checksum ) ) <EOL> handler = getattr ( self , "<STR_LIT>" % dispatch , None ) <EOL> decoder = getattr ( self , "<STR_LIT>" % dispatch , None ) <EOL> if not ( dispatch and handler and decoder ) : <EOL> return <EOL> try : <EOL> decoded = decoder ( * message ) <EOL> except Exception , e : <EOL> raise InvalidSentence ( "<STR_LIT>" % ( line , sentencetype , dispatch ) ) <EOL> return handler ( * decoded ) <EOL> def decode_position ( self , latitude , ns , longitude , ew , utc , status ) : <EOL> latitude , longitude = self . _decode_latlon ( latitude , ns , longitude , ew ) <EOL> utc = self . _decode_utc ( utc ) <EOL> if status == '<STR_LIT:A>' : <EOL> status = <NUM_LIT:1> <EOL> else : <EOL> status = <NUM_LIT:0> <EOL> return ( <EOL> latitude , <EOL> longitude , <EOL> utc , <EOL> status , <EOL> ) <EOL> def decode_positiontime ( self , utc , status , latitude , ns , longitude , ew , speed , course , utcdate , magvar , magdir ) : <EOL> utc = self . _decode_utc ( utc ) <EOL> latitude , longitude = self . _decode_latlon ( latitude , ns , longitude , ew ) <EOL> if speed != '<STR_LIT>' : <EOL> speed = float ( speed ) <EOL> else : <EOL> speed = None <EOL> if course != '<STR_LIT>' : <EOL> course = float ( course ) <EOL> else : <EOL> course = None <EOL> utcdate = <NUM_LIT> + int ( utcdate [ <NUM_LIT:4> : <NUM_LIT:6> ] ) , int ( utcdate [ <NUM_LIT:2> : <NUM_LIT:4> ] ) , int ( utcdate [ <NUM_LIT:0> : <NUM_LIT:2> ] ) <EOL> if self . convert_dates_before_y2k and utcdate [ <NUM_LIT:0> ] > <NUM_LIT> : <EOL> utcdate = ( utcdate [ <NUM_LIT:0> ] - <NUM_LIT:100> , utcdate [ <NUM_LIT:1> ] , utcdate [ <NUM_LIT:2> ] ) <EOL> if magvar != '<STR_LIT>' : <EOL> magvar = float ( magvar ) <EOL> if magdir == '<STR_LIT>' : <EOL> magvar = - magvar <EOL> else : <EOL> magvar = None <EOL> return ( <EOL> latitude , <EOL> longitude , <EOL> speed , <EOL> course , <EOL> utc , <EOL> utcdate , <EOL> magvar , <EOL> ) <EOL> def _decode_utc ( self , utc ) : <EOL> utc_hh , utc_mm , utc_ss = map ( float , ( utc [ : <NUM_LIT:2> ] , utc [ <NUM_LIT:2> : <NUM_LIT:4> ] , utc [ <NUM_LIT:4> : ] ) ) <EOL> return utc_hh * <NUM_LIT> + utc_mm * <NUM_LIT> + utc_ss <EOL> def _decode_latlon ( self , latitude , ns , longitude , ew ) : <EOL> latitude = float ( latitude [ : <NUM_LIT:2> ] ) + float ( latitude [ <NUM_LIT:2> : ] ) / <NUM_LIT> <EOL> if ns == '<STR_LIT:S>' : <EOL> latitude = - latitude <EOL> longitude = float ( longitude [ : <NUM_LIT:3> ] ) + float ( longitude [ <NUM_LIT:3> : ] ) / <NUM_LIT> <EOL> if ew == '<STR_LIT>' : <EOL> longitude = - longitude <EOL> return ( latitude , longitude ) <EOL> def decode_activesatellites ( self , mode1 , mode2 , * args ) : <EOL> satellites , ( pdop , hdop , vdop ) = args [ : <NUM_LIT:12> ] , map ( float , args [ <NUM_LIT:12> : ] ) <EOL> satlist = [ ] <EOL> for n in satellites : <EOL> if n : <EOL> satlist . append ( int ( n ) ) <EOL> else : <EOL> satlist . append ( None ) <EOL> mode = ( mode1 , int ( mode2 ) ) <EOL> return ( <EOL> tuple ( satlist ) , <EOL> mode , <EOL> pdop , <EOL> hdop , <EOL> vdop , <EOL> ) <EOL> def decode_fix ( self , utc , latitude , ns , longitude , ew , posfix , satellites , hdop , altitude , altitude_units , geoid_separation , geoid_separation_units , dgps_age , dgps_station_id ) : <EOL> latitude , longitude = self . _decode_latlon ( latitude , ns , longitude , ew ) <EOL> utc = self . _decode_utc ( utc ) <EOL> posfix = int ( posfix ) <EOL> satellites = int ( satellites ) <EOL> hdop = float ( hdop ) <EOL> altitude = ( float ( altitude ) , altitude_units ) <EOL> if geoid_separation != '<STR_LIT>' : <EOL> geoid = ( float ( geoid_separation ) , geoid_separation_units ) <EOL> else : <EOL> geoid = None <EOL> if dgps_age != '<STR_LIT>' : <EOL> dgps = ( float ( dgps_age ) , dgps_station_id ) <EOL> else : <EOL> dgps = None <EOL> return ( <EOL> utc , <EOL> latitude , <EOL> longitude , <EOL> posfix , <EOL> satellites , <EOL> hdop , <EOL> altitude , <EOL> geoid , <EOL> dgps , <EOL> ) </s>
