1 | # Some common objects for RPC communication
|
---|
2 |
|
---|
3 | #------------------------------------------------------------------------------
|
---|
4 |
|
---|
5 | from . import const
|
---|
6 | from .gates import lhbpGates
|
---|
7 |
|
---|
8 | #------------------------------------------------------------------------------
|
---|
9 |
|
---|
10 | class 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 | def _setStatus(self, letter):
|
---|
28 | """Set the status from the given letter."""
|
---|
29 | self.status = Plane.str2status(letter)
|
---|
30 |
|
---|
31 | def __repr__(self):
|
---|
32 | """Get the representation of the plane object."""
|
---|
33 | s = "<Plane: %s %s" % (self.tailNumber,
|
---|
34 | "home" if self.status==const.PLANE_HOME else \
|
---|
35 | "away" if self.status==const.PLANE_AWAY else \
|
---|
36 | "parking" if self.status==const.PLANE_PARKING \
|
---|
37 | else "unknown")
|
---|
38 | if self.gateNumber is not None:
|
---|
39 | s += " (gate " + self.gateNumber + ")"
|
---|
40 | s += ">"
|
---|
41 | return s
|
---|
42 |
|
---|
43 | #------------------------------------------------------------------------------
|
---|
44 |
|
---|
45 | class Fleet(object):
|
---|
46 | """Information about the whole fleet."""
|
---|
47 | def __init__(self):
|
---|
48 | """Construct the fleet information by reading the given file object."""
|
---|
49 | self._planes = {}
|
---|
50 |
|
---|
51 | def isGateConflicting(self, plane):
|
---|
52 | """Check if the gate of the given plane conflicts with another plane's
|
---|
53 | position."""
|
---|
54 | gate = lhbpGates.find(plane.gateNumber)
|
---|
55 | for p in self._planes.values():
|
---|
56 | if p.tailNumber!=plane.tailNumber and \
|
---|
57 | p.status==const.PLANE_HOME:
|
---|
58 | if p.gateNumber==plane.gateNumber:
|
---|
59 | return True
|
---|
60 | if gate is not None and not gate.isAvailable(plane,
|
---|
61 | lhbpGates,
|
---|
62 | [p.gateNumber]):
|
---|
63 | return True
|
---|
64 |
|
---|
65 | return False
|
---|
66 |
|
---|
67 | def getOccupiedGateNumbers(self):
|
---|
68 | """Get a set containing the numbers of the gates occupied by planes."""
|
---|
69 | gateNumbers = set()
|
---|
70 | for p in self._planes.values():
|
---|
71 | if p.status==const.PLANE_HOME and p.gateNumber:
|
---|
72 | gateNumbers.add(p.gateNumber)
|
---|
73 | return gateNumbers
|
---|
74 |
|
---|
75 | def iterAvailableLHBPGates(self, tailNumber):
|
---|
76 | """Iterate over the available gates at LHBP."""
|
---|
77 | occupiedGateNumbers = self.getOccupiedGateNumbers()
|
---|
78 | plane = self.__getitem__(tailNumber)
|
---|
79 | for gate in lhbpGates.gates:
|
---|
80 | if gate.isAvailable(plane, lhbpGates, occupiedGateNumbers):
|
---|
81 | yield gate
|
---|
82 |
|
---|
83 | def updatePlane(self, tailNumber, status, gateNumber = None):
|
---|
84 | """Update the status of the given plane."""
|
---|
85 | if tailNumber in self._planes:
|
---|
86 | plane = self._planes[tailNumber]
|
---|
87 | plane.status = status
|
---|
88 | plane.gateNumber = gateNumber
|
---|
89 |
|
---|
90 | def _addPlane(self, plane):
|
---|
91 | """Add the given plane to the fleet."""
|
---|
92 | self._planes[plane.tailNumber] = plane
|
---|
93 |
|
---|
94 | def __iter__(self):
|
---|
95 | """Get an iterator over the planes."""
|
---|
96 | for plane in self._planes.values():
|
---|
97 | yield plane
|
---|
98 |
|
---|
99 | def __getitem__(self, tailNumber):
|
---|
100 | """Get the plane with the given tail number.
|
---|
101 |
|
---|
102 | If the plane is not in the fleet, None is returned."""
|
---|
103 | return self._planes[tailNumber] if tailNumber in self._planes else None
|
---|
104 |
|
---|
105 | def __repr__(self):
|
---|
106 | """Get the representation of the fleet object."""
|
---|
107 | return self._planes.__repr__()
|
---|