-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontroller.py
66 lines (59 loc) · 2.83 KB
/
controller.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
from elevator import Elevator
from interface import Interface
from threading import Thread
class Controller(object):
"""Manages/controls one or more elevators that work together, assuming they both can access the same floors"""
def __init__(self, elevators, floors, use_gui=True):
"""Creates a controller with `elevators` (positive int) elevators that can access `floors` (positive int)"""
self.floors = floors
self.elevators = list()
for x in range(elevators):
self.elevators.append(Elevator(self.floors, name="Elevator " + str(x+1)))
self.called = dict() # {1: {"up": False, "down": False}, 2: {"up": False, "down": False}}
for x in range(floors):
self.called[x+1] = {"up": False, "down": False}
if use_gui:
self.interface = Interface(self)
else:
self.interface = None
def monitor_calls(self):
"""Monitors elevator calls"""
while True:
for floor in range(1, self.floors+1):
for direction in ["up", "down"]:
if self.called[floor][direction]:
self.assign_elevator(floor, direction)
def assign_elevator(self, floor, direction):
"""Assigns an elevator that is either not busy or is able to service the call"""
assigned = None
elevators = sorted(self.elevators, key=lambda e: abs(e.current_floor - floor))
for elevator in elevators:
if elevator.busy:
# called to go up and elevator going up from lower floor
if (direction == "up") and elevator.going_up and (elevator.current_floor < floor):
elevator.go_to[floor] = True
self.called[floor][direction] = False
break
# called to go down and elevator going down from higher floor
elif (direction == "down") and not elevator.going_up and (elevator.current_floor > floor):
elevator.go_to[floor] = True
self.called[floor][direction] = False
break
else:
# elevator has nothing to do, so give it something to do
elevator.busy = True
elevator.go_to[floor] = True
self.called[floor][direction] = False
break
def call_elevator(self, floor, direction):
"""Calls an elevator from floor `floor` to the given direction ("up"/"down")"""
self.called[floor][direction] = True
def run(self):
# Start the elevators
for elevator in self.elevators:
Thread(target=elevator.run).start()
# Start the elevator assigner
Thread(target=self.monitor_calls).start()
if self.interface:
self.interface.run()
self.interface.window.mainloop()