1 |
|
---|
2 | from util import utf2unicode
|
---|
3 |
|
---|
4 | import const
|
---|
5 | import cPickle as pickle
|
---|
6 | import datetime
|
---|
7 | import time
|
---|
8 |
|
---|
9 | #------------------------------------------------------------------------------
|
---|
10 |
|
---|
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.
|
---|
18 |
|
---|
19 | #------------------------------------------------------------------------------
|
---|
20 |
|
---|
21 | class PIREP(object):
|
---|
22 | """A pilot's report of a flight."""
|
---|
23 | _flightTypes = { const.FLIGHTTYPE_SCHEDULED : "SCHEDULED",
|
---|
24 | const.FLIGHTTYPE_OLDTIMER : "OT",
|
---|
25 | const.FLIGHTTYPE_VIP : "VIP",
|
---|
26 | const.FLIGHTTYPE_CHARTER : "CHARTER" }
|
---|
27 |
|
---|
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 + "]"
|
---|
33 |
|
---|
34 | @staticmethod
|
---|
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
|
---|
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:
|
---|
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
|
---|
56 | except Exception, e:
|
---|
57 | print "Failed loading PIREP from %s: %s" % (path,
|
---|
58 | utf2unicode(str(e)))
|
---|
59 | return None
|
---|
60 |
|
---|
61 | def __init__(self, flight):
|
---|
62 | """Initialize the PIREP from the given flight."""
|
---|
63 | self.bookedFlight = flight.bookedFlight
|
---|
64 |
|
---|
65 | self.numCrew = flight.numCrew
|
---|
66 | self.numPassengers = flight.numPassengers
|
---|
67 | self.bagWeight = flight.bagWeight
|
---|
68 | self.cargoWeight = flight.cargoWeight
|
---|
69 | self.mailWeight = flight.mailWeight
|
---|
70 |
|
---|
71 | self.filedCruiseAltitude = flight.filedCruiseAltitude
|
---|
72 | self.cruiseAltitude = flight.cruiseAltitude
|
---|
73 | self.route = flight.route
|
---|
74 |
|
---|
75 | self.departureMETAR = flight.departureMETAR.upper()
|
---|
76 | self.arrivalMETAR = flight.arrivalMETAR.upper()
|
---|
77 |
|
---|
78 | self.departureRunway = flight.departureRunway.upper()
|
---|
79 | self.sid = flight.sid.upper()
|
---|
80 |
|
---|
81 | self.star = flight.star
|
---|
82 | self.transition = flight.transition
|
---|
83 | self.approachType = flight.approachType.upper()
|
---|
84 | self.arrivalRunway = flight.arrivalRunway.upper()
|
---|
85 |
|
---|
86 | self.flightType = flight.flightType
|
---|
87 | self.online = flight.online
|
---|
88 |
|
---|
89 | self.comments = flight.comments
|
---|
90 | self.flightDefects = flight.flightDefects
|
---|
91 | self.delayCodes = flight.delayCodes
|
---|
92 |
|
---|
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
|
---|
98 | self.fuelUsed = flight.startFuel - flight.endFuel
|
---|
99 |
|
---|
100 | logger = flight.logger
|
---|
101 | self.rating = logger.getRating()
|
---|
102 | self.logLines = logger.lines
|
---|
103 | self.faultLineIndexes = logger.faultLineIndexes
|
---|
104 |
|
---|
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 |
|
---|
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)
|
---|
152 | text += "\n"
|
---|
153 |
|
---|
154 | text += "\n[Flight Rating: %.1f]" % (max(0.0, self.rating),)
|
---|
155 |
|
---|
156 | return text
|
---|
157 |
|
---|
158 | def getTimeComment(self):
|
---|
159 | """Get the time comment.
|
---|
160 |
|
---|
161 | This is basically a collection of the delay codes, if any."""
|
---|
162 | if not self.delayCodes:
|
---|
163 | return "UTC"
|
---|
164 | else:
|
---|
165 | s = ""
|
---|
166 | for code in self.delayCodes:
|
---|
167 | if s: s += ", "
|
---|
168 | s += code
|
---|
169 | return s
|
---|
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()
|
---|
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)
|
---|
186 | return None
|
---|
187 | except Exception, e:
|
---|
188 | error = utf2unicode(str(e))
|
---|
189 | print u"Failed saving PIREP to %s: %s" % (path, error)
|
---|
190 | return error
|
---|
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)
|
---|