Repository navigation
Expand file tree
/
Copy pathcontrol.py
More file actions
110 lines (85 loc) · 2.55 KB
/
Copy pathcontrol.py
File metadata and controls
110 lines (85 loc) · 2.55 KB
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
import time
from enum import Enum
from shared import *
# set up leds
leds = []
for i in range(2, 2+PLATFORM_CAPACITY):
led = mraa.Gpio(i)
led.dir(mraa.DIR_OUT)
led.write(OFF)
leds.append(led)
class State(Enum):
WAITING_FOR_PASSENGERS = 0
WAITING_FOR_CAR = 1
class Control():
state = State.WAITING_FOR_PASSENGERS
passengers = []
cars = Queue()
timeout = 0
def add_passenger(self, passenger):
self.passengers.append(passenger)
# walk from the edge of platform
time.sleep(TOTAL_DELAY*(6-PLATFORM_CAPACITY))
leds[-1].write(ON)
time.sleep(WAIT_DELAY)
for i in range(len(leds)-1, len(self.passengers)-1, -1):
leds[i-1].write(ON)
time.sleep(OVERLAP_DELAY)
leds[i].write(OFF)
time.sleep(WAIT_DELAY)
def clear_lights(self):
for led in leds:
led.write(OFF)
def display_cars(self):
print "CARS %s" % self.cars.items
def request_pickup(self):
if self.cars.count() > 0:
send_message("PICKUP %s %s" % (self.cars.get(), self.passengers))
control = Control()
def control_c_handler(signum, frame):
control.clear_lights()
exit_program()
signal.signal(signal.SIGINT, control_c_handler)
def on_message(client, userdata, msg):
if "TURNSTILE" in msg.payload:
print "MESSAGE FROM TURNSTILE %s" % msg.payload
splits = str.split(msg.payload, " ")
if splits[4] == "requests" and splits[5] == "entry":
pid = splits[8][1:]
p = Passenger(int(pid))
if len(control.passengers) < PLATFORM_CAPACITY:
control.add_passenger(p)
send_message("CONTROL allow passenger #%d" % p.id)
if len(control.passengers) == CAR_CAPACITY:
control.state = State.WAITING_FOR_CAR
control.request_pickup()
else:
send_message("CONTROL platform is full")
elif "CAR" in msg.payload:
print "MESSAGE FROM CAR %s" % msg.payload
splits = str.split(msg.payload, " ")
if splits[5] == "READY":
if not splits[4] in control.cars.items:
control.cars.put_distinct(splits[4])
control.display_cars()
elif splits[5] == "ACCEPT":
control.timeout = 0
control.state = State.WAITING_FOR_PASSENGERS
control.clear_lights()
del control.passengers[:]
control.display_cars()
def main():
mqtt_client.will_set(mqtt_topic, '___Will of CONTROL___', 0, False)
mqtt_client.on_message = on_message
mqtt_client.loop_start()
while True:
if control.state == State.WAITING_FOR_CAR:
time.sleep(0.5)
control.timeout = control.timeout + 1
if control.timeout > 20:
send_message("CONTROL timeout, requesting another car")
control.request_pickup()
control.timeout = 0
else:
time.sleep(1)
if __name__ == '__main__': main()