Changeset 334:7978a2496c31 for src/mlx/pyuipc_sim.py
- Timestamp:
- 11/13/12 16:52:27 (11 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mlx/pyuipc_sim.py
r321 r334 186 186 bcd |= value % 10 187 187 return bcd 188 188 189 189 @staticmethod 190 190 def _writeFrequency(value): … … 207 207 if isMain: mainFrequency = value 208 208 else: extFrequency = value 209 209 210 210 return Values._toADFFrequency(mainFrequency, extFrequency) 211 211 … … 222 222 223 223 return bcd 224 224 225 225 def __init__(self): 226 226 """Construct the values with defaults.""" … … 235 235 self.latitude = 47.5 236 236 self.longitude = 19.05 237 237 238 238 self.paused = False 239 239 self.frozen = False … … 243 243 self.stalled = False 244 244 self.onTheGround = True 245 245 246 246 self.zfw = 50000.0 247 247 248 248 self.fuelWeights = [0.0, 3000.0, 3000.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] 249 249 # Wikipedia: "Jet fuel", Jet A-1 density at 15*C .804kg/l -> 6.7 pounds/gallon … … 293 293 self.windDirection = 300.0 294 294 self.visibility = 10000 295 295 296 296 self.n1 = [0.0, 0.0, 0.0] 297 297 self.throttles = [0.0, 0.0, 0.0] … … 313 313 self.pmdg_737ng_switches = 0 314 314 self.pmdg_737ngx_lts_positionsw = 0 315 316 self.xpdrC = False 315 317 316 318 def read(self, offset, type): … … 447 449 (self.flapsNotches[index+1] - self.flapsNotches[index])) 448 450 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] 450 452 elif offset==0x0be8: # Gear control 451 453 return int(self.gearControl * 16383.0) … … 504 506 return (self.zfw + sum(self.fuelWeights)) * const.KGSTOLB 505 507 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 507 509 # altitude with some, perhaps random, ground altitude 508 510 # value … … 551 553 elif offset==0x6500: # PMDG 737NGX lights position SW 552 554 return self.pmdg_737ngx_lts_positionsw 555 elif offset==0x7b91: # Transponder standby 556 return 0 if self.xpdrC else 1 553 557 else: 554 558 print "Unhandled offset: %04x" % (offset,) … … 697 701 self.navLightsOn = (value&0x01)!=0 698 702 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 701 705 elif offset==0x0e8a: # Visibility 702 706 self.visibility = value * 1609.344 / 100.0 … … 781 785 elif offset==0x6500: # PMDG 737NGX lights position SW 782 786 self.pmdg_737ngx_lts_positionsw = value 787 elif offset==0x7b91: # Transponder standby 788 self.xpdrC = value==0 783 789 else: 784 790 print "Unhandled offset: %04x" % (offset,) 785 791 raise FSUIPCException(ERR_DATA) 786 792 787 793 def _readUTC(self): 788 794 """Read the UTC time. 789 795 790 796 The current offset is added to it.""" 791 797 return time.gmtime(time.time() + self._timeOffset) 792 798 793 799 def _getFuelLevel(self, index): 794 800 """Get the fuel level for the fuel tank with the given … … 796 802 return 0 if self.fuelCapacities[index]==0.0 else \ 797 803 int(self.fuelWeights[index] * 65536.0 * 128.0 / self.fuelCapacities[index]) 798 804 799 805 def _getFuelCapacity(self, index): 800 806 """Get the capacity of the fuel tank with the given index.""" … … 811 817 tm1 = tm[:index] + (value,) + tm[(index+1):] 812 818 self._timeOffset += calendar.timegm(tm1) - calendar.timegm(tm) 813 819 814 820 def _setThrottle(self, index, value): 815 821 """Set the throttle value for the given index.""" … … 820 826 self.fuelWeights[index] = self.fuelCapacities[index] * float(value) / \ 821 827 65536.0 / 128.0 822 828 823 829 def _setFuelCapacity(self, index, value): 824 830 """Set the capacity of the fuel tank with the given index.""" … … 830 836 const.FEETTOMETRES, 831 837 5.25588) 832 temperature = 15 - self.altitude * 6.5 * const.FEETTOMETRES / 1000.0 838 temperature = 15 - self.altitude * 6.5 * const.FEETTOMETRES / 1000.0 833 839 temperature += 273.15 # Celsius -> Kelvin 834 840 airDensity = pressure / (temperature * 287.05) … … 868 874 else: 869 875 raise FSUIPCException(ERR_NOTOPEN) 870 876 871 877 #------------------------------------------------------------------------------ 872 878 … … 877 883 else: 878 884 raise FSUIPCException(ERR_NOTOPEN) 879 885 880 886 #------------------------------------------------------------------------------ 881 887 … … 887 893 else: 888 894 raise FSUIPCException(ERR_NOTOPEN) 889 895 890 896 #------------------------------------------------------------------------------ 891 897 … … 924 930 serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 925 931 serverSocket.bind(("", PORT)) 926 932 927 933 serverSocket.listen(5) 928 934 929 935 while True: 930 936 (clientSocket, clientAddress) = serverSocket.accept() … … 936 942 socketFile = clientSocket.makefile() 937 943 try: 938 while True: 944 while True: 939 945 (length,) = struct.unpack("I", clientSocket.recv(4)) 940 946 data = clientSocket.recv(length) 941 947 (call, args) = cPickle.loads(data) 942 948 exception = None 943 949 944 950 try: 945 951 if call==CALL_READ: … … 977 983 pass 978 984 clientSocket.close() 979 985 980 986 #------------------------------------------------------------------------------ 981 987 … … 1013 1019 """Quit from the simulator.""" 1014 1020 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) 1016 1022 1017 1023 def _call(self, command, data): … … 1037 1043 """Convert the given string to a PyUIPC boolean value (i.e. 0 or 1).""" 1038 1044 return 1 if s in ["yes", "true", "on"] else 0 1039 1045 1040 1046 @staticmethod 1041 1047 def bool2str(value): … … 1047 1053 """Convert the given degree (as a string) into a PyUIPC value.""" 1048 1054 return int(float(degree) * 65536.0 * 65536.0 / 360.0) 1049 1055 1050 1056 @staticmethod 1051 1057 def pyuipc2degree(value): … … 1057 1063 """Convert the given percentage value (as a string) into a PyUIPC value.""" 1058 1064 return int(float(level) * 128.0 * 65536.0 / 100.0) 1059 1065 1060 1066 @staticmethod 1061 1067 def pyuipc2fuelLevel(value): 1062 1068 """Convert the PyUIPC value into a percentage value.""" 1063 1069 return value * 100.0 / 128.0 / 65536.0 1064 1070 1065 1071 @staticmethod 1066 1072 def fuelCapacity2pyuipc(capacity): 1067 1073 """Convert the given capacity value (as a string) into a PyUIPC value.""" 1068 1074 return int(capacity) 1069 1075 1070 1076 @staticmethod 1071 1077 def pyuipc2fuelCapacity(value): 1072 1078 """Convert the given capacity value into a PyUIPC value.""" 1073 1079 return value 1074 1080 1075 1081 @staticmethod 1076 1082 def throttle2pyuipc(throttle): 1077 1083 """Convert the given throttle value (as a string) into a PyUIPC value.""" 1078 1084 return int(float(throttle) * 16384.0 / 100.0) 1079 1085 1080 1086 @staticmethod 1081 1087 def pyuipc2throttle(value): … … 1086 1092 """Construct the CLI.""" 1087 1093 cmd.Cmd.__init__(self) 1088 1094 1089 1095 self.use_rawinput = True 1090 1096 self.intro = "\nPyUIPC simulator command prompt\n" … … 1158 1164 lambda word: None) 1159 1165 self._valueHandlers["vs"] = ([(0x02c8, "d")], 1160 lambda value: value * 60 / 1166 lambda value: value * 60 / 1161 1167 const.FEETTOMETRES / 256.0, 1162 1168 lambda word: int(float(word) * … … 1164 1170 256.0 / 60.0)) 1165 1171 self._valueHandlers["tdRate"] = ([(0x030c, "d")], 1166 lambda value: value * 60 / 1172 lambda value: value * 60 / 1167 1173 const.FEETTOMETRES / 256.0, 1168 1174 lambda word: int(float(word) * … … 1331 1337 CLI.pyuipc2throttle, 1332 1338 CLI.throttle2pyuipc) 1333 1339 1334 1340 self._valueHandlers["visibility"] = ([(0x0e8a, "H")], 1335 1341 lambda value: value*1609.344/100.0, 1336 1342 lambda word: int(float(word)* 1337 1343 100.0/1609.344)) 1338 1344 1339 1345 self._valueHandlers["payloadCount"] = ([(0x13fc, "d")], 1340 1346 lambda value: value, … … 1348 1354 self._valueHandlers["textScrolling"] = ([(0x1274, "h")], 1349 1355 CLI.bool2str, CLI.str2bool) 1350 1356 1351 1357 self._valueHandlers["messageDuration"] = ([(0x32fa, "h")], 1352 1358 lambda value: value, … … 1371 1377 lambda value: value, 1372 1378 lambda word: int(word)) 1379 self._valueHandlers["xpdrC"] = ([(0x7b91, "b")], 1380 lambda value: value, 1381 lambda word: int(word)) 1373 1382 1374 1383 def default(self, line): … … 1383 1392 """Handle the get command.""" 1384 1393 names = args.split() 1385 data = [] 1394 data = [] 1386 1395 for name in names: 1387 1396 if name not in self._valueHandlers: … … 1408 1417 i+=1 1409 1418 except Exception, e: 1410 print >> sys.stderr, "Failed to read data: " + str(e) 1419 print >> sys.stderr, "Failed to read data: " + str(e) 1411 1420 1412 1421 return False … … 1440 1449 if inWord: 1441 1450 arguments.append(word) 1442 1451 1443 1452 names = [] 1444 1453 data = [] … … 1467 1476 (value, name, str(e)) 1468 1477 return False 1469 1478 1470 1479 try: 1471 1480 self._client.write(data) … … 1508 1517 except Exception, e: 1509 1518 print >> sys.stderr, "Failed to close the connection: " + str(e) 1510 1519 1511 1520 def do_failopen(self, args): 1512 1521 """Enable/disable the failing of opens.""" … … 1516 1525 print "Opening will%s fail" % ("" if value else " not",) 1517 1526 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) 1519 1528 1520 1529 def help_failopen(self, usage = False): … … 1530 1539 else: 1531 1540 return ["yes", "no"] 1532 1541 1533 1542 def do_quit(self, args): 1534 1543 """Handle the quit command.""" … … 1542 1551 else: 1543 1552 server = Server() 1544 server.start() 1545 1546 #------------------------------------------------------------------------------ 1553 server.start() 1554 1555 #------------------------------------------------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.