[401] | 1 |
|
---|
[919] | 2 | from .util import utf2unicode
|
---|
| 3 | from .flight import Flight
|
---|
[298] | 4 |
|
---|
[919] | 5 | from . import const
|
---|
| 6 | import pickle as pickle
|
---|
[829] | 7 | import calendar
|
---|
[743] | 8 | import datetime
|
---|
| 9 | import time
|
---|
[97] | 10 |
|
---|
| 11 | #------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
[298] | 13 | ## @package mlx.pirep
|
---|
| 14 | #
|
---|
| 15 | # The PIREP module.
|
---|
| 16 | #
|
---|
| 17 | # This module defines only one class, \ref PIREP. It is used to extract and
|
---|
| 18 | # store the information needed for a PIREP. The saved PIREPs are pickled
|
---|
| 19 | # instances of this class.
|
---|
[97] | 20 |
|
---|
| 21 | #------------------------------------------------------------------------------
|
---|
| 22 |
|
---|
| 23 | class PIREP(object):
|
---|
| 24 | """A pilot's report of a flight."""
|
---|
[855] | 25 | class Message(object):
|
---|
| 26 | """A message belonging to the PIREP."""
|
---|
| 27 | @staticmethod
|
---|
| 28 | def fromMessageData(messageData):
|
---|
| 29 | """Construct a message from a JSON message data."""
|
---|
| 30 | message = messageData["message"]
|
---|
| 31 | senderPID = messageData["senderPID"]
|
---|
| 32 | senderName = messageData["senderName"]
|
---|
| 33 |
|
---|
| 34 | return PIREP.Message(message, senderPID, senderName)
|
---|
| 35 |
|
---|
| 36 | def __init__(self, message, senderPID, senderName):
|
---|
| 37 | """Construct the message object."""
|
---|
| 38 | self.message = message
|
---|
| 39 | self.senderPID = senderPID
|
---|
| 40 | self.senderName = senderName
|
---|
| 41 |
|
---|
[743] | 42 | _flightTypes = { const.FLIGHTTYPE_SCHEDULED : "SCHEDULED",
|
---|
| 43 | const.FLIGHTTYPE_OLDTIMER : "OT",
|
---|
| 44 | const.FLIGHTTYPE_VIP : "VIP",
|
---|
| 45 | const.FLIGHTTYPE_CHARTER : "CHARTER" }
|
---|
| 46 |
|
---|
[97] | 47 | @staticmethod
|
---|
| 48 | def _formatLine(timeStr, line):
|
---|
| 49 | """Format the given time string and line as needed for the ACARS and
|
---|
| 50 | some other things."""
|
---|
| 51 | return "[" + timeStr + "]-[" + line + "]"
|
---|
[151] | 52 |
|
---|
| 53 | @staticmethod
|
---|
[743] | 54 | def formatTimestampForRPC(t):
|
---|
| 55 | """Format the given timestamp for RPC."""
|
---|
| 56 | return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(t))
|
---|
| 57 |
|
---|
| 58 | @staticmethod
|
---|
[829] | 59 | def parseTimestampFromRPC(s):
|
---|
| 60 | """Format the given timestamp for RPC."""
|
---|
| 61 | dt = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
|
---|
| 62 | return calendar.timegm(dt.utctimetuple())
|
---|
| 63 |
|
---|
| 64 | @staticmethod
|
---|
| 65 | def decodeFlightTypeText(s):
|
---|
| 66 | """Decode the given flight type text."""
|
---|
[919] | 67 | for (flighType, text) in PIREP._flightTypes.items():
|
---|
[829] | 68 | if s==text:
|
---|
| 69 | return flighType
|
---|
[856] | 70 | return const.FLIGHTTYPE_SCHEDULED
|
---|
[829] | 71 |
|
---|
| 72 | @staticmethod
|
---|
| 73 | def parseLogFromRPC(log):
|
---|
| 74 | """Parse the given log coming from the RPC."""
|
---|
| 75 | index = 0
|
---|
| 76 | entries = []
|
---|
| 77 |
|
---|
| 78 | inTimeStr = False
|
---|
| 79 | inEntry = False
|
---|
| 80 |
|
---|
| 81 | timestr = ""
|
---|
| 82 | entry = ""
|
---|
| 83 |
|
---|
| 84 | while index<len(log):
|
---|
| 85 | c = log[index]
|
---|
| 86 | index += 1
|
---|
| 87 |
|
---|
| 88 | if c==']':
|
---|
| 89 | if inEntry:
|
---|
| 90 | entries.append((timestr, entry))
|
---|
| 91 | timestr = ""
|
---|
| 92 | entry = ""
|
---|
| 93 |
|
---|
| 94 | inTimeStr = False
|
---|
| 95 | inEntry = False
|
---|
| 96 | elif not inTimeStr and not inEntry:
|
---|
| 97 | if c=='[':
|
---|
| 98 | if timestr:
|
---|
| 99 | inEntry = True
|
---|
| 100 | else:
|
---|
| 101 | inTimeStr = True
|
---|
| 102 | elif inTimeStr:
|
---|
| 103 | timestr += c
|
---|
| 104 | elif inEntry:
|
---|
| 105 | entry += c
|
---|
| 106 |
|
---|
| 107 | return entries
|
---|
| 108 |
|
---|
| 109 | @staticmethod
|
---|
[151] | 110 | def load(path):
|
---|
| 111 | """Load a PIREP from the given path.
|
---|
| 112 |
|
---|
| 113 | Returns the PIREP object, or None on error."""
|
---|
| 114 | try:
|
---|
| 115 | with open(path, "rb") as f:
|
---|
[303] | 116 | pirep = pickle.load(f)
|
---|
| 117 | if "numCrew" not in dir(pirep):
|
---|
| 118 | pirep.numCrew = pirep.bookedFlight.numCrew
|
---|
| 119 | if "numPassengers" not in dir(pirep):
|
---|
| 120 | pirep.numPassengers = pirep.bookedFlight.numPassengers
|
---|
| 121 | if "bagWeight" not in dir(pirep):
|
---|
| 122 | pirep.bagWeight = pirep.bookedFlight.bagWeight
|
---|
| 123 | if "mailWeight" not in dir(pirep):
|
---|
| 124 | pirep.mailWeight = pirep.bookedFlight.mailWeight
|
---|
| 125 | return pirep
|
---|
[919] | 126 | except Exception as e:
|
---|
| 127 | print("Failed loading PIREP from %s: %s" % (path,
|
---|
| 128 | utf2unicode(str(e))))
|
---|
[151] | 129 | return None
|
---|
[345] | 130 |
|
---|
[262] | 131 | def __init__(self, flight):
|
---|
| 132 | """Initialize the PIREP from the given flight."""
|
---|
[829] | 133 | if flight is None:
|
---|
| 134 | return
|
---|
| 135 |
|
---|
[262] | 136 | self.bookedFlight = flight.bookedFlight
|
---|
[303] | 137 |
|
---|
| 138 | self.numCrew = flight.numCrew
|
---|
| 139 | self.numPassengers = flight.numPassengers
|
---|
| 140 | self.bagWeight = flight.bagWeight
|
---|
[262] | 141 | self.cargoWeight = flight.cargoWeight
|
---|
[303] | 142 | self.mailWeight = flight.mailWeight
|
---|
[345] | 143 |
|
---|
[262] | 144 | self.filedCruiseAltitude = flight.filedCruiseAltitude
|
---|
| 145 | self.cruiseAltitude = flight.cruiseAltitude
|
---|
| 146 | self.route = flight.route
|
---|
[97] | 147 |
|
---|
[262] | 148 | self.departureMETAR = flight.departureMETAR.upper()
|
---|
| 149 | self.arrivalMETAR = flight.arrivalMETAR.upper()
|
---|
[97] | 150 |
|
---|
[262] | 151 | self.departureRunway = flight.departureRunway.upper()
|
---|
| 152 | self.sid = flight.sid.upper()
|
---|
[97] | 153 |
|
---|
[262] | 154 | self.star = flight.star
|
---|
| 155 | self.transition = flight.transition
|
---|
| 156 | self.approachType = flight.approachType.upper()
|
---|
| 157 | self.arrivalRunway = flight.arrivalRunway.upper()
|
---|
[97] | 158 |
|
---|
[262] | 159 | self.flightType = flight.flightType
|
---|
| 160 | self.online = flight.online
|
---|
[97] | 161 |
|
---|
[262] | 162 | self.comments = flight.comments
|
---|
| 163 | self.flightDefects = flight.flightDefects
|
---|
| 164 | self.delayCodes = flight.delayCodes
|
---|
[345] | 165 |
|
---|
[97] | 166 | self.blockTimeStart = flight.blockTimeStart
|
---|
| 167 | self.flightTimeStart = flight.flightTimeStart
|
---|
| 168 | self.flightTimeEnd = flight.flightTimeEnd
|
---|
| 169 | self.blockTimeEnd = flight.blockTimeEnd
|
---|
| 170 | self.flownDistance = flight.flownDistance
|
---|
[102] | 171 | self.fuelUsed = flight.startFuel - flight.endFuel
|
---|
[97] | 172 |
|
---|
[262] | 173 | logger = flight.logger
|
---|
[97] | 174 | self.rating = logger.getRating()
|
---|
| 175 | self.logLines = logger.lines
|
---|
| 176 | self.faultLineIndexes = logger.faultLineIndexes
|
---|
[345] | 177 |
|
---|
[855] | 178 | self.messages = []
|
---|
| 179 |
|
---|
[829] | 180 | def setupFromPIREPData(self, pirepData, bookedFlight):
|
---|
| 181 |
|
---|
| 182 | self.bookedFlight = bookedFlight
|
---|
| 183 |
|
---|
| 184 | self.numCrew = int(pirepData["numCrew"])
|
---|
| 185 | self.numPassengers = int(pirepData["numPassengers"])
|
---|
| 186 | self.bagWeight = int(pirepData["bagWeight"])
|
---|
| 187 | self.cargoWeight = int(pirepData["cargoWeight"])
|
---|
| 188 | self.mailWeight = int(pirepData["mailWeight"])
|
---|
| 189 |
|
---|
[856] | 190 | filedCruiseLevel = pirepData["filedCruiseLevel"].strip()
|
---|
| 191 | if filedCruiseLevel:
|
---|
| 192 | if filedCruiseLevel.startswith("FL"):
|
---|
| 193 | filedCruiseLevel = filedCruiseLevel[2:]
|
---|
| 194 | if filedCruiseLevel:
|
---|
| 195 | self.filedCruiseAltitude = int(filedCruiseLevel)*100
|
---|
| 196 | else:
|
---|
| 197 | self.filedCruiseAltitude = 10000;
|
---|
| 198 |
|
---|
[854] | 199 | cruiseLevel = pirepData["cruiseLevel"].strip()
|
---|
| 200 | if cruiseLevel:
|
---|
| 201 | if cruiseLevel.startswith("FL"):
|
---|
| 202 | cruiseLevel = cruiseLevel[2:]
|
---|
[829] | 203 | if cruiseLevel:
|
---|
| 204 | self.cruiseAltitude = int(cruiseLevel[2:])*100
|
---|
| 205 | else:
|
---|
| 206 | self.cruiseAltitude = self.filedCruiseAltitude
|
---|
| 207 | self.route = pirepData["route"]
|
---|
| 208 |
|
---|
| 209 | self.departureMETAR = pirepData["departureMETAR"]
|
---|
| 210 | self.arrivalMETAR = pirepData["arrivalMETAR"]
|
---|
| 211 |
|
---|
| 212 | self.departureRunway = pirepData["departureRunway"]
|
---|
| 213 | self.sid = pirepData["sid"]
|
---|
| 214 |
|
---|
| 215 | star = pirepData["star"].split(",")
|
---|
| 216 | self.star = star[0]
|
---|
| 217 | self.star.strip()
|
---|
| 218 |
|
---|
| 219 | if len(star)>1:
|
---|
| 220 | self.transition = star[1]
|
---|
| 221 | self.transition.strip()
|
---|
| 222 | else:
|
---|
| 223 | self.transition = ""
|
---|
| 224 | self.approachType = pirepData["approachType"]
|
---|
| 225 | self.arrivalRunway = pirepData["arrivalRunway"]
|
---|
| 226 |
|
---|
| 227 | self.flightType = PIREP.decodeFlightTypeText(pirepData["flightType"])
|
---|
| 228 | self.online = int(pirepData["online"])!=0
|
---|
| 229 |
|
---|
| 230 | self.comments = pirepData["comments"]
|
---|
| 231 | self.flightDefects = pirepData["flightDefects"]
|
---|
| 232 | self.delayCodes = pirepData["timeComment"]
|
---|
| 233 | if self.delayCodes=="UTC":
|
---|
[838] | 234 | self.delayCodes = []
|
---|
| 235 | else:
|
---|
| 236 | self.delayCodes = self.delayCodes.split(", ")
|
---|
[829] | 237 |
|
---|
| 238 | flightDate = pirepData["flightDate"] + " "
|
---|
| 239 |
|
---|
| 240 | self.blockTimeStart = \
|
---|
| 241 | PIREP.parseTimestampFromRPC(flightDate + pirepData["blockTimeStart"])
|
---|
| 242 | self.flightTimeStart = \
|
---|
| 243 | PIREP.parseTimestampFromRPC(flightDate + pirepData["flightTimeStart"])
|
---|
| 244 | self.flightTimeEnd = \
|
---|
| 245 | PIREP.parseTimestampFromRPC(flightDate + pirepData["flightTimeEnd"])
|
---|
| 246 | self.blockTimeEnd = \
|
---|
| 247 | PIREP.parseTimestampFromRPC(flightDate + pirepData["blockTimeEnd"])
|
---|
| 248 | self.flownDistance = float(pirepData["flownDistance"])
|
---|
| 249 | self.fuelUsed = float(pirepData["fuelUsed"])
|
---|
| 250 |
|
---|
| 251 | # logger = flight.logger
|
---|
| 252 | self.rating = float(pirepData["rating"])
|
---|
| 253 |
|
---|
| 254 | log = pirepData["log"]
|
---|
| 255 |
|
---|
| 256 | self.logLines = PIREP.parseLogFromRPC(log)[1:]
|
---|
| 257 | if self.logLines and \
|
---|
| 258 | (self.logLines[0][0]=="LOGGER NG LOG" or
|
---|
| 259 | self.logLines[0][0]=="MAVA LOGGER X"):
|
---|
| 260 | self.logLines = self.logLines[1:]
|
---|
| 261 | numLogLines = len(self.logLines)
|
---|
| 262 |
|
---|
| 263 | lastFaultLineIndex = 0
|
---|
| 264 | self.faultLineIndexes = []
|
---|
| 265 | for ratingText in pirepData["ratingText"].splitlines()[:-1]:
|
---|
| 266 | faultLines = PIREP.parseLogFromRPC(ratingText)
|
---|
| 267 | for (timeStr, entry) in faultLines:
|
---|
| 268 | for i in range(lastFaultLineIndex, numLogLines-1):
|
---|
| 269 | if timeStr>=self.logLines[i][0] and \
|
---|
| 270 | timeStr<self.logLines[i+1][0]:
|
---|
| 271 | self.logLines = self.logLines[:i+1] + \
|
---|
| 272 | [(timeStr, entry)] + self.logLines[i+1:]
|
---|
| 273 | self.faultLineIndexes.append(i+1)
|
---|
| 274 | lastFaultLineIndex = i+1
|
---|
| 275 | numLogLines += 1
|
---|
| 276 | break
|
---|
| 277 |
|
---|
[855] | 278 | self.messages = []
|
---|
| 279 | for messageData in pirepData["messages"]:
|
---|
| 280 | self.messages.append(PIREP.Message.fromMessageData(messageData))
|
---|
| 281 |
|
---|
[743] | 282 | @property
|
---|
[778] | 283 | def flightDateText(self):
|
---|
| 284 | """Get the text version of the booked flight's departure time."""
|
---|
| 285 | return self.bookedFlight.departureTime.strftime("%Y-%m-%d")
|
---|
| 286 |
|
---|
| 287 | @property
|
---|
[743] | 288 | def flightTypeText(self):
|
---|
| 289 | """Get the text representation of the flight type."""
|
---|
| 290 | return PIREP._flightTypes[self.flightType]
|
---|
| 291 |
|
---|
| 292 | @property
|
---|
| 293 | def blockTimeStartText(self):
|
---|
| 294 | """Get the beginning of the block time in string format."""
|
---|
| 295 | return PIREP.formatTimestampForRPC(self.blockTimeStart)
|
---|
| 296 |
|
---|
| 297 | @property
|
---|
| 298 | def flightTimeStartText(self):
|
---|
| 299 | """Get the beginning of the flight time in string format."""
|
---|
| 300 | return PIREP.formatTimestampForRPC(self.flightTimeStart)
|
---|
| 301 |
|
---|
| 302 | @property
|
---|
| 303 | def flightTimeEndText(self):
|
---|
| 304 | """Get the end of the flight time in string format."""
|
---|
| 305 | return PIREP.formatTimestampForRPC(self.flightTimeEnd)
|
---|
| 306 |
|
---|
| 307 | @property
|
---|
| 308 | def blockTimeEndText(self):
|
---|
| 309 | """Get the end of the block time in string format."""
|
---|
| 310 | return PIREP.formatTimestampForRPC(self.blockTimeEnd)
|
---|
| 311 |
|
---|
[97] | 312 | def getACARSText(self):
|
---|
| 313 | """Get the ACARS text.
|
---|
| 314 |
|
---|
| 315 | This is a specially formatted version of the log without the faults."""
|
---|
| 316 | text = "[MAVA LOGGER X LOG]-[%s]" % (const.VERSION,)
|
---|
| 317 | for index in range(0, len(self.logLines)):
|
---|
| 318 | if index not in self.faultLineIndexes:
|
---|
| 319 | (timeStr, line) = self.logLines[index]
|
---|
| 320 | if timeStr is not None:
|
---|
| 321 | text += PIREP._formatLine(timeStr, line)
|
---|
| 322 | return text
|
---|
| 323 |
|
---|
| 324 | def getRatingText(self):
|
---|
| 325 | """Get the rating text.
|
---|
| 326 |
|
---|
| 327 | This is a specially formatted version of the lines containing the
|
---|
| 328 | faults."""
|
---|
| 329 | text = ""
|
---|
| 330 | for index in self.faultLineIndexes:
|
---|
| 331 | (timeStr, line) = self.logLines[index]
|
---|
| 332 | if timeStr is not None:
|
---|
| 333 | text += PIREP._formatLine(timeStr, line)
|
---|
[345] | 334 | text += "\n"
|
---|
[97] | 335 |
|
---|
| 336 | text += "\n[Flight Rating: %.1f]" % (max(0.0, self.rating),)
|
---|
| 337 |
|
---|
| 338 | return text
|
---|
[345] | 339 |
|
---|
[97] | 340 | def getTimeComment(self):
|
---|
| 341 | """Get the time comment.
|
---|
| 342 |
|
---|
| 343 | This is basically a collection of the delay codes, if any."""
|
---|
[99] | 344 | if not self.delayCodes:
|
---|
| 345 | return "UTC"
|
---|
| 346 | else:
|
---|
| 347 | s = ""
|
---|
| 348 | for code in self.delayCodes:
|
---|
| 349 | if s: s += ", "
|
---|
[437] | 350 | s += code
|
---|
[99] | 351 | return s
|
---|
[97] | 352 |
|
---|
| 353 | def getSTAR(self):
|
---|
| 354 | """Get the STAR and/or the transition."""
|
---|
| 355 | star = self.star if self.star is not None else ""
|
---|
| 356 | if self.transition is not None:
|
---|
| 357 | if star: star += ", "
|
---|
| 358 | star += self.transition
|
---|
| 359 | return star.upper()
|
---|
[151] | 360 |
|
---|
| 361 | def save(self, path):
|
---|
| 362 | """Save the PIREP to the given file.
|
---|
| 363 |
|
---|
| 364 | Returns whether the saving has succeeded."""
|
---|
| 365 | try:
|
---|
| 366 | with open(path, "wb") as f:
|
---|
| 367 | pickle.dump(self, f)
|
---|
[393] | 368 | return None
|
---|
[919] | 369 | except Exception as e:
|
---|
[401] | 370 | error = utf2unicode(str(e))
|
---|
[919] | 371 | print("Failed saving PIREP to %s: %s" % (path, error))
|
---|
[393] | 372 | return error
|
---|
[743] | 373 |
|
---|
| 374 | def _serialize(self):
|
---|
| 375 | """Serialize the PIREP for JSON-RPC."""
|
---|
| 376 | attrs = {}
|
---|
| 377 | attrs["log"] = self.getACARSText()
|
---|
[778] | 378 | attrs["flightDate"] = self.flightDateText
|
---|
| 379 | attrs["callsign"] = self.bookedFlight.callsign
|
---|
| 380 | attrs["departureICAO"] = self.bookedFlight.departureICAO
|
---|
| 381 | attrs["arrivalICAO"] = self.bookedFlight.arrivalICAO
|
---|
[743] | 382 | attrs["numPassengers"] = self.numPassengers
|
---|
| 383 | attrs["numCrew"] = self.numCrew
|
---|
| 384 | attrs["cargoWeight"] = self.cargoWeight
|
---|
| 385 | attrs["bagWeight"] = self.bagWeight
|
---|
| 386 | attrs["mailWeight"] = self.mailWeight
|
---|
| 387 | attrs["flightType"] = self.flightTypeText
|
---|
| 388 | attrs["online"] = 1 if self.online else 0
|
---|
| 389 | attrs["blockTimeStart"] = self.blockTimeStartText
|
---|
| 390 | attrs["blockTimeEnd"] = self.blockTimeEndText
|
---|
| 391 | attrs["flightTimeStart"] = self.flightTimeStartText
|
---|
| 392 | attrs["flightTimeEnd"] = self.flightTimeEndText
|
---|
| 393 | attrs["timeComment"] = self.getTimeComment()
|
---|
| 394 | attrs["fuelUsed"] = self.fuelUsed
|
---|
| 395 | attrs["departureRunway"] = self.departureRunway
|
---|
| 396 | attrs["arrivalRunway"] = self.arrivalRunway
|
---|
| 397 | attrs["departureMETAR"] = self.departureMETAR
|
---|
| 398 | attrs["arrivalMETAR"] = self.arrivalMETAR
|
---|
| 399 | attrs["filedCruiseLevel"] = self.filedCruiseAltitude / 100.0
|
---|
| 400 | attrs["cruiseLevel"] = self.cruiseAltitude / 100.0
|
---|
| 401 | attrs["sid"] = self.sid
|
---|
| 402 | attrs["route"] = self.route
|
---|
| 403 | attrs["star"] = self.star
|
---|
| 404 | attrs["approachType"] = self.approachType
|
---|
| 405 | attrs["comments"] = self.comments
|
---|
| 406 | attrs["flightDefects"] = self.flightDefects
|
---|
| 407 | attrs["ratingText"] = self.getRatingText()
|
---|
| 408 | attrs["rating"] = max(0.0, self.rating)
|
---|
[839] | 409 | attrs["flownDistance"] = "%.2f" % (self.flownDistance,)
|
---|
[743] | 410 | # FIXME: it should be stored in the PIREP when it is sent later
|
---|
[778] | 411 | attrs["performDate"] = datetime.date.today().strftime("%Y-%m-%d")
|
---|
[743] | 412 |
|
---|
| 413 | return ([], attrs)
|
---|