source: src/mlx/rpccommon.py

python3
Last change on this file was 1156:8d9eb69a4c39, checked in by István Váradi <ivaradi@…>, 5 days ago

Jetways are not available for aircraft with stairs (re #386).

File size: 4.4 KB
Line 
1# Some common objects for RPC communication
2
3#------------------------------------------------------------------------------
4
5from . import const
6from .gates import lhbpGates
7
8#------------------------------------------------------------------------------
9
10class Plane(object):
11 """Information about an airplane in the fleet."""
12 @staticmethod
13 def str2status(letter):
14 """Convert the given letter into a plane status."""
15 return const.PLANE_HOME if letter=="H" else \
16 const.PLANE_AWAY if letter=="A" else \
17 const.PLANE_PARKING if letter=="P" else \
18 const.PLANE_UNKNOWN
19
20 @staticmethod
21 def status2str(status):
22 """Convert the given status into the corresponding letter code."""
23 return "H" if status==const.PLANE_HOME else \
24 "A" if status==const.PLANE_AWAY else \
25 "P" if status==const.PLANE_PARKING else ""
26
27 @property
28 def hasStairs(self):
29 """Indicate if the plane has its own stairs."""
30 return self.aircraftType in [const.AIRCRAFT_YK40,
31 const.AIRCRAFT_CRJ2,
32 const.AIRCRAFT_F70,
33 const.AIRCRAFT_DH8D]
34
35 def _setStatus(self, letter):
36 """Set the status from the given letter."""
37 self.status = Plane.str2status(letter)
38
39 def __repr__(self):
40 """Get the representation of the plane object."""
41 s = "<Plane: %s %s" % (self.tailNumber,
42 "home" if self.status==const.PLANE_HOME else \
43 "away" if self.status==const.PLANE_AWAY else \
44 "parking" if self.status==const.PLANE_PARKING \
45 else "unknown")
46 if self.gateNumber is not None:
47 s += " (gate " + self.gateNumber + ")"
48 s += ">"
49 return s
50
51#------------------------------------------------------------------------------
52
53class Fleet(object):
54 """Information about the whole fleet."""
55 def __init__(self):
56 """Construct the fleet information by reading the given file object."""
57 self._planes = {}
58
59 def isGateConflicting(self, plane):
60 """Check if the gate of the given plane conflicts with another plane's
61 position."""
62 gate = lhbpGates.find(plane.gateNumber)
63 for p in self._planes.values():
64 if p.tailNumber!=plane.tailNumber and \
65 p.status==const.PLANE_HOME:
66 if p.gateNumber==plane.gateNumber:
67 return True
68 if gate is not None and not gate.isAvailable(plane,
69 lhbpGates,
70 [p.gateNumber]):
71 return True
72
73 return False
74
75 def getOccupiedGateNumbers(self):
76 """Get a set containing the numbers of the gates occupied by planes."""
77 gateNumbers = set()
78 for p in self._planes.values():
79 if p.status==const.PLANE_HOME and p.gateNumber:
80 gateNumbers.add(p.gateNumber)
81 return gateNumbers
82
83 def iterAvailableLHBPGates(self, tailNumber):
84 """Iterate over the available gates at LHBP."""
85 occupiedGateNumbers = self.getOccupiedGateNumbers()
86 plane = self.__getitem__(tailNumber)
87 for gate in lhbpGates.gates:
88 if gate.isAvailable(plane, lhbpGates, occupiedGateNumbers):
89 yield gate
90
91 def updatePlane(self, tailNumber, status, gateNumber = None):
92 """Update the status of the given plane."""
93 if tailNumber in self._planes:
94 plane = self._planes[tailNumber]
95 plane.status = status
96 plane.gateNumber = gateNumber
97
98 def _addPlane(self, plane):
99 """Add the given plane to the fleet."""
100 self._planes[plane.tailNumber] = plane
101
102 def __iter__(self):
103 """Get an iterator over the planes."""
104 for plane in self._planes.values():
105 yield plane
106
107 def __getitem__(self, tailNumber):
108 """Get the plane with the given tail number.
109
110 If the plane is not in the fleet, None is returned."""
111 return self._planes[tailNumber] if tailNumber in self._planes else None
112
113 def __repr__(self):
114 """Get the representation of the fleet object."""
115 return self._planes.__repr__()
Note: See TracBrowser for help on using the repository browser.