File simulatie/connection.py changed (mode: 100644) (index fc6cd90..fd3f1a3) |
... |
... |
def callback(ch, method, prop, body): |
61 |
61 |
print(model.sendQueue + ': status recieved!') |
print(model.sendQueue + ': status recieved!') |
62 |
62 |
pyg.update(body.decode('utf-8', 'ignore')) |
pyg.update(body.decode('utf-8', 'ignore')) |
63 |
63 |
|
|
64 |
|
def sendMsg(msg, index, amount): |
|
|
64 |
|
def purelySendMsg(msg): |
|
65 |
|
channel.basic_publish ( |
|
66 |
|
exchange='', |
|
67 |
|
routing_key=model.sendQueue, |
|
68 |
|
body=msg |
|
69 |
|
) |
|
70 |
|
|
|
71 |
|
print('Msg sent.') |
|
72 |
|
|
|
73 |
|
def sendMsg(msg, index, amount, dirRequests): |
65 |
74 |
channel.basic_publish ( |
channel.basic_publish ( |
66 |
75 |
exchange='', |
exchange='', |
67 |
76 |
routing_key=model.sendQueue, |
routing_key=model.sendQueue, |
|
... |
... |
def sendMsg(msg, index, amount): |
71 |
80 |
print('Msg sent.') |
print('Msg sent.') |
72 |
81 |
|
|
73 |
82 |
for i in range(0, amount): |
for i in range(0, amount): |
74 |
|
pyg.spawnVehicle(index) |
|
|
83 |
|
pyg.spawnVehicle(index, dirRequests) |
75 |
84 |
time.sleep(2) |
time.sleep(2) |
76 |
85 |
|
|
77 |
86 |
def update(index, addCars, dirRequests): |
def update(index, addCars, dirRequests): |
78 |
87 |
if inputEnabled: |
if inputEnabled: |
79 |
88 |
lights[index][1] += addCars |
lights[index][1] += addCars |
80 |
|
lights[index][2] = dirRequests |
|
|
89 |
|
#lights[index][2] = dirRequests |
81 |
90 |
waitingVehicles = None |
waitingVehicles = None |
82 |
91 |
vehicleMutation = 0 |
vehicleMutation = 0 |
83 |
92 |
|
|
|
... |
... |
def update(index, addCars, dirRequests): |
86 |
95 |
|
|
87 |
96 |
for i in range(0, vehicleMutation): |
for i in range(0, vehicleMutation): |
88 |
97 |
if addCars > 0: |
if addCars > 0: |
89 |
|
lights[index][2].append(dirRequests[i]) |
|
|
98 |
|
lights[index][2].append(dirRequests) |
90 |
99 |
else: |
else: |
91 |
100 |
lights[index][2].pop() |
lights[index][2].pop() |
92 |
101 |
|
|
93 |
102 |
if len(lights[index][2]) > 0: |
if len(lights[index][2]) > 0: |
94 |
103 |
waitingVehicles = lights[index][2] |
waitingVehicles = lights[index][2] |
|
104 |
|
print(waitingVehicles) |
95 |
105 |
|
|
96 |
106 |
msg = json.dumps({ |
msg = json.dumps({ |
97 |
107 |
"TrafficUpdate": { |
"TrafficUpdate": { |
98 |
108 |
"LightId": lights[index][0], |
"LightId": lights[index][0], |
99 |
109 |
"Count": lights[index][1], |
"Count": lights[index][1], |
100 |
|
"DirectionRequests": waitingVehicles |
|
|
110 |
|
"DirectionRequests": dirRequests |
|
111 |
|
} |
|
112 |
|
}, sort_keys=True, indent=4, separators=(',',': ')) |
|
113 |
|
|
|
114 |
|
sendMsg(msg, index, addCars, dirRequests) # Geeft deze functie de bug bij een transform? |
|
115 |
|
|
|
116 |
|
else: |
|
117 |
|
print("INPUT IS DISABLED!") |
|
118 |
|
|
|
119 |
|
def transform(index, original): |
|
120 |
|
if inputEnabled: |
|
121 |
|
msg = json.dumps({ |
|
122 |
|
"TrafficUpdate": { |
|
123 |
|
"LightId": lights[original][0], |
|
124 |
|
"Count": 0,#lights[index][1], |
|
125 |
|
"DirectionRequests": None |
|
126 |
|
} |
|
127 |
|
}, sort_keys=True, indent=4, separators=(',',': ')) |
|
128 |
|
|
|
129 |
|
purelySendMsg(msg) |
|
130 |
|
|
|
131 |
|
msg = json.dumps({ |
|
132 |
|
"TrafficUpdate": { |
|
133 |
|
"LightId": lights[index][0], |
|
134 |
|
"Count": 1,#lights[index][1], |
|
135 |
|
"DirectionRequests": None |
101 |
136 |
} |
} |
102 |
137 |
}, sort_keys=True, indent=4, separators=(',',': ')) |
}, sort_keys=True, indent=4, separators=(',',': ')) |
103 |
138 |
|
|
104 |
|
sendMsg(msg, index, amountOfCars) # Geeft deze functie de bug bij een transform? |
|
|
139 |
|
purelySendMsg(msg) |
105 |
140 |
|
|
106 |
141 |
else: |
else: |
107 |
142 |
print("INPUT IS DISABLED!") |
print("INPUT IS DISABLED!") |
File simulatie/pyg.py changed (mode: 100644) (index 1ff7bbf..8e2eb29) |
... |
... |
class Light(object): |
31 |
31 |
|
|
32 |
32 |
def lightCheck(self): |
def lightCheck(self): |
33 |
33 |
self.lamp_tint = self.lamp.copy() |
self.lamp_tint = self.lamp.copy() |
34 |
|
#if tm.lights[self.lightIndex] == 0: # ROOD |
|
35 |
|
# self.lamp_tint.fill((250, 150, 150), None, pygame.BLEND_RGB_MULT) |
|
36 |
|
#elif tm.lights[self.lightIndex] == 1: # ORANJE |
|
37 |
|
self.lamp_tint.fill((190, 60, 60), None, pygame.BLEND_RGB_MULT) |
|
38 |
|
#elif tm.lights[self.lightIndex] == 2: # GROEN |
|
39 |
|
|
|
|
34 |
|
if tm.lights[self.lightIndex] == 0: # ROOD |
|
35 |
|
self.lamp_tint.fill((250, 150, 150), None, pygame.BLEND_RGB_MULT) |
|
36 |
|
elif tm.lights[self.lightIndex] == 1: # ORANJE |
|
37 |
|
self.lamp_tint.fill((190, 60, 60), None, pygame.BLEND_RGB_MULT) |
|
38 |
|
elif tm.lights[self.lightIndex] == 2: # GROEN |
|
39 |
|
self.lamp_tint.fill((40, 190, 40), None, pygame.BLEND_RGB_MULT) |
|
40 |
|
|
40 |
41 |
screen.blit(self.lamp_tint, self.rect) |
screen.blit(self.lamp_tint, self.rect) |
41 |
42 |
|
|
42 |
43 |
|
|
|
... |
... |
class Vehicle(object): |
64 |
65 |
self.direction = 1 |
self.direction = 1 |
65 |
66 |
else: |
else: |
66 |
67 |
print("ERROR: DIRECTION REQUEST NOT FOUND!") |
print("ERROR: DIRECTION REQUEST NOT FOUND!") |
|
68 |
|
self.direction = 0 |
67 |
69 |
|
|
68 |
70 |
self.rect = self.auto.get_rect() |
self.rect = self.auto.get_rect() |
69 |
71 |
self.dest = [x,y] |
self.dest = [x,y] |
|
... |
... |
class Vehicle(object): |
130 |
132 |
print("NW: " + str(self.wp) + " / " + str(len(tm.waypoints[self.trafficId][self.direction]))) |
print("NW: " + str(self.wp) + " / " + str(len(tm.waypoints[self.trafficId][self.direction]))) |
131 |
133 |
|
|
132 |
134 |
elif not self.waiting: # Check for transform |
elif not self.waiting: # Check for transform |
133 |
|
if self.transform != 0: |
|
134 |
|
self.trafficId = self.transform |
|
|
135 |
|
if str(self.transform) != "0":# and self.transform != None and self.transform != "": |
|
136 |
|
#self.trafficId = self.transform |
135 |
137 |
self.wp = 1 |
self.wp = 1 |
136 |
138 |
self.waiting = True |
self.waiting = True |
137 |
139 |
|
|
138 |
140 |
self.transform = self.checkForTransform() |
self.transform = self.checkForTransform() |
139 |
|
connection.update(self.trafficId, 1, self.direction) |
|
|
141 |
|
connection.transform(self.transform, self.trafficId) # Connection.update moet geen nieuwe vehicle spawnen |
|
142 |
|
self.trafficId = self.transform |
|
143 |
|
print("TRANSFORM: " + str(self.transform)) |
140 |
144 |
else: |
else: |
141 |
145 |
pass # Destructor here? |
pass # Destructor here? |
142 |
146 |
|
|
|
... |
... |
class Vehicle(object): |
149 |
153 |
return |
return |
150 |
154 |
self.waiting = False |
self.waiting = False |
151 |
155 |
self.wp = 0 |
self.wp = 0 |
152 |
|
connection.update(self.trafficId, -1, self.direction) |
|
|
156 |
|
connection.update(self.trafficId, -1, None)#self.direction) |
153 |
157 |
self.update() |
self.update() |
154 |
158 |
|
|
155 |
159 |
clock = pygame.time.Clock() |
clock = pygame.time.Clock() |
|
... |
... |
clock = pygame.time.Clock() |
157 |
161 |
def update(jsonObj): |
def update(jsonObj): |
158 |
162 |
result = json.loads(jsonObj) |
result = json.loads(jsonObj) |
159 |
163 |
tm.lights = result['Lights'] |
tm.lights = result['Lights'] |
|
164 |
|
#for i in range(0, len(lights)): |
|
165 |
|
#lights[i].lighCheck() |
160 |
166 |
|
|
161 |
167 |
|
|
162 |
168 |
def spawnTrein(naarGroningen): |
def spawnTrein(naarGroningen): |
|
... |
... |
def spawnTrein(naarGroningen): |
169 |
175 |
def spawnVehicle(index, dirRequest): |
def spawnVehicle(index, dirRequest): |
170 |
176 |
vehicles.append(Vehicle(tm.queues[index][0][0], tm.queues[index][0][1], index, dirRequest)) |
vehicles.append(Vehicle(tm.queues[index][0][0], tm.queues[index][0][1], index, dirRequest)) |
171 |
177 |
|
|
|
178 |
|
for i in range(0, len(tm.lightsPos)): |
|
179 |
|
lights.append(Light(i)) |
|
180 |
|
|
172 |
181 |
# Mainloop |
# Mainloop |
173 |
182 |
def gameloop(): |
def gameloop(): |
174 |
183 |
#id = 12 |
#id = 12 |
File simulatie/traffic.py changed (mode: 100644) (index 8b0a0b6..db87130) |
... |
... |
while True: |
11 |
11 |
print("Direction Reqeust:") |
print("Direction Reqeust:") |
12 |
12 |
direction = raw_input() |
direction = raw_input() |
13 |
13 |
#ui = list(ui) |
#ui = list(ui) |
14 |
|
if direction == None: |
|
15 |
|
print([connection.lights[int(vehicle)], connection.lights[1] + 1, None]) |
|
|
14 |
|
if direction == None or direction == "": |
|
15 |
|
#print([connection.lights[int(vehicle)], connection.lights[1] + 1, 2]) |
16 |
16 |
connection.update(int(vehicle), 1, None) |
connection.update(int(vehicle), 1, None) |
17 |
17 |
else: |
else: |
18 |
|
print([connection.lights[int(vehicle)], connection.lights[1] + 1, int(direction)]) |
|
19 |
|
connection.update(int(vehicle), 1, int(direction)) |
|
|
18 |
|
#print([connection.lights[int(vehicle)], connection.lights[1] + 1, int(direction)]) |
|
19 |
|
connection.update(int(vehicle), 1, [int(direction)]) |
20 |
20 |
|
|
21 |
21 |
sys.exit() |
sys.exit() |