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

RPC is the only communication method (re #357)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.