source: src/mlx/pirep.py@ 743:6bc880ac41eb

Last change on this file since 743:6bc880ac41eb was 743:6bc880ac41eb, checked in by István Váradi <ivaradi@…>, 8 years ago

Prepared the ACARS and PIREP objects for serialization (re #283).

File size: 8.0 KB
RevLine 
[401]1
2from util import utf2unicode
[298]3
4import const
5import cPickle as pickle
[743]6import datetime
7import time
[97]8
9#------------------------------------------------------------------------------
10
[298]11## @package mlx.pirep
12#
13# The PIREP module.
14#
15# This module defines only one class, \ref PIREP. It is used to extract and
16# store the information needed for a PIREP. The saved PIREPs are pickled
17# instances of this class.
[97]18
19#------------------------------------------------------------------------------
20
21class PIREP(object):
22 """A pilot's report of a flight."""
[743]23 _flightTypes = { const.FLIGHTTYPE_SCHEDULED : "SCHEDULED",
24 const.FLIGHTTYPE_OLDTIMER : "OT",
25 const.FLIGHTTYPE_VIP : "VIP",
26 const.FLIGHTTYPE_CHARTER : "CHARTER" }
27
[97]28 @staticmethod
29 def _formatLine(timeStr, line):
30 """Format the given time string and line as needed for the ACARS and
31 some other things."""
32 return "[" + timeStr + "]-[" + line + "]"
[151]33
34 @staticmethod
[743]35 def formatTimestampForRPC(t):
36 """Format the given timestamp for RPC."""
37 return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(t))
38
39 @staticmethod
[151]40 def load(path):
41 """Load a PIREP from the given path.
42
43 Returns the PIREP object, or None on error."""
44 try:
45 with open(path, "rb") as f:
[303]46 pirep = pickle.load(f)
47 if "numCrew" not in dir(pirep):
48 pirep.numCrew = pirep.bookedFlight.numCrew
49 if "numPassengers" not in dir(pirep):
50 pirep.numPassengers = pirep.bookedFlight.numPassengers
51 if "bagWeight" not in dir(pirep):
52 pirep.bagWeight = pirep.bookedFlight.bagWeight
53 if "mailWeight" not in dir(pirep):
54 pirep.mailWeight = pirep.bookedFlight.mailWeight
55 return pirep
[151]56 except Exception, e:
[401]57 print "Failed loading PIREP from %s: %s" % (path,
58 utf2unicode(str(e)))
[151]59 return None
[345]60
[262]61 def __init__(self, flight):
62 """Initialize the PIREP from the given flight."""
63 self.bookedFlight = flight.bookedFlight
[303]64
65 self.numCrew = flight.numCrew
66 self.numPassengers = flight.numPassengers
67 self.bagWeight = flight.bagWeight
[262]68 self.cargoWeight = flight.cargoWeight
[303]69 self.mailWeight = flight.mailWeight
[345]70
[262]71 self.filedCruiseAltitude = flight.filedCruiseAltitude
72 self.cruiseAltitude = flight.cruiseAltitude
73 self.route = flight.route
[97]74
[262]75 self.departureMETAR = flight.departureMETAR.upper()
76 self.arrivalMETAR = flight.arrivalMETAR.upper()
[97]77
[262]78 self.departureRunway = flight.departureRunway.upper()
79 self.sid = flight.sid.upper()
[97]80
[262]81 self.star = flight.star
82 self.transition = flight.transition
83 self.approachType = flight.approachType.upper()
84 self.arrivalRunway = flight.arrivalRunway.upper()
[97]85
[262]86 self.flightType = flight.flightType
87 self.online = flight.online
[97]88
[262]89 self.comments = flight.comments
90 self.flightDefects = flight.flightDefects
91 self.delayCodes = flight.delayCodes
[345]92
[97]93 self.blockTimeStart = flight.blockTimeStart
94 self.flightTimeStart = flight.flightTimeStart
95 self.flightTimeEnd = flight.flightTimeEnd
96 self.blockTimeEnd = flight.blockTimeEnd
97 self.flownDistance = flight.flownDistance
[102]98 self.fuelUsed = flight.startFuel - flight.endFuel
[97]99
[262]100 logger = flight.logger
[97]101 self.rating = logger.getRating()
102 self.logLines = logger.lines
103 self.faultLineIndexes = logger.faultLineIndexes
[345]104
[743]105 @property
106 def flightTypeText(self):
107 """Get the text representation of the flight type."""
108 return PIREP._flightTypes[self.flightType]
109
110 @property
111 def blockTimeStartText(self):
112 """Get the beginning of the block time in string format."""
113 return PIREP.formatTimestampForRPC(self.blockTimeStart)
114
115 @property
116 def flightTimeStartText(self):
117 """Get the beginning of the flight time in string format."""
118 return PIREP.formatTimestampForRPC(self.flightTimeStart)
119
120 @property
121 def flightTimeEndText(self):
122 """Get the end of the flight time in string format."""
123 return PIREP.formatTimestampForRPC(self.flightTimeEnd)
124
125 @property
126 def blockTimeEndText(self):
127 """Get the end of the block time in string format."""
128 return PIREP.formatTimestampForRPC(self.blockTimeEnd)
129
[97]130 def getACARSText(self):
131 """Get the ACARS text.
132
133 This is a specially formatted version of the log without the faults."""
134 text = "[MAVA LOGGER X LOG]-[%s]" % (const.VERSION,)
135 for index in range(0, len(self.logLines)):
136 if index not in self.faultLineIndexes:
137 (timeStr, line) = self.logLines[index]
138 if timeStr is not None:
139 text += PIREP._formatLine(timeStr, line)
140 return text
141
142 def getRatingText(self):
143 """Get the rating text.
144
145 This is a specially formatted version of the lines containing the
146 faults."""
147 text = ""
148 for index in self.faultLineIndexes:
149 (timeStr, line) = self.logLines[index]
150 if timeStr is not None:
151 text += PIREP._formatLine(timeStr, line)
[345]152 text += "\n"
[97]153
154 text += "\n[Flight Rating: %.1f]" % (max(0.0, self.rating),)
155
156 return text
[345]157
[97]158 def getTimeComment(self):
159 """Get the time comment.
160
161 This is basically a collection of the delay codes, if any."""
[99]162 if not self.delayCodes:
163 return "UTC"
164 else:
165 s = ""
166 for code in self.delayCodes:
167 if s: s += ", "
[437]168 s += code
[99]169 return s
[97]170
171 def getSTAR(self):
172 """Get the STAR and/or the transition."""
173 star = self.star if self.star is not None else ""
174 if self.transition is not None:
175 if star: star += ", "
176 star += self.transition
177 return star.upper()
[151]178
179 def save(self, path):
180 """Save the PIREP to the given file.
181
182 Returns whether the saving has succeeded."""
183 try:
184 with open(path, "wb") as f:
185 pickle.dump(self, f)
[393]186 return None
[151]187 except Exception, e:
[401]188 error = utf2unicode(str(e))
189 print u"Failed saving PIREP to %s: %s" % (path, error)
[393]190 return error
[743]191
192 def _serialize(self):
193 """Serialize the PIREP for JSON-RPC."""
194 attrs = {}
195 attrs["log"] = self.getACARSText()
196 attrs["numPassengers"] = self.numPassengers
197 attrs["numCrew"] = self.numCrew
198 attrs["cargoWeight"] = self.cargoWeight
199 attrs["bagWeight"] = self.bagWeight
200 attrs["mailWeight"] = self.mailWeight
201 attrs["flightType"] = self.flightTypeText
202 attrs["online"] = 1 if self.online else 0
203 attrs["blockTimeStart"] = self.blockTimeStartText
204 attrs["blockTimeEnd"] = self.blockTimeEndText
205 attrs["flightTimeStart"] = self.flightTimeStartText
206 attrs["flightTimeEnd"] = self.flightTimeEndText
207 attrs["timeComment"] = self.getTimeComment()
208 attrs["fuelUsed"] = self.fuelUsed
209 attrs["departureRunway"] = self.departureRunway
210 attrs["arrivalRunway"] = self.arrivalRunway
211 attrs["departureMETAR"] = self.departureMETAR
212 attrs["arrivalMETAR"] = self.arrivalMETAR
213 attrs["filedCruiseLevel"] = self.filedCruiseAltitude / 100.0
214 attrs["cruiseLevel"] = self.cruiseAltitude / 100.0
215 attrs["sid"] = self.sid
216 attrs["route"] = self.route
217 attrs["star"] = self.star
218 attrs["approachType"] = self.approachType
219 attrs["comments"] = self.comments
220 attrs["flightDefects"] = self.flightDefects
221 attrs["ratingText"] = self.getRatingText()
222 attrs["rating"] = max(0.0, self.rating)
223 attrs["flownDistance"] = self.flownDistance
224 # FIXME: it should be stored in the PIREP when it is sent later
225 attrs["flightDate"] = datetime.date.today().strftime("%Y-%m-%d")
226
227 return ([], attrs)
Note: See TracBrowser for help on using the repository browser.