-
Notifications
You must be signed in to change notification settings - Fork 2
/
elev.erl
145 lines (120 loc) · 4.04 KB
/
elev.erl
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
-module(elev).
-export([start/1]).
start(ElevatorType) ->
connection_manager:start_auto_discovery(),
OrderDistributerManagerPID = spawn(fun() -> order_distributer_manager_init() end),
OrderDistributerPID = order_distributer:start(OrderDistributerManagerPID),
register(order_distributer, OrderDistributerPID),
DriverManagerPID = spawn(fun() -> driver_manager_init() end),
elev_driver:start(DriverManagerPID, ElevatorType),
FsmManagerPID = spawn(fun() -> fsm_manager_init() end),
FsmPID = elev_fsm:start(FsmManagerPID),
register(fsm, FsmPID),
ButtonLightManagerPID = spawn(fun() -> button_light_manager_init() end),
ScheduleManagerPID = spawn(fun() -> schedule_manager_init() end),
SchedulePID = schedule:start(ScheduleManagerPID),
register(schedule, SchedulePID),
PlausibilityCheckManager = spawn(fun() -> plausibility_check_manager() end),
PlausibilityCheckerPID = plausibility_checks:start_travel_time_plausibility_check(PlausibilityCheckManager),
register(plausibilityChecker, PlausibilityCheckerPID),
OrderDistributerManagerPID ! init_completed,
FsmManagerPID ! init_completed,
DriverManagerPID ! init_completed,
ButtonLightManagerPID ! init_completed,
ScheduleManagerPID ! init_completed.
plausibility_check_manager() ->
receive
{plausibility_check_failed, _Check} ->
io:format("plausibility check failed")
end,
plausibility_check_manager().
fsm_manager_init() ->
receive init_completed ->
ok
end,
fsm_manager().
fsm_manager() ->
receive
{init, started} ->
elev_driver:set_motor_direction(up);
{init, completed} ->
ok;
{direction, request, Caller} ->
Direction = schedule:get_next_direction(schedule),
Caller ! {direction, response, Direction};
{motor, up} ->
plausibility_checks:motor_started(plausibilityChecker),
elev_driver:set_motor_direction(up),
schedule:floor_left(schedule, up);
{motor, down} ->
plausibility_checks:motor_started(plausibilityChecker),
elev_driver:set_motor_direction(down),
schedule:floor_left(schedule, down);
{motor, stop} ->
plausibility_checks:motor_stopped(plausibilityChecker),
elev_driver:set_motor_direction(stop);
{doors, open} ->
schedule:stopped_at_floor(schedule),
elev_driver:set_door_open_lamp(on);
{doors, close} ->
elev_driver:set_door_open_lamp(off)
end,
fsm_manager().
driver_manager_init() ->
receive init_completed ->
ok
end,
driver_manager().
driver_manager() ->
receive
{new_order, Direction, Floor} ->
order_distributer:add_order(Floor, Direction);
{floor_reached, Floor} ->
elev_driver:set_floor_indicator(Floor),
elev_fsm:event_floor_reached(fsm),
schedule:floor_reached(schedule, Floor)
end,
driver_manager().
button_light_manager_init() ->
receive init_completed ->
ok
end,
button_light_manager().
button_light_manager() ->
SetLightFunction = fun(Floor, Direction) ->
ButtonState = case order_distributer:is_order(Floor, Direction) of
true ->
on;
false ->
off
end,
elev_driver:set_button_lamp(Floor, Direction, ButtonState)
end,
elev_driver:foreach_button(SetLightFunction),
timer:sleep(200),
button_light_manager().
order_distributer_manager_init() ->
receive init_completed ->
ok
end,
order_distributer_manager().
order_distributer_manager() ->
receive
{bid_request, Floor, Direction, Caller} ->
Caller ! {bid_price, schedule:get_order_cost(schedule, Floor, Direction)};
{handle_order, Floor, Direction, _Caller} ->
schedule:add_order(schedule, Floor, Direction),
elev_fsm:event_new_order(fsm)
end,
order_distributer_manager().
schedule_manager_init() ->
receive init_completed ->
ok
end,
schedule_manager().
schedule_manager() ->
receive
{order_served, Floor, Direction} ->
order_distributer:remove_order(Floor, Direction)
end,
schedule_manager().