source: src/mlx/web.py@ 305:ddc2dfec2080

Last change on this file since 305:ddc2dfec2080 was 305:ddc2dfec2080, checked in by István Váradi <ivaradi@…>, 12 years ago

The aircraft type name of booked flights is handled and is sent in the ACARS

File size: 35.9 KB
Line 
1
2import const
3import util
4
5import threading
6import sys
7import urllib
8import urllib2
9import hashlib
10import time
11import datetime
12import codecs
13import traceback
14import xml.sax
15
16#---------------------------------------------------------------------------------------
17
18## @package mlx.web
19#
20# Web interface.
21#
22# This module implements a thread that can perform (HTTP) requests
23# asynchronously. When the request is performed, a callback is called. The main
24# interface is the \ref Handler class. Each of its functions creates a \ref
25# Request subclass instance and puts it to the request queue. The handler
26# thread then takes the requests one by one, and executes them.
27#
28# This module also defines some data classes the contents of which are
29# retrieved or sent via HTTP. \ref BookedFlight contains data of a flight
30# booked on the MAVA website, \ref Fleet and \ref Plane represents the MAVA
31# fleet and the gates at Ferihegy and \ref NOTAM is a NOTAM.
32
33#---------------------------------------------------------------------------------------
34
35def readline(f):
36 """Read a line from the given file.
37
38 The line is stripped and empty lines are discarded."""
39 while True:
40 line = f.readline()
41 if not line: return ""
42 line = line.strip()
43 if line:
44 return line
45
46#---------------------------------------------------------------------------------------
47
48class BookedFlight(object):
49 """A flight that was booked."""
50 TYPECODE2TYPE = { "736" : const.AIRCRAFT_B736,
51 "73G" : const.AIRCRAFT_B737,
52 "738" : const.AIRCRAFT_B738,
53 "73H" : const.AIRCRAFT_B738C,
54 "733" : const.AIRCRAFT_B733,
55 "734" : const.AIRCRAFT_B734,
56 "735" : const.AIRCRAFT_B735,
57 "DH4" : const.AIRCRAFT_DH8D,
58 "762" : const.AIRCRAFT_B762,
59 "763" : const.AIRCRAFT_B763,
60 "CR2" : const.AIRCRAFT_CRJ2,
61 "F70" : const.AIRCRAFT_F70,
62 "LI2" : const.AIRCRAFT_DC3,
63 "TU3" : const.AIRCRAFT_T134,
64 "TU5" : const.AIRCRAFT_T154,
65 "YK4" : const.AIRCRAFT_YK40 }
66
67 TYPE2TYPECODE = { const.AIRCRAFT_B736 : "736",
68 const.AIRCRAFT_B737 : "73G",
69 const.AIRCRAFT_B738 : "738",
70 const.AIRCRAFT_B738C : "73H",
71 const.AIRCRAFT_B733 : "733",
72 const.AIRCRAFT_B734 : "734",
73 const.AIRCRAFT_B735 : "735",
74 const.AIRCRAFT_DH8D : "DH4",
75 const.AIRCRAFT_B762 : "762",
76 const.AIRCRAFT_B763 : "763",
77 const.AIRCRAFT_CRJ2 : "CR2",
78 const.AIRCRAFT_F70 : "F70",
79 const.AIRCRAFT_DC3 : "LI2",
80 const.AIRCRAFT_T134 : "TU3",
81 const.AIRCRAFT_T154 : "TU5",
82 const.AIRCRAFT_YK40 : "YK4" }
83
84 @staticmethod
85 def getDateTime(date, time):
86 """Get a datetime object from the given textual date and time."""
87 return datetime.datetime.strptime(date + " " + time,
88 "%Y-%m-%d %H:%M:%S")
89
90 def __init__(self, id = None):
91 """Construct a booked flight with the given ID."""
92 self.id = id
93
94 def readFromWeb(self, f):
95 """Read the data of the flight from the web via the given file
96 object."""
97 self.callsign = readline(f)
98
99 date = readline(f)
100 print "web.BookedFlight.readFromWeb: date:", date
101 if date=="0000-00-00": date = "0001-01-01"
102
103 self.departureICAO = readline(f)
104 self.arrivalICAO = readline(f)
105
106 self._readAircraftType(f)
107 self.tailNumber = readline(f)
108 self.numPassengers = int(readline(f))
109 self.numCrew = int(readline(f))
110 self.bagWeight = int(readline(f))
111 self.cargoWeight = int(readline(f))
112 self.mailWeight = int(readline(f))
113 self.route = readline(f)
114
115 departureTime = readline(f)
116 self.departureTime = BookedFlight.getDateTime(date, departureTime)
117
118 arrivalTime = readline(f)
119 self.arrivalTime = BookedFlight.getDateTime(date, arrivalTime)
120 if self.arrivalTime<self.departureTime:
121 self.arrivalTime += datetime.timedelta(days = 1)
122
123 if not readline(f)==".NEXT.":
124 raise Exception("Invalid line in flight data")
125
126 def readFromFile(self, f):
127 """Read the data of the flight from a file via the given file
128 object."""
129 date = None
130 departureTime = None
131 arrivalTime = None
132
133 line = f.readline()
134 lineNumber = 0
135 while line:
136 lineNumber += 1
137 line = line.strip()
138
139 hashIndex = line.find("#")
140 if hashIndex>=0: line = line[:hashIndex]
141 if line:
142 equalIndex = line.find("=")
143 lineOK = equalIndex>0
144
145 if lineOK:
146 key = line[:equalIndex].strip()
147 value = line[equalIndex+1:].strip().replace("\:", ":")
148
149 lineOK = key and value
150
151 if lineOK:
152 if key=="callsign": self.callsign = value
153 elif key=="date": date = value
154 elif key=="dep_airport": self.departureICAO = value
155 elif key=="dest_airport": self.arrivalICAO = value
156 elif key=="planecode": self.aircraftType = \
157 self._decodeAircraftType(value)
158 elif key=="planetype": self.aircraftTypeName = value
159 elif key=="tail_nr": self.tailNumber = value
160 elif key=="passenger": self.numPassengers = int(value)
161 elif key=="crew": self.numCrew = int(value)
162 elif key=="bag": self.bagWeight = int(value)
163 elif key=="cargo": self.cargoWeight = int(value)
164 elif key=="mail": self.mailWeight = int(value)
165 elif key=="flight_route": self.route = value
166 elif key=="departure_time": departureTime = value
167 elif key=="arrival_time": arrivalTime = value
168 elif key=="foglalas_id":
169 self.id = None if value=="0" else value
170 else: lineOK = False
171
172 if not lineOK:
173 print "web.BookedFlight.readFromFile: line %d is invalid" % \
174 (lineNumber,)
175
176 line = f.readline()
177
178 if date is not None:
179 if departureTime is not None:
180 self.departureTime = BookedFlight.getDateTime(date,
181 departureTime)
182 if arrivalTime is not None:
183 self.arrivalTime = BookedFlight.getDateTime(date,
184 arrivalTime)
185
186 d = dir(self)
187 for attribute in ["callsign", "departureICAO", "arrivalICAO",
188 "aircraftType", "tailNumber",
189 "numPassengers", "numCrew",
190 "bagWeight", "cargoWeight", "mailWeight",
191 "route", "departureTime", "arrivalTime"]:
192 if attribute not in d:
193 raise Exception("Attribute %s could not be read" % (attribute,))
194
195 if "aircraftTypeName" not in d:
196 self.aircraftTypeName = \
197 BookedFlight.TYPE2TYPECODE[self.aircraftType]
198
199 def writeIntoFile(self, f):
200 """Write the flight into a file."""
201 print >> f, "callsign=%s" % (self.callsign,)
202 date = self.departureTime.date()
203 print >> f, "date=%04d-%02d-%0d" % (date.year, date.month, date.day)
204 print >> f, "dep_airport=%s" % (self.departureICAO,)
205 print >> f, "dest_airport=%s" % (self.arrivalICAO,)
206 print >> f, "planecode=%s" % \
207 (BookedFlight.TYPE2TYPECODE[self.aircraftType],)
208 print >> f, "planetype=%s" % (self.aircraftTypeName,)
209 print >> f, "tail_nr=%s" % (self.tailNumber,)
210 print >> f, "passenger=%d" % (self.numPassengers,)
211 print >> f, "crew=%d" % (self.numCrew,)
212 print >> f, "bag=%d" % (self.bagWeight,)
213 print >> f, "cargo=%d" % (self.cargoWeight,)
214 print >> f, "mail=%d" % (self.mailWeight,)
215 print >> f, "flight_route=%s" % (self.route,)
216 departureTime = self.departureTime
217 print >> f, "departure_time=%02d\\:%02d\\:%02d" % \
218 (departureTime.hour, departureTime.minute, departureTime.second)
219 arrivalTime = self.arrivalTime
220 print >> f, "arrival_time=%02d\\:%02d\\:%02d" % \
221 (arrivalTime.hour, arrivalTime.minute, arrivalTime.second)
222 print >> f, "foglalas_id=%s" % ("0" if self.id is None else self.id,)
223
224 def _readAircraftType(self, f):
225 """Read the aircraft type from the given file."""
226 line = readline(f)
227 typeCode = line[:3]
228 self.aircraftType = self._decodeAircraftType(typeCode)
229 self.aircraftTypeName = line[3:]
230
231 def _decodeAircraftType(self, typeCode):
232 """Decode the aircraft type from the given typeCode."""
233 if typeCode in self.TYPECODE2TYPE:
234 return self.TYPECODE2TYPE[typeCode]
235 else:
236 raise Exception("Invalid aircraft type code: '" + typeCode + "'")
237
238 def __repr__(self):
239 """Get a representation of the flight."""
240 s = "<Flight: %s-%s, %s, %s-%s," % (self.departureICAO,
241 self.arrivalICAO,
242 self.route,
243 self.departureTime, self.arrivalTime)
244 s += " %d %s," % (self.aircraftType, self.tailNumber)
245 s += " pax=%d, crew=%d, bag=%d, cargo=%d, mail=%d" % \
246 (self.numPassengers, self.numCrew,
247 self.bagWeight, self.cargoWeight, self.mailWeight)
248 s += ">"
249 return s
250
251#------------------------------------------------------------------------------
252
253class Plane(object):
254 """Information about an airplane in the fleet."""
255 def __init__(self, s):
256 """Build a plane info based on the given string.
257
258 The string consists of three, space-separated fields.
259 The first field is the tail number, the second field is the gate
260 number, the third field is the plane's status as a character."""
261 try:
262 words = s.split(" ")
263 tailNumber = words[0]
264 self.tailNumber = tailNumber
265
266 status = words[2] if len(words)>2 else None
267 self.status = const.PLANE_HOME if status=="H" else \
268 const.PLANE_AWAY if status=="A" else \
269 const.PLANE_PARKING if status=="P" else \
270 const.PLANE_UNKNOWN
271
272 gateNumber = words[1] if len(words)>1 else ""
273 self.gateNumber = gateNumber if gateNumber else None
274
275 except:
276 print >> sys.stderr, "Plane string is invalid: '" + s + "'"
277 self.tailNumber = None
278
279 def __repr__(self):
280 """Get the representation of the plane object."""
281 s = "<Plane: %s %s" % (self.tailNumber,
282 "home" if self.status==const.PLANE_HOME else \
283 "away" if self.status==const.PLANE_AWAY else \
284 "parking" if self.status==const.PLANE_PARKING \
285 else "unknown")
286 if self.gateNumber is not None:
287 s += " (gate " + self.gateNumber + ")"
288 s += ">"
289 return s
290
291
292#------------------------------------------------------------------------------
293
294class Fleet(object):
295 """Information about the whole fleet."""
296 def __init__(self, f):
297 """Construct the fleet information by reading the given file object."""
298 self._planes = {}
299 while True:
300 line = readline(f)
301 if not line or line == "#END": break
302
303 plane = Plane(line)
304 if plane.tailNumber is not None:
305 self._planes[plane.tailNumber] = plane
306
307 def isGateConflicting(self, plane):
308 """Check if the gate of the given plane conflicts with another plane's
309 position."""
310 for p in self._planes.itervalues():
311 if p.tailNumber!=plane.tailNumber and \
312 p.status==const.PLANE_HOME and \
313 p.gateNumber==plane.gateNumber:
314 return True
315
316 return False
317
318 def getOccupiedGateNumbers(self):
319 """Get a set containing the numbers of the gates occupied by planes."""
320 gateNumbers = set()
321 for p in self._planes.itervalues():
322 if p.status==const.PLANE_HOME and p.gateNumber:
323 gateNumbers.add(p.gateNumber)
324 return gateNumbers
325
326 def updatePlane(self, tailNumber, status, gateNumber = None):
327 """Update the status of the given plane."""
328 if tailNumber in self._planes:
329 plane = self._planes[tailNumber]
330 plane.status = status
331 plane.gateNumber = gateNumber
332
333 def __iter__(self):
334 """Get an iterator over the planes."""
335 for plane in self._planes.itervalues():
336 yield plane
337
338 def __getitem__(self, tailNumber):
339 """Get the plane with the given tail number.
340
341 If the plane is not in the fleet, None is returned."""
342 return self._planes[tailNumber] if tailNumber in self._planes else None
343
344 def __repr__(self):
345 """Get the representation of the fleet object."""
346 return self._planes.__repr__()
347
348#------------------------------------------------------------------------------
349
350class NOTAM(object):
351 """A NOTAM for an airport."""
352 def __init__(self, begin, notice, end = None, permanent = False,
353 repeatCycle = None):
354 """Construct the NOTAM."""
355 self.begin = begin
356 self.notice = notice
357 self.end = end
358 self.permanent = permanent
359 self.repeatCycle = None
360
361 def __repr__(self):
362 """Get the representation of the NOTAM."""
363 s = "<NOTAM " + str(self.begin)
364 if self.end:
365 s += " - " + str(self.end)
366 elif self.permanent:
367 s += " - PERMANENT"
368 if self.repeatCycle:
369 s += " (" + self.repeatCycle + ")"
370 s += ": " + self.notice
371 s += ">"
372 return s
373
374#------------------------------------------------------------------------------
375
376class NOTAMHandler(xml.sax.handler.ContentHandler):
377 """A handler for the NOTAM database."""
378 def __init__(self, airportICAOs):
379 """Construct the handler for the airports with the given ICAO code."""
380 self._notams = {}
381 for icao in airportICAOs:
382 self._notams[icao] = []
383
384 def startElement(self, name, attrs):
385 """Start an element."""
386 if name!="notam" or \
387 "A" not in attrs or not attrs["A"] or \
388 "B" not in attrs or not attrs["B"] or \
389 "E" not in attrs or not attrs["E"]:
390 return
391
392 icao = attrs["A"]
393 if icao not in self._notams:
394 return
395
396 begin = datetime.datetime.strptime(attrs["B"], "%Y-%m-%d %H:%M:%S")
397
398 c = attrs["C"] if "C" in attrs else None
399 end = datetime.datetime.strptime(c, "%Y-%m-%d %H:%M:%S") if c else None
400
401 permanent = attrs["C_flag"]=="PERM" if "C_flag" in attrs else False
402
403 repeatCycle = attrs["D"] if "D" in attrs else None
404
405 self._notams[icao].append(NOTAM(begin, attrs["E"], end = end,
406 permanent = permanent,
407 repeatCycle = repeatCycle))
408
409 def get(self, icao):
410 """Get the NOTAMs for the given ICAO code."""
411 return self._notams[icao] if icao in self._notams else []
412
413#------------------------------------------------------------------------------
414
415class Result(object):
416 """A result object.
417
418 An instance of this filled with the appropriate data is passed to the
419 callback function on each request."""
420
421 def __repr__(self):
422 """Get a representation of the result."""
423 s = "<Result:"
424 for (key, value) in self.__dict__.iteritems():
425 s += " " + key + "=" + unicode(value)
426 s += ">"
427 return s
428
429#------------------------------------------------------------------------------
430
431class Request(object):
432 """Base class for requests.
433
434 It handles any exceptions and the calling of the callback.
435
436 If an exception occurs during processing, the callback is called with
437 the two parameters: a boolean value of False, and the exception object.
438
439 If no exception occurs, the callback is called with True and the return
440 value of the run() function.
441
442 If the callback function throws an exception, that is caught and logged
443 to the debug log."""
444 def __init__(self, callback):
445 """Construct the request."""
446 self._callback = callback
447
448 def perform(self):
449 """Perform the request.
450
451 The object's run() function is called. If it throws an exception,
452 the callback is called with False, and the exception. Otherwise the
453 callback is called with True and the return value of the run()
454 function. Any exceptions thrown by the callback are caught and
455 reported."""
456 try:
457 result = self.run()
458 returned = True
459 except Exception, e:
460 traceback.print_exc()
461 result = e
462 returned = False
463
464 try:
465 self._callback(returned, result)
466 except Exception, e:
467 print >> sys.stderr, "web.Handler.Request.perform: callback throwed an exception: " + str(e)
468 traceback.print_exc()
469
470#------------------------------------------------------------------------------
471
472class Login(Request):
473 """A login request."""
474 iso88592decoder = codecs.getdecoder("iso-8859-2")
475
476 def __init__(self, callback, pilotID, password, entranceExam):
477 """Construct the login request with the given pilot ID and
478 password."""
479 super(Login, self).__init__(callback)
480
481 self._pilotID = pilotID
482 self._password = password
483 self._entranceExam = entranceExam
484
485 def run(self):
486 """Perform the login request."""
487 md5 = hashlib.md5()
488 md5.update(self._pilotID)
489 pilotID = md5.hexdigest()
490
491 md5 = hashlib.md5()
492 md5.update(self._password)
493 password = md5.hexdigest()
494
495 if self._entranceExam:
496 url = "http://www.virtualairlines.hu/ellenorzo/getflightplan.php?pid=%s" % \
497 (pilotID,)
498 else:
499 url = "http://www.virtualairlines.hu/leker2.php?pid=%s&psw=%s" % \
500 (pilotID, password)
501
502 result = Result()
503 result.entranceExam = self._entranceExam
504
505 f = urllib2.urlopen(url, timeout = 10.0)
506
507 status = readline(f)
508 if self._entranceExam:
509 result.loggedIn = status != "#NOEXAM"
510 else:
511 result.loggedIn = status == ".OK."
512
513 if result.loggedIn:
514 result.pilotID = self._pilotID
515 result.password = self._password
516 result.flights = []
517 # FIXME: this may not be the correct behaviour
518 # for an entrance exam, but the website returns
519 # an error
520 if self._entranceExam:
521 result.pilotName = result.pilotID
522 result.exams = ""
523 else:
524 result.pilotName = self.iso88592decoder(readline(f))[0]
525 result.exams = readline(f)
526
527 while True:
528 line = readline(f)
529 if not line or line == "#ENDPIREP": break
530
531 flight = BookedFlight(line)
532 flight.readFromWeb(f)
533 result.flights.append(flight)
534
535 result.flights.sort(cmp = lambda flight1, flight2:
536 cmp(flight1.departureTime,
537 flight2.departureTime))
538
539 f.close()
540
541 return result
542
543#------------------------------------------------------------------------------
544
545class GetFleet(Request):
546 """Request to get the fleet from the website."""
547
548 def __init__(self, callback):
549 """Construct the fleet request."""
550 super(GetFleet, self).__init__(callback)
551
552 def run(self):
553 """Perform the login request."""
554 url = "http://www.virtualairlines.hu/onlinegates_get.php"
555
556 f = urllib2.urlopen(url, timeout = 10.0)
557 result = Result()
558 result.fleet = Fleet(f)
559 f.close()
560
561 return result
562
563#------------------------------------------------------------------------------
564
565class UpdatePlane(Request):
566 """Update the status of one of the planes in the fleet."""
567 def __init__(self, callback, tailNumber, status, gateNumber = None):
568 """Construct the request."""
569 super(UpdatePlane, self).__init__(callback)
570 self._tailNumber = tailNumber
571 self._status = status
572 self._gateNumber = gateNumber
573
574 def run(self):
575 """Perform the plane update."""
576 url = "http://www.virtualairlines.hu/onlinegates_set.php"
577
578 status = "H" if self._status==const.PLANE_HOME else \
579 "A" if self._status==const.PLANE_AWAY else \
580 "P" if self._status==const.PLANE_PARKING else ""
581
582 gateNumber = self._gateNumber if self._gateNumber else ""
583
584 data = urllib.urlencode([("lajstrom", self._tailNumber),
585 ("status", status),
586 ("kapu", gateNumber)])
587
588 f = urllib2.urlopen(url, data, timeout = 10.0)
589 line = readline(f)
590
591 result = Result()
592 result.success = line == "OK"
593
594 return result
595
596#------------------------------------------------------------------------------
597
598class GetNOTAMs(Request):
599 """Get the NOTAMs from EURoutePro and select the ones we are interested
600 in."""
601 def __init__(self, callback, departureICAO, arrivalICAO):
602 """Construct the request for the given airports."""
603 super(GetNOTAMs, self).__init__(callback)
604 self._departureICAO = departureICAO
605 self._arrivalICAO = arrivalICAO
606
607 def run(self):
608 """Perform the retrieval of the NOTAMs."""
609 xmlParser = xml.sax.make_parser()
610 notamHandler = NOTAMHandler([self._departureICAO, self._arrivalICAO])
611 xmlParser.setContentHandler(notamHandler)
612
613 url = "http://notams.euroutepro.com/notams.xml"
614
615 f = urllib2.urlopen(url, timeout = 10.0)
616 try:
617 xmlParser.parse(f)
618 finally:
619 f.close()
620
621 result = Result()
622 result.departureNOTAMs = notamHandler.get(self._departureICAO)
623 result.arrivalNOTAMs = notamHandler.get(self._arrivalICAO)
624
625 return result
626
627#------------------------------------------------------------------------------
628
629class GetMETARs(Request):
630 """Get the METARs from the NOAA website for certain airport ICAOs."""
631
632 def __init__(self, callback, airports):
633 """Construct the request for the given airports."""
634 super(GetMETARs, self).__init__(callback)
635 self._airports = airports
636
637 def run(self):
638 """Perform the retrieval opf the METARs."""
639 url = "http://www.aviationweather.gov/adds/dataserver_current/httpparam?"
640 data = urllib.urlencode([ ("dataSource" , "metars"),
641 ("requestType", "retrieve"),
642 ("format", "csv"),
643 ("stationString", " ".join(self._airports)),
644 ("hoursBeforeNow", "24"),
645 ("mostRecentForEachStation", "constraint")])
646 url += data
647 f = urllib2.urlopen(url, timeout = 10.0)
648 try:
649 result = Result()
650 result.metars = {}
651 for line in iter(f.readline, ""):
652 if len(line)>5 and line[4]==' ':
653 icao = line[0:4]
654 if icao in self._airports:
655 result.metars[icao] = line.strip().split(",")[0]
656 finally:
657 f.close()
658
659 return result
660
661#------------------------------------------------------------------------------
662
663class SendPIREP(Request):
664 """A request to send a PIREP to the MAVA website."""
665 _flightTypes = { const.FLIGHTTYPE_SCHEDULED : "SCHEDULED",
666 const.FLIGHTTYPE_OLDTIMER : "OT",
667 const.FLIGHTTYPE_VIP : "VIP",
668 const.FLIGHTTYPE_CHARTER : "CHARTER" }
669
670 _latin2Encoder = codecs.getencoder("iso-8859-2")
671
672 def __init__(self, callback, pirep):
673 """Construct the request for the given PIREP."""
674 super(SendPIREP, self).__init__(callback)
675 self._pirep = pirep
676
677 def run(self):
678 """Perform the retrieval opf the METARs."""
679 url = "http://www.virtualairlines.hu/malevacars.php"
680
681 pirep = self._pirep
682
683 data = {}
684 data["acarsdata"] = pirep.getACARSText()
685
686 bookedFlight = pirep.bookedFlight
687 data["foglalas_id"] = bookedFlight.id
688 data["repdate"] = bookedFlight.departureTime.date().strftime("%Y-%m-%d")
689 data["fltnum"] = bookedFlight.callsign
690 data["depap"] = bookedFlight.departureICAO
691 data["arrap"] = bookedFlight.arrivalICAO
692 data["pass"] = str(pirep.numPassengers)
693 data["crew"] = str(pirep.numCrew)
694 data["cargo"] = str(pirep.cargoWeight)
695 data["bag"] = str(pirep.bagWeight)
696 data["mail"] = str(pirep.mailWeight)
697
698 data["flttype"] = SendPIREP._flightTypes[pirep.flightType]
699 data["onoff"] = "1" if pirep.online else "0"
700 data["bt_dep"] = util.getTimestampString(pirep.blockTimeStart)
701 data["bt_arr"] = util.getTimestampString(pirep.blockTimeEnd)
702 data["bt_dur"] = util.getTimeIntervalString(pirep.blockTimeEnd -
703 pirep.blockTimeStart)
704 data["ft_dep"] = util.getTimestampString(pirep.flightTimeStart)
705 data["ft_arr"] = util.getTimestampString(pirep.flightTimeEnd)
706 data["ft_dur"] = util.getTimeIntervalString(pirep.flightTimeEnd -
707 pirep.flightTimeStart)
708 data["timecomm"] = pirep.getTimeComment()
709 data["fuel"] = "%.0f" % (pirep.fuelUsed,)
710 data["dep_rwy"] = pirep.departureRunway
711 data["arr_rwy"] = pirep.arrivalRunway
712 data["wea_dep"] = pirep.departureMETAR
713 data["wea_arr"] = pirep.arrivalMETAR
714 data["alt"] = "FL%.0f" % (pirep.filedCruiseAltitude/100.0,)
715 if pirep.filedCruiseAltitude!=pirep.cruiseAltitude:
716 data["mod_alt"] = "FL%.0f" % (pirep.cruiseAltitude/100.0,)
717 else:
718 data["mod_alt"] = ""
719 data["sid"] = pirep.sid
720 data["navroute"] = pirep.route
721 data["star"] = pirep.getSTAR()
722 data["aprtype"] = pirep.approachType
723 data["diff"] = "2"
724 data["comment"] = SendPIREP._latin2Encoder(pirep.comments)[0]
725 data["flightdefect"] = SendPIREP._latin2Encoder(pirep.flightDefects)[0]
726 data["kritika"] = pirep.getRatingText()
727 data["flightrating"] = "%.1f" % (max(0.0, pirep.rating),)
728 data["distance"] = "%.3f" % (pirep.flownDistance,)
729 data["insdate"] = datetime.date.today().strftime("%Y-%m-%d")
730
731 f = urllib2.urlopen(url, urllib.urlencode(data), timeout = 10.0)
732 try:
733 result = Result()
734 line = f.readline().strip()
735 print "PIREP result from website:", line
736 result.success = line=="OK"
737 result.alreadyFlown = line=="MARVOLT"
738 result.notAvailable = line=="NOMORE"
739 finally:
740 f.close()
741
742 return result
743
744#------------------------------------------------------------------------------
745
746class SendPIREP(Request):
747 """A request to send a PIREP to the MAVA website."""
748 _flightTypes = { const.FLIGHTTYPE_SCHEDULED : "SCHEDULED",
749 const.FLIGHTTYPE_OLDTIMER : "OT",
750 const.FLIGHTTYPE_VIP : "VIP",
751 const.FLIGHTTYPE_CHARTER : "CHARTER" }
752
753 _latin2Encoder = codecs.getencoder("iso-8859-2")
754
755 def __init__(self, callback, pirep):
756 """Construct the request for the given PIREP."""
757 super(SendPIREP, self).__init__(callback)
758 self._pirep = pirep
759
760 def run(self):
761 """Perform the sending of the PIREP."""
762 url = "http://www.virtualairlines.hu/malevacars.php"
763
764 pirep = self._pirep
765
766 data = {}
767 data["acarsdata"] = pirep.getACARSText()
768
769 bookedFlight = pirep.bookedFlight
770 data["foglalas_id"] = bookedFlight.id
771 data["repdate"] = bookedFlight.departureTime.date().strftime("%Y-%m-%d")
772 data["fltnum"] = bookedFlight.callsign
773 data["depap"] = bookedFlight.departureICAO
774 data["arrap"] = bookedFlight.arrivalICAO
775 data["pass"] = str(bookedFlight.numPassengers)
776 data["crew"] = str(bookedFlight.numCrew)
777 data["cargo"] = str(pirep.cargoWeight)
778 data["bag"] = str(bookedFlight.bagWeight)
779 data["mail"] = str(bookedFlight.mailWeight)
780
781 data["flttype"] = SendPIREP._flightTypes[pirep.flightType]
782 data["onoff"] = "1" if pirep.online else "0"
783 data["bt_dep"] = util.getTimestampString(pirep.blockTimeStart)
784 data["bt_arr"] = util.getTimestampString(pirep.blockTimeEnd)
785 data["bt_dur"] = util.getTimeIntervalString(pirep.blockTimeEnd -
786 pirep.blockTimeStart)
787 data["ft_dep"] = util.getTimestampString(pirep.flightTimeStart)
788 data["ft_arr"] = util.getTimestampString(pirep.flightTimeEnd)
789 data["ft_dur"] = util.getTimeIntervalString(pirep.flightTimeEnd -
790 pirep.flightTimeStart)
791 data["timecomm"] = pirep.getTimeComment()
792 data["fuel"] = "%.0f" % (pirep.fuelUsed,)
793 data["dep_rwy"] = pirep.departureRunway
794 data["arr_rwy"] = pirep.arrivalRunway
795 data["wea_dep"] = pirep.departureMETAR
796 data["wea_arr"] = pirep.arrivalMETAR
797 data["alt"] = "FL%.0f" % (pirep.filedCruiseAltitude/100.0,)
798 if pirep.filedCruiseAltitude!=pirep.cruiseAltitude:
799 data["mod_alt"] = "FL%.0f" % (pirep.cruiseAltitude/100.0,)
800 else:
801 data["mod_alt"] = ""
802 data["sid"] = pirep.sid
803 data["navroute"] = pirep.route
804 data["star"] = pirep.getSTAR()
805 data["aprtype"] = pirep.approachType
806 data["diff"] = "2"
807 data["comment"] = SendPIREP._latin2Encoder(pirep.comments)[0]
808 data["flightdefect"] = SendPIREP._latin2Encoder(pirep.flightDefects)[0]
809 data["kritika"] = pirep.getRatingText()
810 data["flightrating"] = "%.1f" % (max(0.0, pirep.rating),)
811 data["distance"] = "%.3f" % (pirep.flownDistance,)
812 data["insdate"] = datetime.date.today().strftime("%Y-%m-%d")
813
814 f = urllib2.urlopen(url, urllib.urlencode(data), timeout = 10.0)
815 try:
816 result = Result()
817 line = f.readline().strip()
818 print "PIREP result from website:", line
819 result.success = line=="OK"
820 result.alreadyFlown = line=="MARVOLT"
821 result.notAvailable = line=="NOMORE"
822 finally:
823 f.close()
824
825 return result
826
827#------------------------------------------------------------------------------
828
829class SendACARS(Request):
830 """A request to send an ACARS to the MAVA website."""
831 _latin2Encoder = codecs.getencoder("iso-8859-2")
832
833 def __init__(self, callback, acars):
834 """Construct the request for the given PIREP."""
835 super(SendACARS, self).__init__(callback)
836 self._acars = acars
837
838 def run(self):
839 """Perform the sending of the ACARS."""
840 print "Sending the online ACARS"
841
842 url = "http://www.virtualairlines.hu/acars2/acarsonline.php"
843
844 acars = self._acars
845 bookedFlight = acars.bookedFlight
846
847 data = {}
848 data["pid"] = acars.pid
849 data["pilot"] = SendACARS._latin2Encoder(acars.pilotName)[0]
850
851 data["pass"] = str(bookedFlight.numPassengers)
852 data["callsign"] = bookedFlight.callsign
853 data["airplane"] = bookedFlight.aircraftTypeName
854 data["from"] = bookedFlight.departureICAO
855 data["to"] = bookedFlight.arrivalICAO
856 data["lajstrom"] = bookedFlight.tailNumber
857
858 data["block_time"] = acars.getBlockTimeText()
859 data["longitude"] = str(acars.state.longitude)
860 data["latitude"] = str(acars.state.latitude)
861 data["altitude"] = str(acars.state.altitude)
862 data["speed"] = str(acars.state.groundSpeed)
863
864 data["event"] = acars.getEventText()
865
866 f = urllib2.urlopen(url, urllib.urlencode(data), timeout = 10.0)
867 try:
868 result = Result()
869 finally:
870 f.close()
871
872 return result
873
874#------------------------------------------------------------------------------
875
876class Handler(threading.Thread):
877 """The handler for the web services.
878
879 It can process one request at a time. The results are passed to a callback
880 function."""
881 def __init__(self):
882 """Construct the handler."""
883 super(Handler, self).__init__()
884
885 self._requests = []
886 self._requestCondition = threading.Condition()
887
888 self.daemon = True
889
890 def login(self, callback, pilotID, password, entranceExam = False):
891 """Enqueue a login request."""
892 self._addRequest(Login(callback, pilotID, password, entranceExam))
893
894 def getFleet(self, callback):
895 """Enqueue a fleet retrieval request."""
896 self._addRequest(GetFleet(callback))
897
898 def updatePlane(self, callback, tailNumber, status, gateNumber = None):
899 """Update the status of the given plane."""
900 self._addRequest(UpdatePlane(callback, tailNumber, status, gateNumber))
901
902 def getNOTAMs(self, callback, departureICAO, arrivalICAO):
903 """Get the NOTAMs for the given two airports."""
904 self._addRequest(GetNOTAMs(callback, departureICAO, arrivalICAO))
905
906 def getMETARs(self, callback, airports):
907 """Get the METARs for the given airports."""
908 self._addRequest(GetMETARs(callback, airports))
909
910 def sendPIREP(self, callback, pirep):
911 """Send the given PIREP."""
912 self._addRequest(SendPIREP(callback, pirep))
913
914 def sendACARS(self, callback, acars):
915 """Send the given ACARS"""
916 self._addRequest(SendACARS(callback, acars))
917
918 def run(self):
919 """Process the requests."""
920 while True:
921 with self._requestCondition:
922 while not self._requests:
923 self._requestCondition.wait()
924 request = self._requests[0]
925 del self._requests[0]
926
927 request.perform()
928
929 def _addRequest(self, request):
930 """Add the given request to the queue."""
931 with self._requestCondition:
932 self._requests.append(request)
933 self._requestCondition.notify()
934
935#------------------------------------------------------------------------------
936
937if __name__ == "__main__":
938 import time
939
940 def callback(returned, result):
941 print returned, unicode(result)
942
943 handler = Handler()
944 handler.start()
945
946 #handler.login(callback, "P096", "V5fwj")
947 #handler.getFleet(callback)
948 # Plane: HA-LEG home (gate 67)
949 #handler.updatePlane(callback, "HA-LQC", const.PLANE_AWAY, "72")
950 #time.sleep(3)
951 #handler.getFleet(callback)
952 #time.sleep(3)
953
954 #handler.getNOTAMs(callback, "LHBP", "EPWA")
955 #handler.getMETARs(callback, ["LHBP", "EPWA"])
956 #time.sleep(5)
957
958 handler.updatePlane(callback, "HA-LON", const.PLANE_AWAY, "")
959 time.sleep(3)
960
961#------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.