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
| class Elevator {
List<int> activeQueue = [];
List<int> shadowQueue = [];
final ElevatorCurrentState state = ElevatorCurrentState(
currentFloor: 0,
currentState: ElevatorState.idle,
currentDirection: ElevatorDirection.rest,
);
Future<void> floorRequestHandler({
required int requestFromFloor,
required ElevatorDirection requestDirection,
}) async {
print(
"Floor request received: floor $requestFromFloor, direction $requestDirection",
);
await taskScheduler(
requestDirection: requestDirection,
requestedFloor: requestFromFloor,
);
}
Future<void> pannelRequestHandler({required int requestedFloor}) async {
print("Panel request received: floor $requestedFloor");
await taskScheduler(
requestDirection: state.currentFloor > requestedFloor
? ElevatorDirection.down
: ElevatorDirection.up,
requestedFloor: requestedFloor,
);
}
Future<void> taskScheduler({
required int requestedFloor,
required ElevatorDirection requestDirection,
}) async {
if (requestedFloor == state.currentFloor) {
await openDoor();
await closeDoor();
return;
}
if (state.currentDirection == ElevatorDirection.rest) {
activeQueue.add(requestedFloor);
directionChangeNotifier(requestDirection);
sortActive();
print("Added floor $requestedFloor to active queue while at rest");
return;
}
if (state.currentDirection == requestDirection) {
if (state.currentFloor > requestedFloor) {
addIfNotPresent(shadowQueue, requestedFloor);
} else if (state.currentFloor < requestedFloor) {
addIfNotPresent(activeQueue, requestedFloor);
}
} else {
addIfNotPresent(shadowQueue, requestedFloor);
}
}
void floorChangeNotifier(int newFloor) {
state.currentFloor = newFloor;
print("Current Floor: $newFloor");
}
void stateChangeNotifier(ElevatorState newState) {
state.currentState = newState;
print("Current State: $newState");
}
void directionChangeNotifier(ElevatorDirection newDirection) {
state.currentDirection = newDirection;
print("Current Direction: $newDirection");
}
Future<void> openDoor() async {
stateChangeNotifier(ElevatorState.open);
await Future.delayed(
Duration(milliseconds: 600),
); // Simulate door opening time
}
Future<void> closeDoor() async {
stateChangeNotifier(ElevatorState.closed);
await Future.delayed(
Duration(milliseconds: 600),
); // Simulate door closing time
}
void setIdle() {
stateChangeNotifier(ElevatorState.idle);
directionChangeNotifier(ElevatorDirection.rest);
}
Future<void> move() async {
if (state.currentState == ElevatorState.maintenance) {
print("Elevator is under maintenance. Cannot move.");
return;
}
if (state.currentState == ElevatorState.open) {
await closeDoor();
}
while (activeQueue.isNotEmpty || shadowQueue.isNotEmpty) {
print(
"Starting move cycle: activeQueue=$activeQueue, shadowQueue=$shadowQueue",
);
if (activeQueue.isEmpty) {
updateTasks();
}
if (activeQueue.isEmpty) {
setIdle();
break;
}
int nextFloor = activeQueue.first;
if (nextFloor == state.currentFloor) {
print("Arrived at floor $nextFloor, opening doors");
await openDoor();
activeQueue.removeAt(0);
await closeDoor();
} else {
// set direction if rest
if (state.currentDirection == ElevatorDirection.rest) {
directionChangeNotifier(
nextFloor > state.currentFloor
? ElevatorDirection.up
: ElevatorDirection.down,
);
}
floorChangeNotifier(
state.currentFloor +
(state.currentDirection == ElevatorDirection.up ? 1 : -1),
);
await Future.delayed(
Duration(milliseconds: 1000),
); // Simulate time to move between floors
}
}
}
void updateTasks() {
if (shadowQueue.isEmpty) return;
final hasAbove = shadowQueue.any((f) => f > state.currentFloor);
final hasBelow = shadowQueue.any((f) => f < state.currentFloor);
ElevatorDirection newDir;
if (state.currentDirection == ElevatorDirection.up) {
newDir = hasBelow ? ElevatorDirection.down : ElevatorDirection.up;
} else if (state.currentDirection == ElevatorDirection.down) {
newDir = hasAbove ? ElevatorDirection.up : ElevatorDirection.down;
} else {
newDir = hasAbove ? ElevatorDirection.up : ElevatorDirection.down;
}
directionChangeNotifier(newDir);
final toServe = shadowQueue
.where(
(f) => newDir == ElevatorDirection.up
? f > state.currentFloor
: f < state.currentFloor,
)
.toList();
final remaining = shadowQueue
.where(
(f) => newDir == ElevatorDirection.up
? f < state.currentFloor
: f > state.currentFloor,
)
.toList();
activeQueue.addAll(toServe);
shadowQueue
..clear()
..addAll(remaining);
sortActive();
}
void sortActive() {
if (state.currentDirection == ElevatorDirection.up) {
activeQueue.sort();
} else {
activeQueue.sort((a, b) => b.compareTo(a));
}
}
void addIfNotPresent(List<int> queue, int floor) {
if (!queue.contains(floor)) {
queue.add(floor);
queue.sort();
}
}
}
|