Ignore:
Timestamp:
04/21/12 14:49:45 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
Message:

The PIREP can be created and sent.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/web.py

    r88 r97  
    44
    55import const
     6import util
    67
    78import threading
     
    482483#------------------------------------------------------------------------------
    483484
     485class SendPIREP(Request):
     486    """A request to send a PIREP to the MAVA website."""
     487    _flightTypes = { const.FLIGHTTYPE_SCHEDULED : "SCHEDULED",
     488                     const.FLIGHTTYPE_OLDTIMER : "OT",
     489                     const.FLIGHTTYPE_VIP : "VIP",
     490                     const.FLIGHTTYPE_CHARTER : "CHARTER" }
     491
     492    _latin2Encoder = codecs.getencoder("iso-8859-2")
     493
     494    def __init__(self, callback, pirep):
     495        """Construct the request for the given PIREP."""
     496        super(SendPIREP, self).__init__(callback)
     497        self._pirep = pirep
     498
     499    def run(self):
     500        """Perform the retrieval opf the METARs."""
     501        url = "http://www.virtualairlines.hu/malevacars.php"
     502
     503        pirep = self._pirep
     504
     505        data = {}
     506        data["acarsdata"] = pirep.getACARSText()
     507
     508        bookedFlight = pirep.bookedFlight
     509        data["foglalas_id"] = bookedFlight.id
     510        data["repdate"] = bookedFlight.departureTime.date().strftime("%Y-%m-%d")
     511        data["fltnum"] = bookedFlight.callsign
     512        data["depap"] = bookedFlight.departureICAO
     513        data["arrap"] = bookedFlight.arrivalICAO
     514        data["pass"] = str(bookedFlight.numPassengers)
     515        data["crew"] = str(bookedFlight.numCrew)
     516        data["cargo"] = str(pirep.cargoWeight)
     517        data["bag"] = str(bookedFlight.bagWeight)
     518        data["mail"] = str(bookedFlight.mailWeight)
     519       
     520        data["flttype"] = SendPIREP._flightTypes[pirep.flightType]
     521        data["onoff"] = "1" if pirep.online else "0"
     522        data["bt_dep"] = util.getTimestampString(pirep.blockTimeStart)
     523        data["bt_arr"] = util.getTimestampString(pirep.blockTimeEnd)
     524        data["bt_dur"] = util.getTimeIntervalString(pirep.blockTimeEnd -
     525                                                    pirep.blockTimeStart)
     526        data["ft_dep"] = util.getTimestampString(pirep.flightTimeStart)
     527        data["ft_arr"] = util.getTimestampString(pirep.flightTimeEnd)
     528        data["ft_dur"] = util.getTimeIntervalString(pirep.flightTimeEnd -
     529                                                    pirep.flightTimeStart)
     530        data["timecomm"] = pirep.getTimeComment()
     531        data["fuel"] = "%.0f" % (pirep.fuelUsed,)
     532        data["dep_rwy"] = pirep.departureRunway
     533        data["arr_rwy"] = pirep.arrivalRunway
     534        data["wea_dep"] = pirep.departureMETAR
     535        data["wea_arr"] = pirep.arrivalMETAR
     536        data["alt"] = "FL%.0f" % (pirep.filedCruiseAltitude/100.0,)
     537        if pirep.filedCruiseAltitude!=pirep.cruiseAltitude:
     538            data["mod_alt"] = "FL%.0f" % (pirep.cruiseAltitude/100.0,)
     539        else:
     540            data["mod_alt"] = ""
     541        data["sid"] = pirep.sid
     542        data["navroute"] = pirep.route
     543        data["star"] = pirep.getSTAR()
     544        data["aprtype"] = pirep.approachType
     545        data["diff"] = "2"
     546        data["comment"] = SendPIREP._latin2Encoder(pirep.comments)[0]
     547        data["flightdefect"] = SendPIREP._latin2Encoder(pirep.flightDefects)[0]
     548        data["kritika"] = pirep.getRatingText()
     549        data["flightRating"] = "%.1f" % (max(0.0, pirep.rating),)
     550        data["distance"] = "%.3f" % (pirep.flownDistance,)
     551        data["insdate"] = datetime.date.today().strftime("%Y-%m-%d")
     552
     553        f = urllib2.urlopen(url, urllib.urlencode(data), timeout = 10.0)
     554        try:
     555            result = Result()
     556            line = f.readline().strip()
     557            print "PIREP result from website:", line
     558            result.success = line=="OK"
     559            result.alreadyFlown = line=="MARVOLT"
     560            result.notAvailable = line=="NOMORE"
     561        finally:
     562            f.close()
     563
     564        return result   
     565
     566#------------------------------------------------------------------------------
     567
    484568class Handler(threading.Thread):
    485569    """The handler for the web services.
     
    515599        """Get the METARs for the given airports."""
    516600        self._addRequest(GetMETARs(callback, airports))
     601
     602    def sendPIREP(self, callback, pirep):
     603        """Send the given PIREP."""
     604        self._addRequest(SendPIREP(callback, pirep))
    517605       
    518606    def run(self):
Note: See TracChangeset for help on using the changeset viewer.