Ignore:
Timestamp:
11/13/12 16:52:27 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

#139: the transponder state is now queried and made available

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/pyuipc_sim.py

    r321 r334  
    186186        bcd |= value % 10
    187187        return bcd
    188        
     188
    189189    @staticmethod
    190190    def _writeFrequency(value):
     
    207207        if isMain: mainFrequency = value
    208208        else: extFrequency = value
    209        
     209
    210210        return Values._toADFFrequency(mainFrequency, extFrequency)
    211211
     
    222222
    223223        return bcd
    224        
     224
    225225    def __init__(self):
    226226        """Construct the values with defaults."""
     
    235235        self.latitude = 47.5
    236236        self.longitude = 19.05
    237        
     237
    238238        self.paused = False
    239239        self.frozen = False
     
    243243        self.stalled = False
    244244        self.onTheGround = True
    245        
     245
    246246        self.zfw = 50000.0
    247        
     247
    248248        self.fuelWeights = [0.0, 3000.0, 3000.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    249249        # Wikipedia: "Jet fuel", Jet A-1 density at 15*C .804kg/l -> 6.7 pounds/gallon
     
    293293        self.windDirection = 300.0
    294294        self.visibility = 10000
    295        
     295
    296296        self.n1 = [0.0, 0.0, 0.0]
    297297        self.throttles = [0.0, 0.0, 0.0]
     
    313313        self.pmdg_737ng_switches = 0
    314314        self.pmdg_737ngx_lts_positionsw = 0
     315
     316        self.xpdrC = False
    315317
    316318    def read(self, offset, type):
     
    447449                           (self.flapsNotches[index+1] - self.flapsNotches[index]))
    448450        elif offset==0x0be0 or offset==0x0be4:    # Flaps left and  right
    449             return self.flaps * 16383.0 / self.flapsNotches[-1]       
     451            return self.flaps * 16383.0 / self.flapsNotches[-1]
    450452        elif offset==0x0be8:       # Gear control
    451453            return int(self.gearControl * 16383.0)
     
    504506            return (self.zfw + sum(self.fuelWeights)) * const.KGSTOLB
    505507        elif offset==0x31e4:       # Radio altitude
    506             # FIXME: if self.radioAltitude is None, calculate from the 
     508            # FIXME: if self.radioAltitude is None, calculate from the
    507509            # altitude with some, perhaps random, ground altitude
    508510            # value
     
    551553        elif offset==0x6500:       # PMDG 737NGX lights position SW
    552554            return self.pmdg_737ngx_lts_positionsw
     555        elif offset==0x7b91:       # Transponder standby
     556            return 0 if self.xpdrC else 1
    553557        else:
    554558            print "Unhandled offset: %04x" % (offset,)
     
    697701            self.navLightsOn = (value&0x01)!=0
    698702            self.antiCollisionLightsOn = (value&0x02)!=0
    699             self.landingLightsOn = (value&0x04)!=0 
    700             self.strobeLightsOn = (value&0x10)!=0 
     703            self.landingLightsOn = (value&0x04)!=0
     704            self.strobeLightsOn = (value&0x10)!=0
    701705        elif offset==0x0e8a:       # Visibility
    702706            self.visibility = value * 1609.344 / 100.0
     
    781785        elif offset==0x6500:       # PMDG 737NGX lights position SW
    782786            self.pmdg_737ngx_lts_positionsw = value
     787        elif offset==0x7b91:       # Transponder standby
     788            self.xpdrC = value==0
    783789        else:
    784790            print "Unhandled offset: %04x" % (offset,)
    785791            raise FSUIPCException(ERR_DATA)
    786        
     792
    787793    def _readUTC(self):
    788794        """Read the UTC time.
    789        
     795
    790796        The current offset is added to it."""
    791797        return time.gmtime(time.time() + self._timeOffset)
    792        
     798
    793799    def _getFuelLevel(self, index):
    794800        """Get the fuel level for the fuel tank with the given
     
    796802        return 0 if self.fuelCapacities[index]==0.0 else \
    797803            int(self.fuelWeights[index] * 65536.0 * 128.0 / self.fuelCapacities[index])
    798    
     804
    799805    def _getFuelCapacity(self, index):
    800806        """Get the capacity of the fuel tank with the given index."""
     
    811817        tm1 = tm[:index] + (value,) + tm[(index+1):]
    812818        self._timeOffset += calendar.timegm(tm1) - calendar.timegm(tm)
    813    
     819
    814820    def _setThrottle(self, index, value):
    815821        """Set the throttle value for the given index."""
     
    820826        self.fuelWeights[index] = self.fuelCapacities[index] * float(value) / \
    821827                                  65536.0 / 128.0
    822    
     828
    823829    def _setFuelCapacity(self, index, value):
    824830        """Set the capacity of the fuel tank with the given index."""
     
    830836                                      const.FEETTOMETRES,
    831837                                      5.25588)
    832         temperature = 15 - self.altitude * 6.5 * const.FEETTOMETRES / 1000.0 
     838        temperature = 15 - self.altitude * 6.5 * const.FEETTOMETRES / 1000.0
    833839        temperature += 273.15  # Celsius -> Kelvin
    834840        airDensity = pressure / (temperature * 287.05)
     
    868874    else:
    869875        raise FSUIPCException(ERR_NOTOPEN)
    870        
     876
    871877#------------------------------------------------------------------------------
    872878
     
    877883    else:
    878884        raise FSUIPCException(ERR_NOTOPEN)
    879            
     885
    880886#------------------------------------------------------------------------------
    881887
     
    887893    else:
    888894        raise FSUIPCException(ERR_NOTOPEN)
    889            
     895
    890896#------------------------------------------------------------------------------
    891897
     
    924930        serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    925931        serverSocket.bind(("", PORT))
    926    
     932
    927933        serverSocket.listen(5)
    928    
     934
    929935        while True:
    930936            (clientSocket, clientAddress) = serverSocket.accept()
     
    936942        socketFile = clientSocket.makefile()
    937943        try:
    938             while True:               
     944            while True:
    939945                (length,) = struct.unpack("I", clientSocket.recv(4))
    940946                data = clientSocket.recv(length)
    941947                (call, args) = cPickle.loads(data)
    942948                exception = None
    943                
     949
    944950                try:
    945951                    if call==CALL_READ:
     
    977983                pass
    978984            clientSocket.close()
    979                    
     985
    980986#------------------------------------------------------------------------------
    981987
     
    10131019        """Quit from the simulator."""
    10141020        data = cPickle.dumps((CALL_QUIT, None))
    1015         self._socket.send(struct.pack("I", len(data)) + data)       
     1021        self._socket.send(struct.pack("I", len(data)) + data)
    10161022
    10171023    def _call(self, command, data):
     
    10371043        """Convert the given string to a PyUIPC boolean value (i.e. 0 or 1)."""
    10381044        return 1 if s in ["yes", "true", "on"] else 0
    1039    
     1045
    10401046    @staticmethod
    10411047    def bool2str(value):
     
    10471053        """Convert the given degree (as a string) into a PyUIPC value."""
    10481054        return int(float(degree) * 65536.0 * 65536.0 / 360.0)
    1049        
     1055
    10501056    @staticmethod
    10511057    def pyuipc2degree(value):
     
    10571063        """Convert the given percentage value (as a string) into a PyUIPC value."""
    10581064        return int(float(level) * 128.0 * 65536.0 / 100.0)
    1059        
     1065
    10601066    @staticmethod
    10611067    def pyuipc2fuelLevel(value):
    10621068        """Convert the PyUIPC value into a percentage value."""
    10631069        return value * 100.0 / 128.0 / 65536.0
    1064        
     1070
    10651071    @staticmethod
    10661072    def fuelCapacity2pyuipc(capacity):
    10671073        """Convert the given capacity value (as a string) into a PyUIPC value."""
    10681074        return int(capacity)
    1069        
     1075
    10701076    @staticmethod
    10711077    def pyuipc2fuelCapacity(value):
    10721078        """Convert the given capacity value into a PyUIPC value."""
    10731079        return value
    1074        
     1080
    10751081    @staticmethod
    10761082    def throttle2pyuipc(throttle):
    10771083        """Convert the given throttle value (as a string) into a PyUIPC value."""
    10781084        return int(float(throttle) * 16384.0 / 100.0)
    1079        
     1085
    10801086    @staticmethod
    10811087    def pyuipc2throttle(value):
     
    10861092        """Construct the CLI."""
    10871093        cmd.Cmd.__init__(self)
    1088        
     1094
    10891095        self.use_rawinput = True
    10901096        self.intro = "\nPyUIPC simulator command prompt\n"
     
    11581164                                     lambda word: None)
    11591165        self._valueHandlers["vs"] = ([(0x02c8, "d")],
    1160                                      lambda value: value * 60 /                                     
     1166                                     lambda value: value * 60 /
    11611167                                     const.FEETTOMETRES / 256.0,
    11621168                                     lambda word: int(float(word) *
     
    11641170                                                      256.0 / 60.0))
    11651171        self._valueHandlers["tdRate"] = ([(0x030c, "d")],
    1166                                          lambda value: value * 60 /                                     
     1172                                         lambda value: value * 60 /
    11671173                                         const.FEETTOMETRES / 256.0,
    11681174                                         lambda word: int(float(word) *
     
    13311337                                             CLI.pyuipc2throttle,
    13321338                                             CLI.throttle2pyuipc)
    1333                                                            
     1339
    13341340        self._valueHandlers["visibility"] = ([(0x0e8a, "H")],
    13351341                                             lambda value: value*1609.344/100.0,
    13361342                                             lambda word: int(float(word)*
    13371343                                                              100.0/1609.344))
    1338                                                            
     1344
    13391345        self._valueHandlers["payloadCount"] = ([(0x13fc, "d")],
    13401346                                               lambda value: value,
     
    13481354        self._valueHandlers["textScrolling"] = ([(0x1274, "h")],
    13491355                                                CLI.bool2str, CLI.str2bool)
    1350                                                            
     1356
    13511357        self._valueHandlers["messageDuration"] = ([(0x32fa, "h")],
    13521358                                                  lambda value: value,
     
    13711377                                                             lambda value: value,
    13721378                                                             lambda word: int(word))
     1379        self._valueHandlers["xpdrC"] = ([(0x7b91, "b")],
     1380                                        lambda value: value,
     1381                                        lambda word: int(word))
    13731382
    13741383    def default(self, line):
     
    13831392        """Handle the get command."""
    13841393        names = args.split()
    1385         data = []       
     1394        data = []
    13861395        for name in names:
    13871396            if name not in self._valueHandlers:
     
    14081417                i+=1
    14091418        except Exception, e:
    1410             print >> sys.stderr, "Failed to read data: " + str(e)                       
     1419            print >> sys.stderr, "Failed to read data: " + str(e)
    14111420
    14121421        return False
     
    14401449        if inWord:
    14411450            arguments.append(word)
    1442            
     1451
    14431452        names = []
    14441453        data = []
     
    14671476                      (value, name, str(e))
    14681477                return False
    1469                
     1478
    14701479        try:
    14711480            self._client.write(data)
     
    15081517        except Exception, e:
    15091518            print >> sys.stderr, "Failed to close the connection: " + str(e)
    1510        
     1519
    15111520    def do_failopen(self, args):
    15121521        """Enable/disable the failing of opens."""
     
    15161525            print "Opening will%s fail" % ("" if value else " not",)
    15171526        except Exception, e:
    1518             print >> sys.stderr, "Failed to set open failure: " + str(e)       
     1527            print >> sys.stderr, "Failed to set open failure: " + str(e)
    15191528
    15201529    def help_failopen(self, usage = False):
     
    15301539        else:
    15311540            return ["yes", "no"]
    1532        
     1541
    15331542    def do_quit(self, args):
    15341543        """Handle the quit command."""
     
    15421551else:
    15431552    server = Server()
    1544     server.start()   
    1545 
    1546 #------------------------------------------------------------------------------
     1553    server.start()
     1554
     1555#------------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.