-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevator.py
255 lines (206 loc) · 7.96 KB
/
elevator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import enum
import logging
import random
from typing import List, Optional
logger = logging.getLogger(__name__)
FLOOR_MAX_LIMIT = 10
class FloorError(Exception):
""" Raised when floor number is not correct. """
pass
class EnterElevatorError(Exception):
""" Raised in incorrect elevator entering. """
pass
class ElevatorMoveError(Exception):
""" Raised while elevator moving action. """
pass
class ElevatorDoorsError(Exception):
""" Raised with elevator's doors errors. """
pass
class StatusElevator(enum.Enum):
idle = 0
moving = 1
open = 2
class Direction(enum.Enum):
up = 0
down = 1
class FloorValidatorMixin:
def _validate_floor(self, floor: int, allow_zero: bool = False) -> None:
if not allow_zero and floor < 1:
raise FloorError('The floor cannot be less than 1.')
if floor > FLOOR_MAX_LIMIT:
raise FloorError(
f'The floor cannot be more than {FLOOR_MAX_LIMIT}.')
class Passenger(FloorValidatorMixin):
id: int
__floor_current: int
__floor_destination: int
elevator_called: bool = False
is_inside: bool = False
def __init__(
self, floor: Optional[int] = None, id: Optional[int] = None
) -> None:
if floor:
self.floor_current = floor
else:
self.floor_current = random.randint(1, FLOOR_MAX_LIMIT)
if not id:
self.id = random.randint(1, 1000)
else:
self.id = id
self.generate_destination()
def __str__(self) -> str:
return f'Passenger {self.id}'
def generate_destination(self) -> None:
self.floor_destination = self.floor_current
while self.floor_destination == self.floor_current:
self.floor_destination = random.randint(1, FLOOR_MAX_LIMIT)
@property
def floor_current(self) -> int:
return self.__floor_current
@floor_current.setter
def floor_current(self, floor: int) -> None:
self._validate_floor(floor, allow_zero=True)
self.__floor_current = floor
@property
def floor_destination(self) -> int:
return self.__floor_destination
@floor_destination.setter
def floor_destination(self, floor: int) -> None:
self._validate_floor(floor)
self.__floor_destination = floor
def call_elevator(self, elevator: 'Elevator') -> None:
elevator.add_floor_outside(self.__floor_current)
self.elevator_called = True
def enter_elevator(self, elevator: 'Elevator') -> None:
elevator.add_floor_outside(self.__floor_destination)
self.is_inside = True
self.elevator_called = False
self.floor_current = 0
def leave_elevator(self, elevator: 'Elevator') -> None:
self.floor_current = elevator.floor
self.is_inside = False
self.generate_destination()
def tick(self, elevator: 'Elevator') -> None:
if not self.is_inside and not self.elevator_called:
self.call_elevator(elevator)
class Elevator(FloorValidatorMixin):
status: StatusElevator
direction: Direction
__floor: int
__floors_queue: set[int]
passengers: set[Passenger]
capacity: int
def __init__(self, capacity: int = 5) -> None:
self.status = StatusElevator.idle
self.direction = Direction.up
self.__floor = 1
self.__floors_queue = set()
self.passengers = set()
self.capacity = capacity
logger.debug(f'Elevator created. {random.randint(0, 100)}')
@property
def floor(self) -> int:
return self.__floor
@floor.setter
def floor(self, floor: int) -> None:
self._validate_floor(floor)
self.__floor = floor
def get_floors_queue(self):
return self.__floors_queue
def add_floor_inside(self, floor: int) -> None:
""" Add floor to the floors queue from inside the elevator. """
self._validate_floor(floor)
self.__floors_queue.add(floor)
def add_floor_outside(self, floor: int) -> None:
""" Add floor to the floors queue from outside the elevator. """
self._validate_floor(floor)
self.__floors_queue.add(floor)
def change_direction(self):
if self.direction == Direction.up:
self.direction = Direction.down
else:
self.direction = Direction.up
def move(
self, all_passengers: Optional[List[List[Passenger]]] = None
) -> None:
""" Move the elevator by one floor. """
if len(self.__floors_queue) == 0:
return
if self.status == StatusElevator.open:
raise ElevatorMoveError('Doors open. Elevator cannot move.')
logger.debug(
f'Elevator starts moving. Current floor: {self.floor}. '
f'Direction: {self.direction.name}.'
)
self.status = StatusElevator.moving
try:
if self.direction == Direction.up:
self.floor += 1
else:
self.floor -= 1
self.status = StatusElevator.idle
if self.floor in self.__floors_queue:
self.open_doors()
if all_passengers:
for passenger in all_passengers[self.floor]:
self.add_passenger(passenger)
for passenger in self.passengers.copy():
if passenger.floor_destination == self.floor:
self.remove_passenger(passenger)
self.close_doors()
self.__floors_queue.remove(self.floor)
except FloorError:
""" Top or down floor limits. """
self.status = StatusElevator.idle
logger.debug('Elevator moving. Floor limit reached.')
if self.floor == 1:
self.direction = Direction.up
elif self.floor == FLOOR_MAX_LIMIT:
self.direction = Direction.down
else:
# Check if no more floors in the direction and change direction
need_change = True
for floor in self.__floors_queue:
if self.direction == Direction.up:
if floor > self.floor:
need_change = False
break
else:
if floor < self.floor:
need_change = False
break
if need_change:
self.change_direction()
logger.debug(
f'Elevator finished moving. Current floor: {self.floor}. '
f'Direction: {self.direction.name}.'
)
def open_doors(self):
if self.status != StatusElevator.idle:
raise ElevatorDoorsError(
'Cannot open doors. Elevator is not in idle status.')
self.status = StatusElevator.open
logger.debug('Elevator doors opened.')
def close_doors(self):
if self.status != StatusElevator.open:
raise ElevatorDoorsError(
'Cannot close doors. Doors are not opened.')
self.status = StatusElevator.idle
logger.debug('Elevator doors closed.')
def add_passenger(self, passenger: Passenger) -> None:
if self.status != StatusElevator.open:
raise EnterElevatorError('Doors closed. Passenger cannot enter.')
if passenger in self.passengers:
raise EnterElevatorError('Passenger already in elevator.')
if len(self.passengers) < self.capacity and passenger.elevator_called:
self.passengers.add(passenger)
passenger.enter_elevator(self)
logger.debug(f'{passenger} entered elevator.')
def remove_passenger(self, passenger: Passenger) -> None:
if self.status != StatusElevator.open:
raise EnterElevatorError('Doors closed. Passenger cannot remove.')
if passenger not in self.passengers:
raise EnterElevatorError('Passenger not in elevator.')
self.passengers.remove(passenger)
passenger.leave_elevator(self)
logger.debug(f'{passenger} leaved elevator.')