Ignore:
Timestamp:
05/01/12 08:29:24 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
Message:

Implemented ACARS sending

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/web.py

    r130 r139  
    5050                      "YK4" : const.AIRCRAFT_YK40 }
    5151
     52    TYPE2TYPECODE = { const.AIRCRAFT_B736 : "736",
     53                      const.AIRCRAFT_B737 : "73G",
     54                      const.AIRCRAFT_B738 : "738",
     55                      const.AIRCRAFT_B733 : "733",
     56                      const.AIRCRAFT_B734 : "734",
     57                      const.AIRCRAFT_B735 : "735",
     58                      const.AIRCRAFT_DH8D : "DH4",
     59                      const.AIRCRAFT_B762 : "762",
     60                      const.AIRCRAFT_B763 : "763",
     61                      const.AIRCRAFT_CRJ2 : "CR2",
     62                      const.AIRCRAFT_F70  : "F70",
     63                      const.AIRCRAFT_DC3  : "LI2",
     64                      const.AIRCRAFT_T134 : "TU3",
     65                      const.AIRCRAFT_T154 : "TU5",
     66                      const.AIRCRAFT_YK40 : "YK4" }
     67   
    5268    def __init__(self, id, f):
    5369        """Construct a booked flight with the given ID.
     
    581597#------------------------------------------------------------------------------
    582598
     599class SendPIREP(Request):
     600    """A request to send a PIREP to the MAVA website."""
     601    _flightTypes = { const.FLIGHTTYPE_SCHEDULED : "SCHEDULED",
     602                     const.FLIGHTTYPE_OLDTIMER : "OT",
     603                     const.FLIGHTTYPE_VIP : "VIP",
     604                     const.FLIGHTTYPE_CHARTER : "CHARTER" }
     605
     606    _latin2Encoder = codecs.getencoder("iso-8859-2")
     607
     608    def __init__(self, callback, pirep):
     609        """Construct the request for the given PIREP."""
     610        super(SendPIREP, self).__init__(callback)
     611        self._pirep = pirep
     612
     613    def run(self):
     614        """Perform the sending of the PIREP."""
     615        url = "http://www.virtualairlines.hu/malevacars.php"
     616
     617        pirep = self._pirep
     618
     619        data = {}
     620        data["acarsdata"] = pirep.getACARSText()
     621
     622        bookedFlight = pirep.bookedFlight
     623        data["foglalas_id"] = bookedFlight.id
     624        data["repdate"] = bookedFlight.departureTime.date().strftime("%Y-%m-%d")
     625        data["fltnum"] = bookedFlight.callsign
     626        data["depap"] = bookedFlight.departureICAO
     627        data["arrap"] = bookedFlight.arrivalICAO
     628        data["pass"] = str(bookedFlight.numPassengers)
     629        data["crew"] = str(bookedFlight.numCrew)
     630        data["cargo"] = str(pirep.cargoWeight)
     631        data["bag"] = str(bookedFlight.bagWeight)
     632        data["mail"] = str(bookedFlight.mailWeight)
     633       
     634        data["flttype"] = SendPIREP._flightTypes[pirep.flightType]
     635        data["onoff"] = "1" if pirep.online else "0"
     636        data["bt_dep"] = util.getTimestampString(pirep.blockTimeStart)
     637        data["bt_arr"] = util.getTimestampString(pirep.blockTimeEnd)
     638        data["bt_dur"] = util.getTimeIntervalString(pirep.blockTimeEnd -
     639                                                    pirep.blockTimeStart)
     640        data["ft_dep"] = util.getTimestampString(pirep.flightTimeStart)
     641        data["ft_arr"] = util.getTimestampString(pirep.flightTimeEnd)
     642        data["ft_dur"] = util.getTimeIntervalString(pirep.flightTimeEnd -
     643                                                    pirep.flightTimeStart)
     644        data["timecomm"] = pirep.getTimeComment()
     645        data["fuel"] = "%.0f" % (pirep.fuelUsed,)
     646        data["dep_rwy"] = pirep.departureRunway
     647        data["arr_rwy"] = pirep.arrivalRunway
     648        data["wea_dep"] = pirep.departureMETAR
     649        data["wea_arr"] = pirep.arrivalMETAR
     650        data["alt"] = "FL%.0f" % (pirep.filedCruiseAltitude/100.0,)
     651        if pirep.filedCruiseAltitude!=pirep.cruiseAltitude:
     652            data["mod_alt"] = "FL%.0f" % (pirep.cruiseAltitude/100.0,)
     653        else:
     654            data["mod_alt"] = ""
     655        data["sid"] = pirep.sid
     656        data["navroute"] = pirep.route
     657        data["star"] = pirep.getSTAR()
     658        data["aprtype"] = pirep.approachType
     659        data["diff"] = "2"
     660        data["comment"] = SendPIREP._latin2Encoder(pirep.comments)[0]
     661        data["flightdefect"] = SendPIREP._latin2Encoder(pirep.flightDefects)[0]
     662        data["kritika"] = pirep.getRatingText()
     663        data["flightrating"] = "%.1f" % (max(0.0, pirep.rating),)
     664        data["distance"] = "%.3f" % (pirep.flownDistance,)
     665        data["insdate"] = datetime.date.today().strftime("%Y-%m-%d")
     666
     667        f = urllib2.urlopen(url, urllib.urlencode(data), timeout = 10.0)
     668        try:
     669            result = Result()
     670            line = f.readline().strip()
     671            print "PIREP result from website:", line
     672            result.success = line=="OK"
     673            result.alreadyFlown = line=="MARVOLT"
     674            result.notAvailable = line=="NOMORE"
     675        finally:
     676            f.close()
     677
     678        return result   
     679
     680#------------------------------------------------------------------------------
     681
     682class SendACARS(Request):
     683    """A request to send an ACARS to the MAVA website."""
     684    _latin2Encoder = codecs.getencoder("iso-8859-2")
     685
     686    def __init__(self, callback, acars):
     687        """Construct the request for the given PIREP."""
     688        super(SendACARS, self).__init__(callback)
     689        self._acars = acars
     690
     691    def run(self):
     692        """Perform the sending of the ACARS."""
     693        url = "http://www.virtualairlines.hu/acars2/acarsonline.php"
     694
     695        acars = self._acars
     696        bookedFlight = acars.bookedFlight
     697
     698        data = {}
     699        data["pid"] = acars.pid
     700        data["pilot"] = SendACARS._latin2Encoder(acars.pilotName)[0]
     701   
     702        data["pass"] = str(bookedFlight.numPassengers)
     703        data["callsign"] = bookedFlight.callsign
     704        data["airplane"] = BookedFlight.TYPE2TYPECODE[bookedFlight.aircraftType]
     705        data["from"] = bookedFlight.departureICAO
     706        data["to"] = bookedFlight.arrivalICAO       
     707        data["lajstrom"] = bookedFlight.tailNumber
     708
     709        data["block_time"] = acars.getBlockTimeText()
     710        data["longitude"] = str(acars.state.longitude)
     711        data["latitude"] = str(acars.state.latitude)
     712        data["altitude"] = str(acars.state.altitude)
     713        data["speed"] = str(acars.state.groundSpeed)
     714       
     715        data["event"] = acars.getEventText()
     716
     717        f = urllib2.urlopen(url, urllib.urlencode(data), timeout = 10.0)
     718        try:
     719            result = Result()
     720        finally:
     721            f.close()
     722
     723        return result   
     724
     725#------------------------------------------------------------------------------
     726
    583727class Handler(threading.Thread):
    584728    """The handler for the web services.
     
    618762        """Send the given PIREP."""
    619763        self._addRequest(SendPIREP(callback, pirep))
     764
     765    def sendACARS(self, callback, acars):
     766        """Send the given ACARS"""
     767        self._addRequest(SendACARS(callback, acars))
    620768       
    621769    def run(self):
Note: See TracChangeset for help on using the changeset viewer.