Ignore:
Timestamp:
06/08/17 18:36:07 (7 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
Message:

The timetable can be queried, displayed and filtered (re #304)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/rpc.py

    r854 r858  
    4747class Reply(RPCObject):
    4848    """The generic reply structure."""
     49
     50#---------------------------------------------------------------------------------------
     51
     52class ScheduledFlight(RPCObject):
     53    """A scheduled flight in the time table."""
     54    # The instructions for the construction
     55    # Type: normal flight
     56    TYPE_NORMAL = 0
     57
     58    # Type: VIP flight
     59    TYPE_VIP = 1
     60
     61    _instructions = {
     62        "id" : int,
     63        "pairID": int,
     64        "typeCode": lambda value: BookedFlight._decodeAircraftType(value),
     65        "departureTime": lambda value: ScheduledFlight._decodeTime(value),
     66        "arrivalTime": lambda value: ScheduledFlight._decodeTime(value),
     67        "duration": lambda value: ScheduledFlight._decodeDuration(value),
     68        "type": int,
     69        "spec": int
     70        }
     71
     72    @staticmethod
     73    def _decodeTime(value):
     74        """Decode the given value as a time value."""
     75        return datetime.datetime.strptime(value, "%H:%M:%S").time()
     76
     77    @staticmethod
     78    def _decodeDuration(value):
     79        """Decode the given value as a duration.
     80
     81        A number of seconds will be returned."""
     82        t = datetime.datetime.strptime(value, "%H:%M:%S")
     83        return (t.hour*60 + t.minute) * 60 + t.second
     84
     85    def __init__(self, value):
     86        """Construct the scheduled flight object from the given JSON value."""
     87        super(ScheduledFlight, self).__init__(value,
     88                                              ScheduledFlight._instructions)
     89        self.aircraftType = self.typeCode
     90        del self.typeCode
     91
     92    def __repr__(self):
     93        return "ScheduledFlight<%d, %d, %s, %s (%s) - %s (%s) -> %d, %d>" % \
     94          (self.id, self.pairID, BookedFlight.TYPE2TYPECODE[self.aircraftType],
     95           self.departureICAO, str(self.departureTime),
     96           self.arrivalICAO, str(self.arrivalTime),
     97           self.duration, self.spec)
     98
     99#---------------------------------------------------------------------------------------
     100
     101class ScheduledFlightPair(object):
     102    """A pair of scheduled flights.
     103
     104    Occasionally, one of the flights may be missing."""
     105    @staticmethod
     106    def scheduledFlights2Pairs(scheduledFlights):
     107        """Convert the given list of scheduled flights into a list of flight
     108        pairs."""
     109        flights = {}
     110        for flight in scheduledFlights:
     111            flights[flight.id] = flight
     112
     113        flightPairs = []
     114
     115        while flights:
     116            (id, flight) = flights.popitem()
     117            pairID = flight.pairID
     118            if pairID in flights:
     119                pairFlight = flights[pairID]
     120                if flight.departureICAO=="LHBP" or \
     121                  (pairFlight.departureICAO!="LHBP" and id<pairID):
     122                    flightPairs.append(ScheduledFlightPair(flight, pairFlight))
     123                else:
     124                    flightPairs.append(ScheduledFlightPair(pairFlight, flight))
     125                del flights[pairID]
     126            else:
     127                flightPairs.append(ScheduledFlightPair(flight))
     128
     129        return flightPairs
     130
     131    def __init__(self, flight0, flight1 = None):
     132        """Construct the pair with the given flights."""
     133        self.flight0 = flight0
     134        self.flight1 = flight1
    49135
    50136#---------------------------------------------------------------------------------------
     
    73159
    74160    # FIXME: copied from web.BookedFlight
     161    TYPE2TYPECODE = { const.AIRCRAFT_B736  : "736",
     162                      const.AIRCRAFT_B737  : "73G",
     163                      const.AIRCRAFT_B738  : "738",
     164                      const.AIRCRAFT_B738C : "73H",
     165                      const.AIRCRAFT_B732  : "732",
     166                      const.AIRCRAFT_B733  : "733",
     167                      const.AIRCRAFT_B734  : "734",
     168                      const.AIRCRAFT_B735  : "735",
     169                      const.AIRCRAFT_DH8D  : "DH4",
     170                      const.AIRCRAFT_B762  : "762",
     171                      const.AIRCRAFT_B763  : "763",
     172                      const.AIRCRAFT_CRJ2  : "CR2",
     173                      const.AIRCRAFT_F70   : "F70",
     174                      const.AIRCRAFT_DC3   : "LI2",
     175                      const.AIRCRAFT_T134  : "TU3",
     176                      const.AIRCRAFT_T154  : "TU5",
     177                      const.AIRCRAFT_YK40  : "YK4",
     178                      const.AIRCRAFT_B462  : "146" }
     179
     180    # FIXME: copied from web.BookedFlight
    75181    @staticmethod
    76182    def _decodeAircraftType(typeCode):
     
    311417            self._loginCount += 1
    312418            self._sessionID = reply.value["sessionID"]
    313             return (reply.value["name"], reply.value["rank"])
     419
     420            types = [BookedFlight.TYPECODE2TYPE[typeCode]
     421                     for typeCode in reply.value["typeCodes"]]
     422
     423            return (reply.value["name"], reply.value["rank"], types)
    314424        else:
    315425            return None
     
    404514        self._performCall(lambda sessionID:
    405515                          self._server.deleteFlights(sessionID, flightIDs))
     516
     517    def getTimetable(self, date, types = None):
     518        """Get the time table for the given date restricted to the given list
     519        of type codes, if any."""
     520        typeCodes = None if types is None else \
     521            [BookedFlight.TYPE2TYPECODE[type] for type in types]
     522
     523        values = self._performCall(lambda sessionID:
     524                                   self._server.getTimetable(sessionID,
     525                                                             date.strftime("%Y-%m-%d"),
     526                                                             date.weekday() + 1,
     527                                                             typeCodes))
     528        return ScheduledFlightPair.scheduledFlights2Pairs([ScheduledFlight(value)
     529                                                          for value in values])
    406530
    407531    def _performCall(self, callFn, acceptResults = []):
Note: See TracChangeset for help on using the changeset viewer.