You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
10 KiB
236 lines
10 KiB
import requests
|
|
import time
|
|
import json
|
|
import os
|
|
|
|
"""
|
|
waitTime = [0, 0, 0, 0]
|
|
serviceTime = [20, 20, 20, 20]
|
|
trafficLightState = [0, 0, 0, 0]
|
|
|
|
|
|
while True:
|
|
os.system("clear")
|
|
res = requests.get("http://cloud_computing_project_api:8080/sensors/read")
|
|
|
|
sensors = json.loads(res.text)
|
|
|
|
sum = [0, 0, 0, 0]
|
|
weight = [0, 0, 0, 0]
|
|
|
|
for i in range(4):
|
|
for j in range(5):
|
|
print(sensors[i*5+j]["value"])
|
|
|
|
for i in range(4):
|
|
for j in range(5):
|
|
sensorValue = sensors[i*5+j]["value"]
|
|
sum[i] += sensorValue
|
|
|
|
priority = 0
|
|
prioritySet = False
|
|
for i in range(4):
|
|
#weight[i] = sum[i] * (1 + waitTime[i] / 40 + 10 / serviceTime[i])
|
|
weight[i] = sum[i] * (1 + waitTime[i] / 40 + 20 / serviceTime[i])
|
|
if(weight[i] != 0):
|
|
prioritySet = True
|
|
|
|
if(weight[i] > weight[priority]):
|
|
priority = i
|
|
|
|
print("weight[" + str(i) + "]= " + str([weight[i]]))
|
|
|
|
if not prioritySet:
|
|
if trafficLightState[0] == 1 or trafficLightState[1] == 1:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=11000&redLight=0&orangeLight=1&greenLight=0")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=21000&redLight=0&orangeLight=1&greenLight=0")
|
|
time.sleep(6)
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=11000&redLight=1&orangeLight=0&greenLight=0")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=21000&redLight=1&orangeLight=0&greenLight=0")
|
|
trafficLightState[0] = 0
|
|
trafficLightState[1] = 0
|
|
|
|
elif trafficLightState[2] == 1 or trafficLightState[3] == 1:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=31000&redLight=0&orangeLight=1&greenLight=0")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=41000&redLight=0&orangeLight=1&greenLight=0")
|
|
time.sleep(6)
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=31000&redLight=1&orangeLight=0&greenLight=0")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=41000&redLight=1&orangeLight=0&greenLight=0")
|
|
trafficLightState[2] = 0
|
|
trafficLightState[3] = 0
|
|
|
|
elif priority == 0 or priority == 1:
|
|
if trafficLightState[2] == 1 or trafficLightState[3] == 1:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=31000&redLight=0&orangeLight=1&greenLight=0")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=41000&redLight=0&orangeLight=1&greenLight=0")
|
|
time.sleep(6)
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=31000&redLight=1&orangeLight=0&greenLight=0")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=41000&redLight=1&orangeLight=0&greenLight=0")
|
|
trafficLightState[2] = 0
|
|
trafficLightState[3] = 0
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=11000&redLight=0&orangeLight=0&greenLight=1")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=21000&redLight=0&orangeLight=0&greenLight=1")
|
|
trafficLightState[0] = 1
|
|
trafficLightState[1] = 1
|
|
serviceTime[0] = 1
|
|
serviceTime[1] = 1
|
|
|
|
elif trafficLightState[0] == 0 or trafficLightState[1] == 0:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=11000&redLight=0&orangeLight=0&greenLight=1")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=21000&redLight=0&orangeLight=0&greenLight=1")
|
|
trafficLightState[0] = 1
|
|
trafficLightState[1] = 1
|
|
serviceTime[0] = 1
|
|
serviceTime[1] = 1
|
|
|
|
elif priority == 2 or priority == 3:
|
|
if trafficLightState[0] == 1 or trafficLightState[1] == 1:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=11000&redLight=0&orangeLight=1&greenLight=0")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=21000&redLight=0&orangeLight=1&greenLight=0")
|
|
time.sleep(6)
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=11000&redLight=1&orangeLight=0&greenLight=0")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=21000&redLight=1&orangeLight=0&greenLight=0")
|
|
trafficLightState[0] = 0
|
|
trafficLightState[1] = 0
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=31000&redLight=0&orangeLight=0&greenLight=1")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=41000&redLight=0&orangeLight=0&greenLight=1")
|
|
trafficLightState[2] = 1
|
|
trafficLightState[3] = 1
|
|
serviceTime[2] = 1
|
|
serviceTime[3] = 1
|
|
|
|
elif trafficLightState[2] == 0 or trafficLightState[3] == 0:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=31000&redLight=0&orangeLight=0&greenLight=1")
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=41000&redLight=0&orangeLight=0&greenLight=1")
|
|
trafficLightState[2] = 1
|
|
trafficLightState[3] = 1
|
|
serviceTime[2] = 1
|
|
serviceTime[3] = 1
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
for i in range(4):
|
|
if trafficLightState[i] == 0 and weight != 0:
|
|
waitTime[i] += 1
|
|
serviceTime[i] = 20
|
|
elif trafficLightState[i] == 1:
|
|
waitTime[i] = 0
|
|
serviceTime[i] += 1
|
|
else:
|
|
waitTime[i] = 0
|
|
serviceTime[i] = 20
|
|
"""
|
|
|
|
|
|
|
|
class Lane:
|
|
def __init__(self, ID):
|
|
self.ID = ID
|
|
self.weight = 0
|
|
self.waitTime = 0
|
|
self.serviceTime = 20
|
|
self.trafficLightState = 0
|
|
|
|
Lanes = [Lane(11000), Lane(21000), Lane(31000), Lane(41000), Lane(12000), Lane(22000), Lane(32000), Lane(42000)]
|
|
|
|
secondSelections = [[4, 1, 6], [5, 0, 7], [6, 3, 5], [7, 2, 4], [0, 5, 3], [1, 4, 2], [2, 7, 0], [3, 6, 1]]
|
|
|
|
while True:
|
|
os.system("clear")
|
|
res = requests.get("http://cloud_computing_project_api:8080/sensors/read")
|
|
|
|
sensorValues = json.loads(res.text)
|
|
|
|
sum = [0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
for i in range(8):
|
|
for j in range(5):
|
|
#print(sensorValues[i*5+j]["value"])
|
|
sum[i] += sensorValues[i*5+j]["value"]
|
|
print("")
|
|
|
|
|
|
firstPriority = 0
|
|
secondPriority = 0
|
|
firstPrioritySet = False
|
|
secondPrioritySet = False
|
|
togglingOrangeLight = False
|
|
|
|
for i in range(8):
|
|
Lanes[i].weight = sum[i] * (1 + Lanes[i].waitTime / 40 + 20 / Lanes[i].serviceTime)
|
|
if(Lanes[i].weight != 0):
|
|
firstPrioritySet = True
|
|
|
|
if(Lanes[i].weight > Lanes[firstPriority].weight):
|
|
firstPriority = i
|
|
|
|
print("weight[" + str(i) + "]= " + str(Lanes[i].weight))
|
|
|
|
if firstPrioritySet:
|
|
secondPriority = secondSelections[firstPriority][0]
|
|
|
|
if Lanes[secondPriority].weight != 0:
|
|
secondPrioritySet = True
|
|
|
|
|
|
for i in range(1, 3):
|
|
if Lanes[secondSelections[firstPriority][i]].weight > Lanes[secondPriority].weight:
|
|
secondPriority = secondSelections[firstPriority][i]
|
|
secondPrioritySet = True
|
|
|
|
|
|
for i in range(8):
|
|
if i != firstPriority and ((not secondPrioritySet) or i != secondPriority) and Lanes[i].trafficLightState == 1:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=" + str(Lanes[i].ID) +"&redLight=0&orangeLight=1&greenLight=0")
|
|
togglingOrangeLight = True
|
|
|
|
if togglingOrangeLight:
|
|
#time.sleep(6)
|
|
time.sleep(12)
|
|
|
|
for i in range(8):
|
|
if i != firstPriority and ((not secondPrioritySet) or i != secondPriority) and Lanes[i].trafficLightState == 1:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=" + str(Lanes[i].ID) +"&redLight=1&orangeLight=0&greenLight=0")
|
|
Lanes[i].trafficLightState = 0
|
|
|
|
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=" + str(Lanes[firstPriority].ID) +"&redLight=0&orangeLight=0&greenLight=1")
|
|
Lanes[firstPriority].trafficLightState = 1
|
|
Lanes[firstPriority].serviceTime = 1
|
|
|
|
if secondPrioritySet:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=" + str(Lanes[secondPriority].ID) +"&redLight=0&orangeLight=0&greenLight=1")
|
|
Lanes[secondPriority].trafficLightState = 1
|
|
Lanes[secondPriority].serviceTime = 1
|
|
|
|
|
|
else:
|
|
for i in range(8):
|
|
if Lanes[i].trafficLightState == 1:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=" + str(Lanes[i].ID) +"&redLight=0&orangeLight=1&greenLight=0")
|
|
togglingOrangeLight = True
|
|
|
|
if togglingOrangeLight:
|
|
#time.sleep(6)
|
|
time.sleep(12)
|
|
|
|
for i in range(8):
|
|
if Lanes[i].trafficLightState == 1:
|
|
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=" + str(Lanes[i].ID) +"&redLight=1&orangeLight=0&greenLight=0")
|
|
Lanes[i].trafficLightState = 0
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
for i in range(8):
|
|
if Lanes[i].trafficLightState == 0 and Lanes[i].weight != 0:
|
|
Lanes[i].waitTime += 1
|
|
Lanes[i].serviceTime = 20
|
|
elif Lanes[i].trafficLightState == 1:
|
|
Lanes[i].waitTime = 0
|
|
Lanes[i].serviceTime += 1
|
|
else:
|
|
Lanes[i].waitTime = 0
|
|
Lanes[i].serviceTime = 20
|
|
|