[8] | 1 | # Module related to the high-level tracking of the flight
|
---|
| 2 |
|
---|
| 3 | #---------------------------------------------------------------------------------------
|
---|
| 4 |
|
---|
[176] | 5 | from soundsched import SoundScheduler, ChecklistScheduler
|
---|
[170] | 6 |
|
---|
[8] | 7 | import const
|
---|
[89] | 8 | import util
|
---|
[8] | 9 |
|
---|
| 10 | import threading
|
---|
| 11 |
|
---|
| 12 | #---------------------------------------------------------------------------------------
|
---|
| 13 |
|
---|
| 14 | class Flight(object):
|
---|
| 15 | """The object with the global flight state.
|
---|
| 16 |
|
---|
| 17 | It is also the hub for the other main objects participating in the handling of
|
---|
| 18 | the flight."""
|
---|
[29] | 19 | def __init__(self, logger, gui):
|
---|
[8] | 20 | """Construct the flight."""
|
---|
| 21 | self._stage = None
|
---|
| 22 | self.logger = logger
|
---|
[29] | 23 | self._gui = gui
|
---|
[11] | 24 |
|
---|
[31] | 25 | gui.resetFlightStatus()
|
---|
| 26 |
|
---|
[170] | 27 | self._pilotHotkeyPressed = False
|
---|
| 28 | self._checklistHotkeyPressed = False
|
---|
| 29 |
|
---|
[9] | 30 | self.flareTimeFromFS = False
|
---|
[11] | 31 |
|
---|
[8] | 32 | self.aircraftType = None
|
---|
| 33 | self.aircraft = None
|
---|
| 34 | self.simulator = None
|
---|
| 35 |
|
---|
[89] | 36 | self.blockTimeStart = None
|
---|
| 37 | self.flightTimeStart = None
|
---|
| 38 | self.flightTimeEnd = None
|
---|
| 39 | self.blockTimeEnd = None
|
---|
| 40 |
|
---|
| 41 | self._lastDistanceTime = None
|
---|
| 42 | self._previousLatitude = None
|
---|
| 43 | self._previousLongitude = None
|
---|
| 44 | self.flownDistance = 0.0
|
---|
| 45 |
|
---|
| 46 | self.startFuel = None
|
---|
| 47 | self.endFuel = None
|
---|
| 48 |
|
---|
[8] | 49 | self._endCondition = threading.Condition()
|
---|
| 50 |
|
---|
[9] | 51 | self._flareStart = None
|
---|
| 52 | self._flareStartFS = None
|
---|
| 53 |
|
---|
[170] | 54 | self._tdRate = None
|
---|
| 55 |
|
---|
[176] | 56 | self._soundScheduler = SoundScheduler(self)
|
---|
| 57 | self._checklistScheduler = ChecklistScheduler(self)
|
---|
| 58 |
|
---|
[8] | 59 | @property
|
---|
[134] | 60 | def config(self):
|
---|
| 61 | """Get the configuration."""
|
---|
| 62 | return self._gui.config
|
---|
| 63 |
|
---|
| 64 | @property
|
---|
[8] | 65 | def stage(self):
|
---|
| 66 | """Get the flight stage."""
|
---|
| 67 | return self._stage
|
---|
| 68 |
|
---|
[84] | 69 | @property
|
---|
[215] | 70 | def loggedIn(self):
|
---|
| 71 | """Indicate if the user has logged in properly."""
|
---|
| 72 | return self._gui.loggedIn
|
---|
| 73 |
|
---|
| 74 | @property
|
---|
[184] | 75 | def entranceExam(self):
|
---|
| 76 | """Get whether an entrance exam is being performed."""
|
---|
| 77 | return self._gui.entranceExam
|
---|
| 78 |
|
---|
| 79 | @property
|
---|
[134] | 80 | def bookedFlight(self):
|
---|
| 81 | """Get the booked flight."""
|
---|
| 82 | return self._gui.bookedFlight
|
---|
| 83 |
|
---|
| 84 | @property
|
---|
[84] | 85 | def zfw(self):
|
---|
| 86 | """Get the Zero-Fuel Weight of the flight."""
|
---|
| 87 | return self._gui.zfw
|
---|
| 88 |
|
---|
| 89 | @property
|
---|
| 90 | def cruiseAltitude(self):
|
---|
| 91 | """Get the cruise altitude of the flight."""
|
---|
| 92 | return self._gui.cruiseAltitude
|
---|
| 93 |
|
---|
| 94 | @property
|
---|
| 95 | def v1(self):
|
---|
| 96 | """Get the V1 speed of the flight."""
|
---|
| 97 | return self._gui.v1
|
---|
| 98 |
|
---|
| 99 | @property
|
---|
| 100 | def vr(self):
|
---|
| 101 | """Get the Vr speed of the flight."""
|
---|
| 102 | return self._gui.vr
|
---|
| 103 |
|
---|
| 104 | @property
|
---|
| 105 | def v2(self):
|
---|
| 106 | """Get the V2 speed of the flight."""
|
---|
| 107 | return self._gui.v2
|
---|
| 108 |
|
---|
[86] | 109 | @property
|
---|
| 110 | def vref(self):
|
---|
| 111 | """Get the VRef speed of the flight."""
|
---|
| 112 | return self._gui.vref
|
---|
| 113 |
|
---|
[170] | 114 | @property
|
---|
| 115 | def tdRate(self):
|
---|
| 116 | """Get the touchdown rate if known, None otherwise."""
|
---|
| 117 | return self._tdRate
|
---|
| 118 |
|
---|
[241] | 119 | @property
|
---|
| 120 | def speedInKnots(self):
|
---|
| 121 | """Determine if the speeds for the flight are to be expressed in
|
---|
| 122 | knots."""
|
---|
| 123 | return self.aircraft.speedInKnots if self.aircraft is not None \
|
---|
| 124 | else True
|
---|
| 125 |
|
---|
[89] | 126 | def handleState(self, oldState, currentState):
|
---|
| 127 | """Handle a new state information."""
|
---|
| 128 | self._updateFlownDistance(currentState)
|
---|
| 129 |
|
---|
| 130 | self.endFuel = sum(currentState.fuel)
|
---|
| 131 | if self.startFuel is None:
|
---|
| 132 | self.startFuel = self.endFuel
|
---|
[170] | 133 |
|
---|
| 134 | self._soundScheduler.schedule(currentState,
|
---|
| 135 | self._pilotHotkeyPressed)
|
---|
| 136 | self._pilotHotkeyPressed = False
|
---|
[89] | 137 |
|
---|
[176] | 138 | if self._checklistHotkeyPressed:
|
---|
| 139 | self._checklistScheduler.hotkeyPressed()
|
---|
| 140 | self._checklistHotkeyPressed = False
|
---|
| 141 |
|
---|
[8] | 142 | def setStage(self, timestamp, stage):
|
---|
[9] | 143 | """Set the flight stage.
|
---|
| 144 |
|
---|
| 145 | Returns if the stage has really changed."""
|
---|
[8] | 146 | if stage!=self._stage:
|
---|
| 147 | self._stage = stage
|
---|
[84] | 148 | self._gui.setStage(stage)
|
---|
[8] | 149 | self.logger.stage(timestamp, stage)
|
---|
[89] | 150 | if stage==const.STAGE_PUSHANDTAXI:
|
---|
| 151 | self.blockTimeStart = timestamp
|
---|
| 152 | elif stage==const.STAGE_TAKEOFF:
|
---|
| 153 | self.flightTimeStart = timestamp
|
---|
| 154 | elif stage==const.STAGE_TAXIAFTERLAND:
|
---|
| 155 | self.flightTimeEnd = timestamp
|
---|
| 156 | elif stage==const.STAGE_PARKING:
|
---|
| 157 | self.blockTimeEnd = timestamp
|
---|
| 158 | elif stage==const.STAGE_END:
|
---|
[8] | 159 | with self._endCondition:
|
---|
| 160 | self._endCondition.notify()
|
---|
[9] | 161 | return True
|
---|
| 162 | else:
|
---|
| 163 | return False
|
---|
| 164 |
|
---|
[30] | 165 | def handleFault(self, faultID, timestamp, what, score):
|
---|
| 166 | """Handle the given fault.
|
---|
| 167 |
|
---|
| 168 | faultID as a unique ID for the given kind of fault. If another fault of
|
---|
| 169 | this ID has been reported earlier, it will be reported again only if
|
---|
| 170 | the score is greater than last time. This ID can be, e.g. the checker
|
---|
| 171 | the report comes from."""
|
---|
| 172 | self.logger.fault(faultID, timestamp, what, score)
|
---|
[31] | 173 | self._gui.setRating(self.logger.getRating())
|
---|
[30] | 174 |
|
---|
| 175 | def handleNoGo(self, faultID, timestamp, what, shortReason):
|
---|
| 176 | """Handle a No-Go fault."""
|
---|
| 177 | self.logger.noGo(faultID, timestamp, what)
|
---|
[31] | 178 | self._gui.setNoGo(shortReason)
|
---|
[30] | 179 |
|
---|
[9] | 180 | def flareStarted(self, flareStart, flareStartFS):
|
---|
| 181 | """Called when the flare time has started."""
|
---|
| 182 | self._flareStart = flareStart
|
---|
| 183 | self._flareStartFS = flareStartFS
|
---|
| 184 |
|
---|
[170] | 185 | def flareFinished(self, flareEnd, flareEndFS, tdRate):
|
---|
[9] | 186 | """Called when the flare time has ended.
|
---|
| 187 |
|
---|
| 188 | Return a tuple of the following items:
|
---|
| 189 | - a boolean indicating if FS time is used
|
---|
| 190 | - the flare time
|
---|
| 191 | """
|
---|
[170] | 192 | self._tdRate = tdRate
|
---|
[9] | 193 | if self.flareTimeFromFS:
|
---|
| 194 | return (True, flareEndFS - self._flareStartFS)
|
---|
| 195 | else:
|
---|
| 196 | return (False, flareEnd - self._flareStart)
|
---|
[8] | 197 |
|
---|
| 198 | def wait(self):
|
---|
| 199 | """Wait for the flight to end."""
|
---|
| 200 | with self._endCondition:
|
---|
| 201 | while self._stage!=const.STAGE_END:
|
---|
[14] | 202 | self._endCondition.wait(1)
|
---|
[8] | 203 |
|
---|
[134] | 204 | def getFleet(self, callback, force = False):
|
---|
| 205 | """Get the fleet and call the given callback."""
|
---|
| 206 | self._gui.getFleetAsync(callback = callback, force = force)
|
---|
| 207 |
|
---|
[170] | 208 | def pilotHotkeyPressed(self):
|
---|
| 209 | """Called when the pilot hotkey is pressed."""
|
---|
| 210 | self._pilotHotkeyPressed = True
|
---|
| 211 |
|
---|
| 212 | def checklistHotkeyPressed(self):
|
---|
| 213 | """Called when the checklist hotkey is pressed."""
|
---|
| 214 | self._checklistHotkeyPressed = True
|
---|
| 215 |
|
---|
[241] | 216 | def speedFromKnots(self, knots):
|
---|
| 217 | """Convert the given speed value expressed in knots into the flight's
|
---|
| 218 | speed unit."""
|
---|
| 219 | return knots if self.speedInKnots else knots * const.KNOTSTOKMPH
|
---|
| 220 |
|
---|
| 221 | def speedToKnots(self, speed):
|
---|
| 222 | """Convert the given speed expressed in the flight's speed unit into
|
---|
| 223 | knots."""
|
---|
| 224 | return speed if self.speedInKnots else speed * const.KMPHTOKNOTS
|
---|
| 225 |
|
---|
| 226 | def getEnglishSpeedUnit(self):
|
---|
| 227 | """Get the English name of the speed unit used by the flight."""
|
---|
| 228 | return "knots" if self.speedInKnots else "km/h"
|
---|
| 229 |
|
---|
| 230 | def getI18NSpeedUnit(self):
|
---|
| 231 | """Get the speed unit suffix for i18n message identifiers."""
|
---|
| 232 | return "_knots" if self.speedInKnots else "_kmph"
|
---|
| 233 |
|
---|
[89] | 234 | def _updateFlownDistance(self, currentState):
|
---|
| 235 | """Update the flown distance."""
|
---|
| 236 | if not currentState.onTheGround:
|
---|
| 237 | updateData = False
|
---|
| 238 | if self._lastDistanceTime is None or \
|
---|
| 239 | self._previousLatitude is None or \
|
---|
| 240 | self._previousLongitude is None:
|
---|
| 241 | updateData = True
|
---|
| 242 | elif currentState.timestamp >= (self._lastDistanceTime + 30.0):
|
---|
| 243 | updateData = True
|
---|
| 244 | self.flownDistance += self._getDistance(currentState)
|
---|
| 245 |
|
---|
| 246 | if updateData:
|
---|
| 247 | self._previousLatitude = currentState.latitude
|
---|
| 248 | self._previousLongitude = currentState.longitude
|
---|
| 249 | self._lastDistanceTime = currentState.timestamp
|
---|
| 250 | else:
|
---|
| 251 | if self._lastDistanceTime is not None and \
|
---|
| 252 | self._previousLatitude is not None and \
|
---|
| 253 | self._previousLongitude is not None:
|
---|
| 254 | self.flownDistance += self._getDistance(currentState)
|
---|
| 255 |
|
---|
| 256 | self._lastDistanceTime = None
|
---|
| 257 |
|
---|
| 258 | def _getDistance(self, currentState):
|
---|
| 259 | """Get the distance between the previous and the current state."""
|
---|
| 260 | return util.getDistCourse(self._previousLatitude, self._previousLongitude,
|
---|
| 261 | currentState.latitude, currentState.longitude)[0]
|
---|
| 262 |
|
---|
[8] | 263 | #---------------------------------------------------------------------------------------
|
---|