File simulatie/connection.py changed (mode: 100644) (index eda78d6..e3018ee) |
1 |
1 |
import pika |
import pika |
2 |
2 |
import threading |
import threading |
|
3 |
|
import json |
3 |
4 |
|
|
4 |
5 |
import model |
import model |
5 |
6 |
import pyg |
import pyg |
6 |
7 |
|
|
|
8 |
|
import time |
|
9 |
|
|
|
10 |
|
# lights = [[id, amountOfVehicles, DirRequests]] |
|
11 |
|
lights = [ |
|
12 |
|
[101, 0, None], |
|
13 |
|
[102, 0, None], |
|
14 |
|
[103, 0, None], |
|
15 |
|
[104, 0, None], |
|
16 |
|
[105, 0, None], |
|
17 |
|
[106, 0, None], |
|
18 |
|
[107, 0, None], |
|
19 |
|
[108, 0, None], |
|
20 |
|
[109, 0, None], |
|
21 |
|
[110, 0, None], |
|
22 |
|
[201, 0, None], |
|
23 |
|
[301, 0, None], |
|
24 |
|
[302, 0, None], |
|
25 |
|
[303, 0, None], |
|
26 |
|
[304, 0, None], |
|
27 |
|
[305, 0, None], |
|
28 |
|
[401, 0, None], |
|
29 |
|
[402, 0, None], |
|
30 |
|
[403, 0, None], |
|
31 |
|
[404, 0, None], |
|
32 |
|
[405, 0, None], |
|
33 |
|
[406, 0, None], |
|
34 |
|
[501, 0, None], |
|
35 |
|
[601, 0, None] |
|
36 |
|
] |
|
37 |
|
|
|
38 |
|
msg = '' |
|
39 |
|
|
7 |
40 |
creds = pika.PlainCredentials(model.uname, model.passwd) |
creds = pika.PlainCredentials(model.uname, model.passwd) |
8 |
41 |
SendConnection = pika.BlockingConnection(pika.ConnectionParameters(host=model.host, virtual_host=model.vhost, credentials=creds)) |
SendConnection = pika.BlockingConnection(pika.ConnectionParameters(host=model.host, virtual_host=model.vhost, credentials=creds)) |
9 |
42 |
RecieveConnection = pika.BlockingConnection(pika.ConnectionParameters(host=model.host, virtual_host=model.vhost, credentials=creds)) |
RecieveConnection = pika.BlockingConnection(pika.ConnectionParameters(host=model.host, virtual_host=model.vhost, credentials=creds)) |
|
... |
... |
def callback(ch, method, prop, body): |
20 |
53 |
print(model.sendQueue + ': status recieved!') |
print(model.sendQueue + ': status recieved!') |
21 |
54 |
pyg.update(body.decode('utf-8', 'ignore')) |
pyg.update(body.decode('utf-8', 'ignore')) |
22 |
55 |
|
|
23 |
|
def sendMsg(msg): |
|
|
56 |
|
def sendMsg(msg, index, amount): |
24 |
57 |
channel.basic_publish ( |
channel.basic_publish ( |
25 |
58 |
exchange='', |
exchange='', |
26 |
59 |
routing_key=model.sendQueue, |
routing_key=model.sendQueue, |
27 |
60 |
body=msg |
body=msg |
28 |
61 |
) |
) |
29 |
|
pyg.spawnVehicle(0) |
|
|
62 |
|
|
30 |
63 |
print('Msg sent.') |
print('Msg sent.') |
31 |
|
|
|
|
64 |
|
|
|
65 |
|
for i in range(0, amount): |
|
66 |
|
pyg.spawnVehicle(index) |
|
67 |
|
time.sleep(2) |
|
68 |
|
|
|
69 |
|
def update(index, amountOfCars, dirRequests): |
|
70 |
|
lights[index][1] = amountOfCars |
|
71 |
|
lights[index][2] = dirRequests |
|
72 |
|
|
|
73 |
|
msg = json.dumps({ |
|
74 |
|
"TrafficUpdate": { |
|
75 |
|
"LightId": lights[index][0], |
|
76 |
|
"Count": lights[index][1], |
|
77 |
|
"DirectionRequests": lights[index][2] |
|
78 |
|
} |
|
79 |
|
}, sort_keys=True, indent=4, separators=(',',': ')) |
|
80 |
|
|
|
81 |
|
sendMsg(msg, index, amountOfCars) |
|
82 |
|
|
32 |
83 |
def listen(): |
def listen(): |
33 |
84 |
print('Waiting for a sign..') |
print('Waiting for a sign..') |
34 |
85 |
recieve.basic_consume ( |
recieve.basic_consume ( |
File simulatie/pyg.py changed (mode: 100644) (index 0f9270e..4dbb9e0) |
... |
... |
import time |
6 |
6 |
import json |
import json |
7 |
7 |
|
|
8 |
8 |
import trafficModel as tm |
import trafficModel as tm |
|
9 |
|
import connection |
9 |
10 |
|
|
10 |
11 |
vehicles = list() |
vehicles = list() |
11 |
12 |
|
|
|
... |
... |
class Vehicle(object): |
74 |
75 |
self.wp += 1 |
self.wp += 1 |
75 |
76 |
print("W: " + str(self.wp) + " / " + str(len(tm.queues[self.trafficId]))) |
print("W: " + str(self.wp) + " / " + str(len(tm.queues[self.trafficId]))) |
76 |
77 |
|
|
77 |
|
elif self.wp < len(tm.waypoints[self.trafficId]): |
|
|
78 |
|
elif not self.waiting and self.wp < len(tm.waypoints[self.trafficId]): |
78 |
79 |
self.goto(tm.waypoints[self.trafficId][self.wp][0], tm.waypoints[self.trafficId][self.wp][1]) |
self.goto(tm.waypoints[self.trafficId][self.wp][0], tm.waypoints[self.trafficId][self.wp][1]) |
79 |
80 |
self.wp += 1 |
self.wp += 1 |
80 |
81 |
print("NW: " + str(self.wp) + " / " + str(len(tm.waypoints[self.trafficId]))) |
print("NW: " + str(self.wp) + " / " + str(len(tm.waypoints[self.trafficId]))) |
|
... |
... |
class Vehicle(object): |
85 |
86 |
if tm.lights[self.trafficId]["Status"] != 0 and self.waiting and self.wp <= len(tm.queues[self.trafficId]): |
if tm.lights[self.trafficId]["Status"] != 0 and self.waiting and self.wp <= len(tm.queues[self.trafficId]): |
86 |
87 |
self.waiting = False |
self.waiting = False |
87 |
88 |
self.wp = 0 |
self.wp = 0 |
|
89 |
|
connection.update(self.trafficId, 0, None) |
88 |
90 |
self.update() |
self.update() |
89 |
91 |
|
|
90 |
92 |
clock = pygame.time.Clock() |
clock = pygame.time.Clock() |
|
... |
... |
def spawnVehicle(index): |
118 |
120 |
|
|
119 |
121 |
# Mainloop |
# Mainloop |
120 |
122 |
def gameloop(): |
def gameloop(): |
121 |
|
#id = 0 |
|
122 |
|
#car = Vehicle(tm.queues[id][0][0],tm.queues[id][0][1],id) |
|
|
123 |
|
id = 5 |
|
124 |
|
car = Vehicle(tm.queues[id][0][0],tm.queues[id][0][1],id) |
123 |
125 |
while True: |
while True: |
124 |
126 |
clock.tick(60) |
clock.tick(60) |
125 |
127 |
for event in pygame.event.get(): |
for event in pygame.event.get(): |
|
... |
... |
def gameloop(): |
129 |
131 |
screen.blit(bg, bg.get_rect()) |
screen.blit(bg, bg.get_rect()) |
130 |
132 |
for vehicle in vehicles: |
for vehicle in vehicles: |
131 |
133 |
vehicle.trafficCheck() |
vehicle.trafficCheck() |
132 |
|
#car.trafficCheck() |
|
|
134 |
|
car.trafficCheck() |
133 |
135 |
#screen.blit(car.auto, car.rect) |
#screen.blit(car.auto, car.rect) |
134 |
136 |
pygame.display.flip() |
pygame.display.flip() |
File simulatie/reciever.py deleted (index dcd4cde..0000000) |
1 |
|
#!/usr/bin/env python |
|
2 |
|
|
|
3 |
|
import pika |
|
4 |
|
import model |
|
5 |
|
|
|
6 |
|
creds = pika.PlainCredentials(model.uname, model.passwd) |
|
7 |
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host=model.host, virtual_host=model.vhost, credentials=creds)) |
|
8 |
|
channel = connection.channel() |
|
9 |
|
|
|
10 |
|
channel.exchange_declare( |
|
11 |
|
exchange='1234', |
|
12 |
|
exchange_type='direct' |
|
13 |
|
) |
|
14 |
|
|
|
15 |
|
result = channel.queue_declare(exclusive=True) |
|
16 |
|
queuename = result.method.queue |
|
17 |
|
|
|
18 |
|
channel.queue_bind( |
|
19 |
|
exchange='1234', |
|
20 |
|
queue=queuename, |
|
21 |
|
routing_key=model.vhost |
|
22 |
|
) |
|
23 |
|
|
|
24 |
|
def callback(ch, method, prop, body): |
|
25 |
|
print('%r ontvangen' % body) |
|
26 |
|
|
|
27 |
|
channel.basic_consume ( |
|
28 |
|
callback, |
|
29 |
|
queue=queuename, |
|
30 |
|
no_ack=True |
|
31 |
|
) |
|
32 |
|
|
|
33 |
|
print('Waiting for a sign..') |
|
34 |
|
channel.start_consuming() |
|
File simulatie/sender.py deleted (index 0b2e6e4..0000000) |
1 |
|
#!/usr/bin/env python |
|
2 |
|
|
|
3 |
|
import pika |
|
4 |
|
#import threading |
|
5 |
|
import model |
|
6 |
|
|
|
7 |
|
creds = pika.PlainCredentials(model.uname, model.passwd) |
|
8 |
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host=model.host, virtual_host=model.vhost, credentials=creds)) |
|
9 |
|
channel = connection.channel() |
|
10 |
|
|
|
11 |
|
channel.queue_declare( |
|
12 |
|
queue=model.c, |
|
13 |
|
auto_delete=True |
|
14 |
|
) |
|
15 |
|
|
|
16 |
|
def start(): |
|
17 |
|
channel.basic_consume( |
|
18 |
|
consumer_callback=cons_callb, |
|
19 |
|
queue=model.c |
|
20 |
|
); |
|
21 |
|
|
|
22 |
|
channel.start_consuming() |
|
23 |
|
|
|
24 |
|
def cons_callb(self, channel, method, properties, body): |
|
25 |
|
content = body.decode('utf-8', 'ignore') |
|
26 |
|
print(content) |
|
27 |
|
|
|
28 |
|
#channel.exchange_declare( |
|
29 |
|
# exchange='1234', |
|
30 |
|
# exchange_type='direct' |
|
31 |
|
#) |
|
32 |
|
|
|
33 |
|
channel.basic_publish ( |
|
34 |
|
exchange='', |
|
35 |
|
routing_key=model.vhost, |
|
36 |
|
properties=pika.BasicProperties(correlation_id=properties.correlation_id), |
|
37 |
|
body='Simulator ' + model.vhost |
|
38 |
|
) |
|
39 |
|
|
|
40 |
|
cannel.basic_ack(delivery_tag=method.delivery_tag) |
|
41 |
|
|
|
42 |
|
print('Ack sent.') |
|
43 |
|
|
|
44 |
|
#connection.close() |
|
45 |
|
start() |
|
46 |
|
#thread = threading.Thread(target=start) |
|
47 |
|
#thread.start() |
|
48 |
|
#thread.join(0) |
|
File simulatie/traffic.py changed (mode: 100644) (index af2c9a5..ab50a6d) |
1 |
|
import json |
|
2 |
|
import time |
|
3 |
1 |
import sys |
import sys |
4 |
2 |
|
|
5 |
3 |
import connection |
import connection |
6 |
4 |
|
|
7 |
|
# lights = [[id, amountOfVehicles, DirRequests]] |
|
8 |
|
lights = [ |
|
9 |
|
[101, 0, None], |
|
10 |
|
[102, 0, None], |
|
11 |
|
[103, 0, None], |
|
12 |
|
[104, 0, None], |
|
13 |
|
[105, 0, None], |
|
14 |
|
[106, 0, None], |
|
15 |
|
[107, 0, None], |
|
16 |
|
[108, 0, None], |
|
17 |
|
[109, 0, None], |
|
18 |
|
[110, 0, None], |
|
19 |
|
[201, 0, None], |
|
20 |
|
[301, 0, None], |
|
21 |
|
[302, 0, None], |
|
22 |
|
[303, 0, None], |
|
23 |
|
[304, 0, None], |
|
24 |
|
[305, 0, None], |
|
25 |
|
[401, 0, None], |
|
26 |
|
[402, 0, None], |
|
27 |
|
[403, 0, None], |
|
28 |
|
[404, 0, None], |
|
29 |
|
[405, 0, None], |
|
30 |
|
[406, 0, None], |
|
31 |
|
[501, 0, None], |
|
32 |
|
[601, 0, None] |
|
33 |
|
] |
|
34 |
|
|
|
35 |
|
msg = '' |
|
36 |
|
|
|
37 |
|
def update(index, amountOfCars, dirRequests): |
|
38 |
|
lights[index][1] = amountOfCars |
|
39 |
|
lights[index][2] = dirRequests |
|
40 |
|
|
|
41 |
|
msg = json.dumps({ |
|
42 |
|
"TrafficUpdate": { |
|
43 |
|
"LightId": lights[index][0], |
|
44 |
|
"Count": lights[index][1], |
|
45 |
|
"DirectionRequests": lights[index][2] |
|
46 |
|
} |
|
47 |
|
}, sort_keys=True, indent=4, separators=(',',': ')) |
|
48 |
|
|
|
49 |
|
connection.sendMsg(msg) |
|
50 |
|
|
|
51 |
5 |
while True: |
while True: |
52 |
6 |
ui = raw_input() |
ui = raw_input() |
53 |
7 |
ui = list(ui) |
ui = list(ui) |
54 |
|
print([lights[int(ui[0])], int(ui[1]), None]) |
|
55 |
|
update(int(ui[0]), int(ui[1]), None) |
|
|
8 |
|
print([connection.lights[int(ui[0])], int(ui[1]), None]) |
|
9 |
|
connection.update(int(ui[0]), int(ui[1]), None) |
56 |
10 |
|
|
57 |
11 |
sys.exit() |
sys.exit() |