Changeset 1044:988bfea3e8db


Ignore:
Timestamp:
04/04/22 13:59:17 (3 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
python3
Phase:
public
Message:

RPC is the only communication method (re #357)

Location:
src/mlx
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/config.py

    r1040 r1044  
    259259        #    self._updateURL += "/exp"
    260260        self._updateURLUpdated = True
    261         self._useRPC = True
    262261
    263262        self._messageTypeLevels = {}
     
    668667        if updateURL!=self._updateURL:
    669668            self._updateURL = updateURL
    670             self._modified = True
    671 
    672     @property
    673     def useRPC(self):
    674         """Determine if RPC calls should be used."""
    675         return self._useRPC
    676 
    677     @useRPC.setter
    678     def useRPC(self, useRPC):
    679         """Set whether RPC calls should be used."""
    680         if useRPC!=self._useRPC:
    681             self._useRPC = useRPC
    682669            self._modified = True
    683670
     
    798785            self._updateURLUpdated = True
    799786
    800         self._useRPC = self._getBoolean(config, "general", "useRPC", True)
    801 
    802787        for aircraftType in const.aircraftTypes:
    803788            self._checklists[aircraftType] = \
     
    892877        config.set("update", "urlUpdated", self._updateURLUpdated)
    893878
    894         config.set("general", "useRPC",
    895                    "yes" if self._useRPC else "no")
    896 
    897879        config.add_section(Checklist.SECTION)
    898880        config.add_section(ApproachCallouts.SECTION)
     
    10301012        print("  updateURL:", self._updateURL)
    10311013        print("  updateURLUpdated:", self._updateURLUpdated)
    1032         print("  useRPC:", self._useRPC)
    10331014
    10341015        print("  messageTypeLevels:")
  • src/mlx/gui/flight.py

    r1034 r1044  
    942942            try:
    943943                with open(fileName, "rt") as f:
    944                     bookedFlight.readFromFile(f)
     944                    bookedFlight.readFromFile(f, self._wizard._loginResult.fleet)
    945945                self.addFlight(bookedFlight)
    946946            except Exception as e:
  • src/mlx/gui/prefs.py

    r999 r1044  
    276276
    277277        self._updateURL.set_text(config.updateURL)
    278 
    279         self._useRPC.set_active(config.useRPC)
    280278
    281279        self._settingFromConfig = False
     
    322320        config.autoUpdate = self._autoUpdate.get_active()
    323321        config.updateURL = self._updateURL.get_text()
    324         config.useRPC = self._useRPC.get_active()
    325322
    326323    def _buildGeneral(self):
     
    817814        updateURLBox.pack_start(self._updateURL, True, True, 4)
    818815
    819         self._useRPC = Gtk.CheckButton(xstr("prefs_use_rpc"))
    820         mainBox.pack_start(self._useRPC, False, False, 16)
    821 
    822         self._useRPC.set_use_underline(True)
    823         self._useRPC.set_tooltip_text(xstr("prefs_use_rpc_tooltip"))
    824 
    825816        return mainAlignment
    826817
  • src/mlx/rpc.py

    r1039 r1044  
    194194class BookedFlight(RPCObject):
    195195    """A booked flight."""
    196     # FIXME: copied from web.BookedFlight
    197196    TYPECODE2TYPE = { "B736"  : const.AIRCRAFT_B736,
     197                      "736"   : const.AIRCRAFT_B736,
    198198                      "B737"  : const.AIRCRAFT_B737,
     199                      "73G"   : const.AIRCRAFT_B737,
    199200                      "B738"  : const.AIRCRAFT_B738,
     201                      "738"   : const.AIRCRAFT_B738,
    200202                      "B73H"  : const.AIRCRAFT_B738C,
     203                      "73H"   : const.AIRCRAFT_B738C,
    201204                      "B732"  : const.AIRCRAFT_B732,
     205                      "732"   : const.AIRCRAFT_B732,
    202206                      "B733"  : const.AIRCRAFT_B733,
     207                      "733"   : const.AIRCRAFT_B733,
    203208                      "B734"  : const.AIRCRAFT_B734,
     209                      "734"   : const.AIRCRAFT_B734,
    204210                      "B735"  : const.AIRCRAFT_B735,
     211                      "735"   : const.AIRCRAFT_B735,
    205212                      "DH8D"  : const.AIRCRAFT_DH8D,
     213                      "DH4"   : const.AIRCRAFT_DH8D,
    206214                      "B762"  : const.AIRCRAFT_B762,
     215                      "762"   : const.AIRCRAFT_B762,
    207216                      "B763"  : const.AIRCRAFT_B763,
     217                      "763"   : const.AIRCRAFT_B763,
    208218                      "CRJ2"  : const.AIRCRAFT_CRJ2,
    209                       "F70"  : const.AIRCRAFT_F70,
    210                       "LI2"  : const.AIRCRAFT_DC3,
     219                      "CR2"   : const.AIRCRAFT_CRJ2,
     220                      "F70"   : const.AIRCRAFT_F70,
     221                      "LI2"   : const.AIRCRAFT_DC3,
    211222                      "T134"  : const.AIRCRAFT_T134,
     223                      "TU3"   : const.AIRCRAFT_T134,
    212224                      "T154"  : const.AIRCRAFT_T154,
     225                      "TU5"   : const.AIRCRAFT_T154,
    213226                      "YK40"  : const.AIRCRAFT_YK40,
     227                      "YK4"   : const.AIRCRAFT_YK40,
    214228                      "B462"  : const.AIRCRAFT_B462,
     229                      "146"   : const.AIRCRAFT_B462,
    215230                      "IL62"  : const.AIRCRAFT_IL62 }
    216231
    217     # FIXME: copied from web.BookedFlight
    218232    TYPE2TYPECODE = { const.AIRCRAFT_B736  : "B736",
    219233                      const.AIRCRAFT_B737  : "B737",
     
    236250                      const.AIRCRAFT_IL62  : "IL62" }
    237251
    238     # FIXME: copied from web.BookedFlight
     252    checkFlightTypes = [ const.AIRCRAFT_B736, const.AIRCRAFT_B737,
     253                         const.AIRCRAFT_B738, const.AIRCRAFT_DH8D ]
     254
    239255    @staticmethod
    240256    def _decodeAircraftType(typeCode):
     
    272288            return const.FLIGHTTYPE_SCHEDULED
    273289
    274     # FIXME: copied from web.BookedFlight
    275290    @staticmethod
    276291    def getDateTime(date, time):
     
    279294                                          "%Y-%m-%d %H:%M:%S")
    280295
    281     # FIXME: copied from web.BookedFlight
    282296    STATUS_BOOKED = 1
    283297
    284     # FIXME: copied from web.BookedFlight
    285298    STATUS_REPORTED = 2
    286299
    287     # FIXME: copied from web.BookedFlight
    288300    STATUS_ACCEPTED = 3
    289301
    290     # FIXME: copied from web.BookedFlight
    291302    STATUS_REJECTED = 4
     303
     304    @staticmethod
     305    def forCheckFlight(aircraftType):
     306        """Create a booked flight for a check flight with the given aircraft
     307        type."""
     308        flight = BookedFlight()
     309
     310        flight.departureICAO = "LHBP"
     311        flight.arrivalICAO = "LHBP"
     312
     313        flight.aircraftType = aircraftType
     314        flight.aircraftTypeName = BookedFlight.TYPE2TYPECODE[aircraftType]
     315
     316        # FIXME: perhaps find one for the type
     317        flight.tailNumber = "HA-CHK"
     318        flight.callsign = "HA-CHK"
     319
     320        flight.numPassengers = 0
     321        flight.numChildren = 0
     322        flight.numInfants = 0
     323        flight.numCabinCrew = 0
     324        flight.bagWeight = 0
     325        flight.cargoWeight = 0
     326        flight.mailWeight = 0
     327        flight.route = "DCT"
     328
     329        t = datetime.datetime.now() + datetime.timedelta(minutes = 20)
     330        flight.departureTime = datetime.datetime(t.year, t.month, t.day,
     331                                                 t.hour, t.minute)
     332        t = flight.departureTime + datetime.timedelta(minutes = 30)
     333        flight.arrivalTime = datetime.datetime(t.year, t.month, t.day,
     334                                               t.hour, t.minute)
     335
     336        return flight
    292337
    293338    # The instructions for the construction
     
    309354        }
    310355
    311     def __init__(self, value):
     356    def __init__(self, value = None, id = None):
    312357        """Construct the booked flight object from the given RPC result
    313358        value."""
    314359        self.status = BookedFlight.STATUS_BOOKED
    315         super(BookedFlight, self).__init__(value, BookedFlight._instructions)
    316         self.departureTime = \
    317           BookedFlight.getDateTime(self.date, self.departureTime)
    318         self.arrivalTime = \
    319           BookedFlight.getDateTime(self.date, self.arrivalTime)
     360        if value is None:
     361            self.id = id
     362        else:
     363            super(BookedFlight, self).__init__(value, BookedFlight._instructions)
     364            self.departureTime = \
     365              BookedFlight.getDateTime(self.date, self.departureTime)
     366            self.arrivalTime = \
     367              BookedFlight.getDateTime(self.date, self.arrivalTime)
     368            if self.arrivalTime<self.departureTime:
     369                self.arrivalTime += datetime.timedelta(days = 1)
     370
     371    def readFromFile(self, f, fleet):
     372        """Read the data of the flight from a file via the given file
     373        object."""
     374        date = None
     375        departureTime = None
     376        arrivalTime = None
     377
     378        line = f.readline()
     379        lineNumber = 0
     380        self.numChildren = 0
     381        self.numInfants = 0
     382        self.numCockpitCrew = 2
     383        self.flightType = const.FLIGHTTYPE_SCHEDULED
     384        while line:
     385            lineNumber += 1
     386            line = line.strip()
     387
     388            hashIndex = line.find("#")
     389            if hashIndex>=0: line = line[:hashIndex]
     390            if line:
     391                equalIndex = line.find("=")
     392                lineOK = equalIndex>0
     393
     394                if lineOK:
     395                    key = line[:equalIndex].strip()
     396                    value = line[equalIndex+1:].strip().replace("\:", ":")
     397
     398                    lineOK = key and value
     399
     400                if lineOK:
     401                    if key=="callsign": self.callsign = value
     402                    elif key=="date": date = value
     403                    elif key=="dep_airport": self.departureICAO = value
     404                    elif key=="dest_airport": self.arrivalICAO = value
     405                    elif key=="planecode": self.aircraftType = \
     406                         BookedFlight._decodeAircraftType(value)
     407                    elif key=="planetype": self.aircraftTypeName = value
     408                    elif key=="tail_nr": self.tailNumber = value
     409                    elif key=="passenger": self.numPassengers = int(value)
     410                    elif key=="child": self.numChildren = int(value)
     411                    elif key=="infant": self.numInfants = int(value)
     412                    elif key=="crew": self.numCabinCrew = int(value) - 2
     413                    elif key=="cabin_crew": self.numCabinCrew = int(value)
     414                    elif key=="cockpit_crew": self.numCockpitCrew = int(value)
     415                    elif key=="bag": self.bagWeight = int(value)
     416                    elif key=="cargo": self.cargoWeight = int(value)
     417                    elif key=="mail": self.mailWeight = int(value)
     418                    elif key=="flight_route": self.route = value
     419                    elif key=="departure_time": departureTime = value
     420                    elif key=="arrival_time": arrivalTime = value
     421                    elif key=="foglalas_id":
     422                        self.id = None if value=="0" else value
     423                    elif key=="flight_type": self.flightType = int(value)
     424                    else: lineOK = False
     425
     426                if not lineOK:
     427                    print("web.BookedFlight.readFromFile: line %d is invalid" % \
     428                          (lineNumber,))
     429
     430            line = f.readline()
     431
     432        if date is not None:
     433            if departureTime is not None:
     434                self.departureTime = BookedFlight.getDateTime(date,
     435                                                              departureTime)
     436            if arrivalTime is not None:
     437                self.arrivalTime = BookedFlight.getDateTime(date,
     438                                                            arrivalTime)
     439
     440        d = dir(self)
     441        for attribute in ["callsign", "departureICAO", "arrivalICAO",
     442                          "aircraftType", "tailNumber",
     443                          "numPassengers", "numCockpitCrew", "numCabinCrew",
     444                          "bagWeight", "cargoWeight", "mailWeight",
     445                          "route", "departureTime", "arrivalTime"]:
     446            if attribute not in d:
     447                raise Exception("Attribute %s could not be read" % (attribute,))
     448
     449        if "aircraftTypeName" not in d:
     450            self.aircraftTypeName = \
     451                BookedFlight.TYPE2TYPECODE[self.aircraftType]
     452
     453        plane = fleet[self.tailNumber]
     454        if plane is None:
     455            self.dow = 0
     456            self.maxPassengers = 0
     457            self.dowNumCabinCrew = 0
     458        else:
     459            self.dow = plane.dow
     460            self.maxPassengers = plane.maxPassengers
     461            self.dowNumCabinCrew = plane.dowNumCabinCrew
     462
     463    def setupFromPIREPData(self, pirepData):
     464        """Setup the booked flight from the given PIREP data."""
     465        bookedFlightData = pirepData["bookedFlight"]
     466
     467        self.callsign = bookedFlightData["callsign"]
     468
     469        date = bookedFlightData["date"]
     470
     471        departureTime = bookedFlightData["departureTime"]
     472        self.departureTime = BookedFlight.getDateTime(date, departureTime)
     473
     474        arrivalTime = bookedFlightData["arrivalTime"]
     475        self.arrivalTime = BookedFlight.getDateTime(date, arrivalTime)
    320476        if self.arrivalTime<self.departureTime:
    321477            self.arrivalTime += datetime.timedelta(days = 1)
     478
     479        self.departureICAO = bookedFlightData["departureICAO"]
     480        self.arrivalICAO = bookedFlightData["arrivalICAO"]
     481
     482        self.aircraftType = \
     483            BookedFlight._decodeAircraftType(bookedFlightData["aircraftType"])
     484        self.tailNumber = bookedFlightData["tailNumber"]
     485        self.numPassengers = int(bookedFlightData["numPassengers"])
     486        self.numChildren = int(bookedFlightData["numChildren"])
     487        self.numInfants = int(bookedFlightData["numInfants"])
     488        self.maxPassengers = int(bookedFlightData["maxPassengers"])
     489        self.numCockpitCrew = int(bookedFlightData["numCockpitCrew"])
     490        self.numCabinCrew = int(bookedFlightData["numCabinCrew"])
     491        self.bagWeight = int(bookedFlightData["bagWeight"])
     492        self.cargoWeight = int(bookedFlightData["cargoWeight"])
     493        self.mailWeight = int(bookedFlightData["mailWeight"])
     494        self.route = bookedFlightData["route"]
     495        self.flightType = BookedFlight._convertFlightType(bookedFlightData["flightType"])
    322496
    323497    def writeIntoFile(self, f):
     
    333507        print("tail_nr=%s" % (self.tailNumber,), file=f)
    334508        print("passenger=%d" % (self.numPassengers,), file=f)
    335         print("crew=%d" % (self.numCrew,), file=f)
     509        print("child=%d" % (self.numChildren,), file=f)
     510        print("infant=%d" % (self.numInfants,), file=f)
     511        print("cockpit_crew=%d" % (self.numCockpitCrew,), file=f)
     512        print("cabin_crew=%d" % (self.numCabinCrew,), file=f)
    336513        print("bag=%d" % (self.bagWeight,), file=f)
    337514        print("cargo=%d" % (self.cargoWeight,), file=f)
     
    345522              (arrivalTime.hour, arrivalTime.minute, arrivalTime.second), file=f)
    346523        print("foglalas_id=%s" % ("0" if self.id is None else self.id,), file=f)
     524        print("flight_type=%d" % (self.flightType,))
    347525
    348526    def __setstate__(self, state):
    349527        """Set the state from the given unpickled dictionary."""
    350528        self.__dict__.update(fixUnpickled(state))
     529
     530    def __repr__(self):
     531        """Get a representation of the flight."""
     532        s = "<Flight: %s-%s, %s, %s-%s," % (self.departureICAO,
     533                                           self.arrivalICAO,
     534                                           self.route,
     535                                           self.departureTime, self.arrivalTime)
     536        s += " %d %s," % (self.aircraftType, self.tailNumber)
     537        s += " pax=%d, crew=%d, bag=%d, cargo=%d, mail=%d" % \
     538             (self.numPassengers, self.numCrew,
     539              self.bagWeight, self.cargoWeight, self.mailWeight)
     540        s += ">"
     541        return s
    351542
    352543#---------------------------------------------------------------------------------------
     
    392583        "status" : lambda value: rpccommon.Plane.str2status(value),
    393584        "gateNumber" : lambda value: value if value else None,
    394         "typeCode": lambda value: BookedFlight._decodeAircraftType(value)
     585        "typeCode": lambda value: BookedFlight._decodeAircraftType(value),
     586        "dow": int,
     587        "dowNumCabinCrew": int,
     588        "maxPassengers": int,
    395589        }
    396590
  • src/mlx/web.py

    r1034 r1044  
    22from . import const
    33from . import util
    4 from .rpc import Registration
     4from .rpc import Registration, BookedFlight
    55from . import rpc
    66from . import rpccommon
     
    5454            return line
    5555
    56 #---------------------------------------------------------------------------------------
    57 
    58 class BookedFlight(object):
    59     """A flight that was booked."""
    60     TYPECODE2TYPE = { "B736"  : const.AIRCRAFT_B736,
    61                       "B737"  : const.AIRCRAFT_B737,
    62                       "B738"  : const.AIRCRAFT_B738,
    63                       "B73H"  : const.AIRCRAFT_B738C,
    64                       "B732"  : const.AIRCRAFT_B732,
    65                       "B733"  : const.AIRCRAFT_B733,
    66                       "B734"  : const.AIRCRAFT_B734,
    67                       "B735"  : const.AIRCRAFT_B735,
    68                       "DH8D"  : const.AIRCRAFT_DH8D,
    69                       "B762"  : const.AIRCRAFT_B762,
    70                       "B763"  : const.AIRCRAFT_B763,
    71                       "CRJ2"  : const.AIRCRAFT_CRJ2,
    72                       "F70"  : const.AIRCRAFT_F70,
    73                       "LI2"  : const.AIRCRAFT_DC3,
    74                       "T134"  : const.AIRCRAFT_T134,
    75                       "T154"  : const.AIRCRAFT_T154,
    76                       "YK40"  : const.AIRCRAFT_YK40,
    77                       "B462"  : const.AIRCRAFT_B462,
    78                       "IL62"  : const.AIRCRAFT_IL62 }
    79 
    80     TYPE2TYPECODE = { const.AIRCRAFT_B736  : "B736",
    81                       const.AIRCRAFT_B737  : "B737",
    82                       const.AIRCRAFT_B738  : "B738",
    83                       const.AIRCRAFT_B738C : "B73H",
    84                       const.AIRCRAFT_B732  : "B732",
    85                       const.AIRCRAFT_B733  : "B733",
    86                       const.AIRCRAFT_B734  : "B734",
    87                       const.AIRCRAFT_B735  : "B735",
    88                       const.AIRCRAFT_DH8D  : "DH8D",
    89                       const.AIRCRAFT_B762  : "B762",
    90                       const.AIRCRAFT_B763  : "B763",
    91                       const.AIRCRAFT_CRJ2  : "CRJ2",
    92                       const.AIRCRAFT_F70   : "F70",
    93                       const.AIRCRAFT_DC3   : "LI2",
    94                       const.AIRCRAFT_T134  : "T134",
    95                       const.AIRCRAFT_T154  : "T155",
    96                       const.AIRCRAFT_YK40  : "YK40",
    97                       const.AIRCRAFT_B462  : "B462",
    98                       const.AIRCRAFT_IL62  : "IL62" }
    99 
    100     checkFlightTypes = [ const.AIRCRAFT_B736, const.AIRCRAFT_B737,
    101                          const.AIRCRAFT_B738, const.AIRCRAFT_DH8D ]
    102 
    103     STATUS_BOOKED = 1
    104 
    105     STATUS_REPORTED = 2
    106 
    107     STATUS_ACCEPTED = 3
    108 
    109     STATUS_REJECTED = 4
    110 
    111     @staticmethod
    112     def getDateTime(date, time):
    113         """Get a datetime object from the given textual date and time."""
    114         return datetime.datetime.strptime(date + " " + time,
    115                                           "%Y-%m-%d %H:%M:%S")
    116 
    117     @staticmethod
    118     def forCheckFlight(aircraftType):
    119         """Create a booked flight for a check flight with the given aircraft
    120         type."""
    121         flight = BookedFlight()
    122 
    123         flight.departureICAO = "LHBP"
    124         flight.arrivalICAO = "LHBP"
    125 
    126         flight.aircraftType = aircraftType
    127         flight.aircraftTypeName = BookedFlight.TYPE2TYPECODE[aircraftType]
    128 
    129         # FIXME: perhaps find one for the type
    130         flight.tailNumber = "HA-CHK"
    131         flight.callsign = "HA-CHK"
    132 
    133         flight.numPassengers = 0
    134         flight.numCrew = 2
    135         flight.bagWeight = 0
    136         flight.cargoWeight = 0
    137         flight.mailWeight = 0
    138         flight.route = "DCT"
    139 
    140         t = datetime.datetime.now() + datetime.timedelta(minutes = 20)
    141         flight.departureTime = datetime.datetime(t.year, t.month, t.day,
    142                                                  t.hour, t.minute)
    143         t = flight.departureTime + datetime.timedelta(minutes = 30)
    144         flight.arrivalTime = datetime.datetime(t.year, t.month, t.day,
    145                                                t.hour, t.minute)
    146 
    147         return flight
    148 
    149     @staticmethod
    150     def _convertFlightType(ft):
    151         """Convert the in-database flight-type to one of our constants."""
    152         ft = int(ft)
    153         if ft==0:
    154             return const.FLIGHTTYPE_SCHEDULED
    155         elif ft==1:
    156             return const.FLIGHTTYPE_VIP
    157         elif ft==2:
    158             return const.FLIGHTTYPE_CHARTER
    159         else:
    160             return const.FLIGHTTYPE_SCHEDULED
    161 
    162     def __init__(self, id = None):
    163         """Construct a booked flight with the given ID."""
    164         self.id = id
    165 
    166     @property
    167     def status(self):
    168         """Get the status of the flight.
    169 
    170         For web-based flights this is always STATUS_BOOKED."""
    171         return BookedFlight.STATUS_BOOKED
    172 
    173     def readFromWeb(self, f):
    174         """Read the data of the flight from the web via the given file
    175         object."""
    176         self.callsign = readline(f)
    177 
    178         date = readline(f)
    179         print("web.BookedFlight.readFromWeb: date:", date)
    180         if date=="0000-00-00": date = "0001-01-01"
    181 
    182         self.departureICAO = readline(f)
    183         self.arrivalICAO = readline(f)
    184 
    185         self._readAircraftType(f)
    186         self.tailNumber = readline(f)
    187         self.numPassengers = int(readline(f))
    188         self.numCrew = int(readline(f))
    189         self.bagWeight = int(readline(f))
    190         self.cargoWeight = int(readline(f))
    191         self.mailWeight = int(readline(f))
    192         self.route = readline(f)
    193 
    194         departureTime = readline(f)
    195         self.departureTime = BookedFlight.getDateTime(date, departureTime)
    196 
    197         arrivalTime = readline(f)
    198         self.arrivalTime = BookedFlight.getDateTime(date, arrivalTime)
    199         if self.arrivalTime<self.departureTime:
    200             self.arrivalTime += datetime.timedelta(days = 1)
    201 
    202         if not readline(f)==".NEXT.":
    203             raise Exception("Invalid line in flight data")
    204 
    205     def readFromFile(self, f):
    206         """Read the data of the flight from a file via the given file
    207         object."""
    208         date = None
    209         departureTime = None
    210         arrivalTime = None
    211 
    212         line = f.readline()
    213         lineNumber = 0
    214         while line:
    215             lineNumber += 1
    216             line = line.strip()
    217 
    218             hashIndex = line.find("#")
    219             if hashIndex>=0: line = line[:hashIndex]
    220             if line:
    221                 equalIndex = line.find("=")
    222                 lineOK = equalIndex>0
    223 
    224                 if lineOK:
    225                     key = line[:equalIndex].strip()
    226                     value = line[equalIndex+1:].strip().replace("\:", ":")
    227 
    228                     lineOK = key and value
    229 
    230                 if lineOK:
    231                     if key=="callsign": self.callsign = value
    232                     elif key=="date": date = value
    233                     elif key=="dep_airport": self.departureICAO = value
    234                     elif key=="dest_airport": self.arrivalICAO = value
    235                     elif key=="planecode": self.aircraftType = \
    236                          self._decodeAircraftType(value)
    237                     elif key=="planetype": self.aircraftTypeName = value
    238                     elif key=="tail_nr": self.tailNumber = value
    239                     elif key=="passenger": self.numPassengers = int(value)
    240                     elif key=="crew": self.numCrew = int(value)
    241                     elif key=="bag": self.bagWeight = int(value)
    242                     elif key=="cargo": self.cargoWeight = int(value)
    243                     elif key=="mail": self.mailWeight = int(value)
    244                     elif key=="flight_route": self.route = value
    245                     elif key=="departure_time": departureTime = value
    246                     elif key=="arrival_time": arrivalTime = value
    247                     elif key=="foglalas_id":
    248                         self.id = None if value=="0" else value
    249                     else: lineOK = False
    250 
    251                 if not lineOK:
    252                     print("web.BookedFlight.readFromFile: line %d is invalid" % \
    253                           (lineNumber,))
    254 
    255             line = f.readline()
    256 
    257         if date is not None:
    258             if departureTime is not None:
    259                 self.departureTime = BookedFlight.getDateTime(date,
    260                                                               departureTime)
    261             if arrivalTime is not None:
    262                 self.arrivalTime = BookedFlight.getDateTime(date,
    263                                                             arrivalTime)
    264 
    265         d = dir(self)
    266         for attribute in ["callsign", "departureICAO", "arrivalICAO",
    267                           "aircraftType", "tailNumber",
    268                           "numPassengers", "numCrew",
    269                           "bagWeight", "cargoWeight", "mailWeight",
    270                           "route", "departureTime", "arrivalTime"]:
    271             if attribute not in d:
    272                 raise Exception("Attribute %s could not be read" % (attribute,))
    273 
    274         if "aircraftTypeName" not in d:
    275             self.aircraftTypeName = \
    276                 BookedFlight.TYPE2TYPECODE[self.aircraftType]
    277 
    278     def setupFromPIREPData(self, pirepData):
    279         """Setup the booked flight from the given PIREP data."""
    280         bookedFlightData = pirepData["bookedFlight"]
    281 
    282         self.callsign = bookedFlightData["callsign"]
    283 
    284         date = bookedFlightData["date"]
    285 
    286         departureTime = bookedFlightData["departureTime"]
    287         self.departureTime = BookedFlight.getDateTime(date, departureTime)
    288 
    289         arrivalTime = bookedFlightData["arrivalTime"]
    290         self.arrivalTime = BookedFlight.getDateTime(date, arrivalTime)
    291         if self.arrivalTime<self.departureTime:
    292             self.arrivalTime += datetime.timedelta(days = 1)
    293 
    294         self.departureICAO = bookedFlightData["departureICAO"]
    295         self.arrivalICAO = bookedFlightData["arrivalICAO"]
    296 
    297         self.aircraftType = \
    298           self._decodeAircraftType(bookedFlightData["aircraftType"])
    299         self.tailNumber = bookedFlightData["tailNumber"]
    300         self.numPassengers = int(bookedFlightData["numPassengers"])
    301         self.numChildren = int(bookedFlightData["numChildren"])
    302         self.numInfants = int(bookedFlightData["numInfants"])
    303         self.maxPassengers = int(bookedFlightData["maxPassengers"])
    304         self.numCockpitCrew = int(bookedFlightData["numCockpitCrew"])
    305         self.numCabinCrew = int(bookedFlightData["numCabinCrew"])
    306         self.bagWeight = int(bookedFlightData["bagWeight"])
    307         self.cargoWeight = int(bookedFlightData["cargoWeight"])
    308         self.mailWeight = int(bookedFlightData["mailWeight"])
    309         self.route = bookedFlightData["route"]
    310         self.flightType = BookedFlight._convertFlightType(bookedFlightData["flightType"])
    311 
    312     def writeIntoFile(self, f):
    313         """Write the flight into a file."""
    314         print("callsign=%s" % (self.callsign,), file=f)
    315         date = self.departureTime.date()
    316         print("date=%04d-%02d-%0d" % (date.year, date.month, date.day), file=f)
    317         print("dep_airport=%s" % (self.departureICAO,), file=f)
    318         print("dest_airport=%s" % (self.arrivalICAO,), file=f)
    319         print("planecode=%s" % \
    320               (BookedFlight.TYPE2TYPECODE[self.aircraftType],), file=f)
    321         print("planetype=%s" % (self.aircraftTypeName,), file=f)
    322         print("tail_nr=%s" % (self.tailNumber,), file=f)
    323         print("passenger=%d" % (self.numPassengers,), file=f)
    324         print("crew=%d" % (self.numCrew,), file=f)
    325         print("bag=%d" % (self.bagWeight,), file=f)
    326         print("cargo=%d" % (self.cargoWeight,), file=f)
    327         print("mail=%d" % (self.mailWeight,), file=f)
    328         print("flight_route=%s" % (self.route,), file=f)
    329         departureTime = self.departureTime
    330         print("departure_time=%02d\\:%02d\\:%02d" % \
    331               (departureTime.hour, departureTime.minute, departureTime.second), file=f)
    332         arrivalTime = self.arrivalTime
    333         print("arrival_time=%02d\\:%02d\\:%02d" % \
    334               (arrivalTime.hour, arrivalTime.minute, arrivalTime.second), file=f)
    335         print("foglalas_id=%s" % ("0" if self.id is None else self.id,), file=f)
    336 
    337     def _readAircraftType(self, f):
    338         """Read the aircraft type from the given file."""
    339         line = readline(f)
    340         typeCode = line[:3]
    341         self.aircraftType = self._decodeAircraftType(typeCode)
    342         self.aircraftTypeName = line[3:]
    343 
    344     def _decodeAircraftType(self, typeCode):
    345         """Decode the aircraft type from the given typeCode."""
    346         if typeCode in self.TYPECODE2TYPE:
    347             return self.TYPECODE2TYPE[typeCode]
    348         else:
    349             raise Exception("Invalid aircraft type code: '" + typeCode + "'")
    350 
    351     def __repr__(self):
    352         """Get a representation of the flight."""
    353         s = "<Flight: %s-%s, %s, %s-%s," % (self.departureICAO,
    354                                            self.arrivalICAO,
    355                                            self.route,
    356                                            self.departureTime, self.arrivalTime)
    357         s += " %d %s," % (self.aircraftType, self.tailNumber)
    358         s += " pax=%d, crew=%d, bag=%d, cargo=%d, mail=%d" % \
    359              (self.numPassengers, self.numCrew,
    360               self.bagWeight, self.cargoWeight, self.mailWeight)
    361         s += ">"
    362         return s
    363 
    36456#------------------------------------------------------------------------------
    36557
     
    769461#------------------------------------------------------------------------------
    770462
    771 class Login(Request):
    772     """A login request."""
    773     iso88592decoder = codecs.getdecoder("iso-8859-2")
    774 
    775     def __init__(self, callback, pilotID, password):
    776         """Construct the login request with the given pilot ID and
    777         password."""
    778         super(Login, self).__init__(callback)
    779 
    780         self._pilotID = pilotID
    781         self._password = password
    782 
    783     def run(self):
    784         """Perform the login request."""
    785         md5 = hashlib.md5()
    786         md5.update(self._pilotID)
    787         pilotID = md5.hexdigest()
    788 
    789         md5 = hashlib.md5()
    790         md5.update(self._password)
    791         password = md5.hexdigest()
    792 
    793         url = MAVA_BASE_URL + "/leker2.php?pid=%s&psw=%s" %  (pilotID, password)
    794 
    795         result = Result()
    796 
    797         f = urllib.request.urlopen(url, timeout = 10.0)
    798 
    799         status = readline(f)
    800         result.loggedIn = status == ".OK."
    801 
    802         if result.loggedIn:
    803             result.pilotID = self._pilotID
    804             result.password = self._password
    805             result.rank = "FO"
    806             result.flights = []
    807 
    808             result.pilotName = self.iso88592decoder(readline(f))[0]
    809             result.exams = readline(f)
    810 
    811             while True:
    812                 line = readline(f)
    813                 if not line or line == "#ENDPIREP": break
    814 
    815                 flight = BookedFlight(line)
    816                 flight.readFromWeb(f)
    817                 result.flights.append(flight)
    818 
    819             result.flights.sort(key = lambda flight: flight.departureTime)
    820 
    821         f.close()
    822 
    823         return result
    824 
    825 #------------------------------------------------------------------------------
    826 
    827463class LoginRPC(RPCRequest):
    828464    """An RPC-based login request."""
     
    839475            result.types = loginResult[2]
    840476            result.password = password
     477            result.fleet = client.getFleet()
    841478            flights = client.getFlights()
    842479            result.flights = flights[0]
     
    893530#------------------------------------------------------------------------------
    894531
    895 class GetFleet(Request):
    896     """Request to get the fleet from the website."""
    897 
    898     def __init__(self, callback):
    899         """Construct the fleet request."""
    900         super(GetFleet, self).__init__(callback)
    901 
    902     def run(self):
    903         """Perform the login request."""
    904         url = MAVA_BASE_URL + "/onlinegates_get.php"
    905 
    906         f = urllib.request.urlopen(url, timeout = 10.0)
    907         result = Result()
    908         result.fleet = Fleet(f)
    909         f.close()
    910 
    911         return result
    912 
    913 #------------------------------------------------------------------------------
    914 
    915532class GetFleetRPC(RPCRequest):
    916533    """Request to get the fleet from the website using RPC."""
     
    924541
    925542        result.fleet = self._client.getFleet()
    926 
    927         return result
    928 
    929 #------------------------------------------------------------------------------
    930 
    931 class UpdatePlane(Request):
    932     """Update the status of one of the planes in the fleet."""
    933     def __init__(self, callback, tailNumber, status, gateNumber = None):
    934         """Construct the request."""
    935         super(UpdatePlane, self).__init__(callback)
    936         self._tailNumber = tailNumber
    937         self._status = status
    938         self._gateNumber = gateNumber
    939 
    940     def run(self):
    941         """Perform the plane update."""
    942         url = MAVA_BASE_URL + "/onlinegates_set.php"
    943 
    944         status = Plane.status2str(self._status)
    945 
    946         gateNumber = self._gateNumber if self._gateNumber else ""
    947 
    948         data = urllib.parse.urlencode([("lajstrom", self._tailNumber),
    949                                  ("status", status),
    950                                  ("kapu", gateNumber)])
    951 
    952         f = urllib.request.urlopen(url, data, timeout = 10.0)
    953         line = readline(f)
    954 
    955         result = Result()
    956         result.success = line == "OK"
    957543
    958544        return result
     
    1095681#------------------------------------------------------------------------------
    1096682
    1097 class SendPIREP(Request):
    1098     """A request to send a PIREP to the MAVA website."""
    1099     _latin2Encoder = codecs.getencoder("iso-8859-2")
    1100 
    1101     def __init__(self, callback, pirep):
    1102         """Construct the sending of the PIREP."""
    1103         super(SendPIREP, self).__init__(callback)
    1104         self._pirep = pirep
    1105 
    1106     def run(self):
    1107         """Perform the sending of the PIREP."""
    1108         url = MAVA_BASE_URL + "/malevacars.php"
    1109 
    1110         pirep = self._pirep
    1111 
    1112         data = {}
    1113         data["acarsdata"] = SendPIREP._latin2Encoder(pirep.getACARSText())[0]
    1114 
    1115         bookedFlight = pirep.bookedFlight
    1116         data["foglalas_id"] = bookedFlight.id
    1117         data["repdate"] = bookedFlight.departureTime.date().strftime("%Y-%m-%d")
    1118         data["fltnum"] = bookedFlight.callsign
    1119         data["depap"] = bookedFlight.departureICAO
    1120         data["arrap"] = bookedFlight.arrivalICAO
    1121         data["pass"] = str(pirep.numPassengers)
    1122         data["crew"] = str(pirep.numCrew)
    1123         data["cargo"] = str(pirep.cargoWeight)
    1124         data["bag"] = str(pirep.bagWeight)
    1125         data["mail"] = str(pirep.mailWeight)
    1126 
    1127         data["flttype"] = pirep.flightTypeText
    1128         data["onoff"] = "1" if pirep.online else "0"
    1129         data["bt_dep"] = util.getTimestampString(pirep.blockTimeStart)
    1130         data["bt_arr"] = util.getTimestampString(pirep.blockTimeEnd)
    1131         data["bt_dur"] = util.getTimeIntervalString(pirep.blockTimeEnd -
    1132                                                     pirep.blockTimeStart)
    1133         data["ft_dep"] = util.getTimestampString(pirep.flightTimeStart)
    1134         data["ft_arr"] = util.getTimestampString(pirep.flightTimeEnd)
    1135         data["ft_dur"] = util.getTimeIntervalString(pirep.flightTimeEnd -
    1136                                                     pirep.flightTimeStart)
    1137         data["timecomm"] = pirep.getTimeComment()
    1138         data["fuel"] = "%.2f" % (pirep.fuelUsed,)
    1139         data["dep_rwy"] = pirep.departureRunway
    1140         data["arr_rwy"] = pirep.arrivalRunway
    1141         data["wea_dep"] = pirep.departureMETAR
    1142         data["wea_arr"] = pirep.arrivalMETAR
    1143         data["alt"] = "FL%.0f" % (pirep.filedCruiseAltitude/100.0,)
    1144         if pirep.filedCruiseAltitude!=pirep.cruiseAltitude:
    1145             data["mod_alt"] = "FL%.0f" % (pirep.cruiseAltitude/100.0,)
    1146         else:
    1147             data["mod_alt"] = ""
    1148         data["sid"] = pirep.sid
    1149         data["navroute"] = pirep.route
    1150         data["star"] = pirep.getSTAR()
    1151         data["aprtype"] = pirep.approachType
    1152         data["diff"] = "2"
    1153         data["comment"] = SendPIREP._latin2Encoder(pirep.comments)[0]
    1154         data["flightdefect"] = SendPIREP._latin2Encoder(pirep.flightDefects)[0]
    1155         data["kritika"] = pirep.getRatingText()
    1156         data["flightrating"] = "%.1f" % (max(0.0, pirep.rating),)
    1157         data["distance"] = "%.3f" % (pirep.flownDistance,)
    1158         data["insdate"] = datetime.date.today().strftime("%Y-%m-%d")
    1159 
    1160         postData = urllib.parse.urlencode(data)
    1161         f = urllib.request.urlopen(url, postData, timeout = 10.0)
    1162         try:
    1163             result = Result()
    1164             line = f.readline().strip()
    1165             print("PIREP result from website:", line)
    1166             result.success = line=="OK"
    1167             result.alreadyFlown = line=="MARVOLT"
    1168             result.notAvailable = line=="NOMORE"
    1169         finally:
    1170             f.close()
    1171 
    1172         return result
    1173 #------------------------------------------------------------------------------
    1174 
    1175683class SendPIREPRPC(RPCRequest):
    1176684    """A request to send a PIREP to the MAVA website via the RPC interface."""
     
    1192700        result.alreadyFlown = resultCode==rpc.Client.RESULT_FLIGHT_ALREADY_REPORTED
    1193701        result.notAvailable = resultCode==rpc.Client.RESULT_FLIGHT_NOT_EXISTS
    1194 
    1195         return result
    1196 
    1197 #------------------------------------------------------------------------------
    1198 
    1199 class SendACARS(Request):
    1200     """A request to send an ACARS to the MAVA website."""
    1201     _latin2Encoder = codecs.getencoder("iso-8859-2")
    1202 
    1203     def __init__(self, callback, acars):
    1204         """Construct the request for the given PIREP."""
    1205         super(SendACARS, self).__init__(callback)
    1206         self._acars = acars
    1207 
    1208     def run(self):
    1209         """Perform the sending of the ACARS."""
    1210         print("Sending the online ACARS")
    1211 
    1212         url = MAVA_BASE_URL  + "/acars2/acarsonline.php"
    1213 
    1214         acars = self._acars
    1215         bookedFlight = acars.bookedFlight
    1216 
    1217         data = {}
    1218         data["pid"] = acars.pid
    1219         data["pilot"] = SendACARS._latin2Encoder(acars.pilotName)[0]
    1220 
    1221         data["pass"] = str(bookedFlight.numPassengers)
    1222         data["callsign"] = bookedFlight.callsign
    1223         data["airplane"] = bookedFlight.aircraftTypeName
    1224         data["from"] = bookedFlight.departureICAO
    1225         data["to"] = bookedFlight.arrivalICAO
    1226         data["lajstrom"] = bookedFlight.tailNumber
    1227 
    1228         data["block_time"] = acars.getBlockTimeText()
    1229         data["longitude"] = str(acars.state.longitude)
    1230         data["latitude"] = str(acars.state.latitude)
    1231         data["altitude"] = str(acars.state.altitude)
    1232         data["speed"] = str(acars.state.groundSpeed)
    1233 
    1234         data["event"] = acars.getEventText()
    1235 
    1236         f = urllib.request.urlopen(url, urllib.parse.urlencode(data), timeout = 10.0)
    1237         try:
    1238             result = Result()
    1239         finally:
    1240             f.close()
    1241702
    1242703        return result
     
    1320781        print("pirepData:", pirepData)
    1321782
    1322         bookedFlight = BookedFlight(self._flightID)
     783        bookedFlight = BookedFlight(id = self._flightID)
    1323784        bookedFlight.setupFromPIREPData(pirepData)
    1324785
     
    1437898    def login(self, callback, pilotID, password):
    1438899        """Enqueue a login request."""
    1439         request = \
    1440           LoginRPC(self._rpcClient, callback, pilotID, password) \
    1441           if self._config.useRPC else Login(callback, pilotID, password)
     900        request = LoginRPC(self._rpcClient, callback, pilotID, password)
    1442901
    1443902        self._addRequest(request)
     
    1449908    def getFleet(self, callback):
    1450909        """Enqueue a fleet retrieval request."""
    1451         request = \
    1452           GetFleetRPC(self._rpcClient, callback,) if self._config.useRPC \
    1453           else GetFleet(callback)
     910        request = GetFleetRPC(self._rpcClient, callback,)
    1454911        self._addRequest(request)
    1455912
    1456913    def updatePlane(self, callback, tailNumber, status, gateNumber = None):
    1457914        """Update the status of the given plane."""
    1458         request = \
    1459           UpdatePlaneRPC(self._rpcClient, callback,
    1460                          tailNumber, status, gateNumber) \
    1461           if self._config.useRPC \
    1462           else UpdatePlane(callback, tailNumber, status, gateNumber)
     915        request = UpdatePlaneRPC(self._rpcClient, callback,
     916                                 tailNumber, status, gateNumber)
    1463917        self._addRequest(request)
    1464918
     
    1473927    def sendPIREP(self, callback, pirep, update = False):
    1474928        """Send the given PIREP."""
    1475         request = \
    1476           SendPIREPRPC(self._rpcClient, callback, pirep, update) \
    1477           if self._config.useRPC else SendPIREP(callback, pirep)
     929        request = SendPIREPRPC(self._rpcClient, callback, pirep, update)
    1478930        self._addRequest(request)
    1479931
    1480932    def sendACARS(self, callback, acars):
    1481933        """Send the given ACARS"""
    1482         request = \
    1483           SendACARSRPC(self._rpcClient, callback, acars) if self._config.useRPC \
    1484           else SendACARS(callback, acars)
     934        request = SendACARSRPC(self._rpcClient, callback, acars)
    1485935        self._addRequest(request)
    1486936
Note: See TracChangeset for help on using the changeset viewer.