Browse Source

changes

master
cse242017107 2 years ago
parent
commit
0039a27286
  1. 15
      INSTALL.md
  2. 2
      README.md
  3. 14
      api/Dockerfile
  4. 7
      api/package.json
  5. 290
      api/src/index.js
  6. 8
      coordinator/Dockerfile
  7. 133
      coordinator/src/coordinator.py
  8. 236
      coordinator/src/new_coordinator.py
  9. 6
      entry_handler/package.json
  10. 39
      entry_handler/src/index.js
  11. 8
      sensors/Dockerfile
  12. 24
      sensors/src/sensor.py
  13. 122
      start.sh
  14. 4
      stop.sh
  15. 8
      traffic_lights/Dockerfile
  16. 15
      traffic_lights/src/traffic_light.py
  17. 8
      vehicles/Dockerfile
  18. 66
      vehicles/src/vehicle.py
  19. 4
      web_server/Dockerfile
  20. BIN
      web_server/src/images/street.png
  21. BIN
      web_server/src/images/vehicle1.png
  22. BIN
      web_server/src/images/vehicle10.png
  23. BIN
      web_server/src/images/vehicle11.png
  24. BIN
      web_server/src/images/vehicle12.png
  25. BIN
      web_server/src/images/vehicle13.png
  26. BIN
      web_server/src/images/vehicle14.png
  27. BIN
      web_server/src/images/vehicle15.png
  28. BIN
      web_server/src/images/vehicle16.png
  29. BIN
      web_server/src/images/vehicle17.png
  30. BIN
      web_server/src/images/vehicle18.png
  31. BIN
      web_server/src/images/vehicle19.png
  32. BIN
      web_server/src/images/vehicle2.png
  33. BIN
      web_server/src/images/vehicle20.png
  34. BIN
      web_server/src/images/vehicle21.png
  35. BIN
      web_server/src/images/vehicle22.png
  36. BIN
      web_server/src/images/vehicle23.png
  37. BIN
      web_server/src/images/vehicle24.png
  38. BIN
      web_server/src/images/vehicle3.png
  39. BIN
      web_server/src/images/vehicle4.png
  40. BIN
      web_server/src/images/vehicle5.png
  41. BIN
      web_server/src/images/vehicle6.png
  42. BIN
      web_server/src/images/vehicle7.png
  43. BIN
      web_server/src/images/vehicle8.png
  44. BIN
      web_server/src/images/vehicle9.png
  45. 156
      web_server/src/index.css
  46. 91
      web_server/src/index.html
  47. 602
      web_server/src/index.js

15
INSTALL.md

@ -0,0 +1,15 @@
# Οδηγίες Εγκατάστασης-Εκτέλεσης Εφαρμογής<br/>
Για την εκτέλεση της εφαρμογής είναι απαραίτητο να είναι εγκατεστημένο στο σύστημα του χρήστη το περιβάλλον εκτέλεσης Node js και το npm καθώς εκτελείται πρόγραμμα Node js στο επίπεδο του host(εκτός docker) με σκοπό την
δημιουργία container οχημάτων ανάλογα με την είσοδο του χρήστη μέσω της web εφαρμογής.
Η εφαρμογή εκκινεί με την εκτέλεση του script "start.sh". Το script "start.sh" αρχικά δημιουργεί τα docker images των κόμβων με βάση τα αντίστοιχα Dockerfile και το δίκτυο των container. Στην συνέχεια δημιουργεί ένα container για τις βάσεις
δεδομένων, ένα container για το REST API, 8 containers για τους φωτεινούς σηματοδότες, 40 containers για τους αισθητήρες κίνησης, ένα container για τον κόμβο συντονιστή και ένα container για τον web server. Τέλος εγκαθιστά τα κατάλληλα modules
για το πρόγραμμα(entry handler) που εκτελείται στο επίπεδο του host και το θέτει σε λειτουργία αναμένοντας αιτήματα από τον χρήστη. Μεταβαίνοντας στην διεύθυνση 127.0.0.1:8080 ο χρήστης μπορεί να εισάγει νέα οχήματα και να παρατηρεί την κατάσταση του έξυπνου οδικού δικτύου.
Η εφαρμογή σταματάει με την εκτέλεση του script "stop.sh". Το script "stop.sh" σταματάει τα container που δημιουργήθηκαν και στην συνέχεια τα διαγράφει. Ο χρήστης μπορεί να σταματήσει την εκτέλεση του προγράμματος(entry handler) που εκτελείται
στο επίπεδο του host κλείνοντας το τερματικό στο οποίο εκτελείται το script "start.sh"(ή πατώντας τον συνδυασμό πλήκτρων CTRL+C).
Είναι σημαντικό να σημειωθεί ότι λόγω του πλήθους των container και των αιτημάτων που πραγματοποιούνται στο REST API, η εφαρμογή καταναλώνει αρκετή επεξεργαστική ισχύ. Επομένως προτείνεται σε χρήστες που έχουν περιορισμένη επεξεργαστική ισχύ
να αυξήσουν τον χρόνο αναμονής(να διπλασιάσουν το όρισμα της μεθόδου time.sleep) στους κόμβους αισθητήρων κίνησης(αρχείο sensor.py) και στους κόμβους οχημάτων(αρχείο vehicle.py) πριν θέσουν σε λειτουργία την εφαρμογή. Αυτό θα έχει σαν αποτέλεσμα οι αισθητήρες κίνησης να δειγματοληπτούν σε μικρότερο βαθμό και τα οχήματα να κινούνται με μικρότερη ταχύτητα.

2
README.md

@ -1,4 +1,4 @@
# Υπολογιστική Νέφους και Υπηρεσίες - Εργασία Εξαμήνου<br/>
# Οδηγίες Εγκατάστασης-Εκτέλεσης Εφαρμογής<br/>
## Συγγραφείς

14
api/Dockerfile

@ -0,0 +1,14 @@
FROM node:18-alpine
WORKDIR /app
COPY ["api/package.json", "./"]
RUN npm install
WORKDIR /app/src
COPY ["api/src", "./"]
CMD ["node", "."]

7
api/package.json

@ -0,0 +1,7 @@
{
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"mongodb": "4.4"
}
}

290
api/src/index.js

@ -0,0 +1,290 @@
const app = require("express")();
const cors = require("cors");
const mongo = require("mongodb").MongoClient;
const ObjectId = require("mongodb").ObjectId;
const PORT = 8080;
const database = "mongodb://cloud_computing_project_db:27017/";
let traffic_lights_db;
let sensors_db;
let vehicles_db;
mongo.connect(database, function(err, db) {
if(err) throw err;
traffic_lights_db = db.db("traffic_lights_db");
sensors_db = db.db("sensors_db");
vehicles_db = db.db("vehicles_db");
})
app.use(cors());
app.listen(
PORT,
() => console.log("API up and running")
);
app.get("/:database/subscribe", (req, res) => {
if(req.params.database === "traffic_lights")
{
if(req.query.road === undefined)
{
res.status(404).send("Not found");
}
else
{
let obj = { road: +req.query.road, redLight: 1, orangeLight: 0, greenLight: 0 };
traffic_lights_db.collection("traffic_lights").insertOne(obj, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else if(req.params.database === "sensors")
{
if(req.query.position === undefined)
{
res.status(404).send("Not found");
}
else
{
let obj = { position: +req.query.position, value: 0 };
sensors_db.collection("sensors").insertOne(obj, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else if(req.params.database === "vehicles")
{
if(req.query.start === undefined || req.query.destination === undefined)
{
res.status(404).send("Not found");
}
else
{
let obj = { start: +req.query.start, destination: +req.query.destination, position: +req.query.start };
vehicles_db.collection("vehicles").insertOne(obj, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else
{
res.status(404).send("Not found");
}
});
app.get("/:database/update", (req, res) => {
if(req.params.database === "traffic_lights")
{
if(req.query.road === undefined || req.query.redLight === undefined || req.query.orangeLight === undefined || req.query.greenLight === undefined)
{
res.status(404).send("Not found");
}
else
{
let query = { road: +req.query.road };
let values = { $set: { redLight: +req.query.redLight, orangeLight: +req.query.orangeLight, greenLight: +req.query.greenLight } };
traffic_lights_db.collection("traffic_lights").updateOne(query, values, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else if(req.params.database === "sensors")
{
if(req.query.id === undefined || req.query.value === undefined)
{
res.status(404).send("Not found");
}
else
{
let objectId = new ObjectId(req.query.id);
let query = { _id: objectId };
let values = { $set: { value: +req.query.value } };
sensors_db.collection("sensors").updateOne(query, values, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else if(req.params.database === "vehicles")
{
if(req.query.id === undefined || req.query.position === undefined)
{
res.status(404).send("Not found");
}
else
{
let objectId = new ObjectId(req.query.id);
let query = { _id: objectId };
let values = { $set: { position: +req.query.position } };
vehicles_db.collection("vehicles").updateOne(query, values, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else
{
res.status(404).send("Not found");
}
});
app.get("/:database/read", (req, res) => {
if(req.params.database === "traffic_lights")
{
if(req.query.id === undefined)
{
traffic_lights_db.collection("traffic_lights").find({}).toArray(function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
else
{
let objectId = new ObjectId(req.query.id);
let query = { _id: objectId };
traffic_lights_db.collection("traffic_lights").findOne(query, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else if(req.params.database === "sensors")
{
sensors_db.collection("sensors").find({}).toArray(function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
else if(req.params.database === "vehicles")
{
vehicles_db.collection("vehicles").find({}).toArray(function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
else
{
res.status(404).send("Not found");
}
});
app.get("/:database/discover", (req, res) => {
if(req.params.database === "vehicles")
{
if(req.query.position === undefined)
{
res.status(404).send("Not found");
}
else
{
rangeStart = req.query.position - 8;
rangeEnd = +req.query.position;
let query = { position: { $gte: rangeStart, $lte: rangeEnd} };
vehicles_db.collection("vehicles").findOne(query, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else
{
res.status(404).send("Not found");
}
});
app.get("/:database/look", (req, res) => {
if(req.params.database === "traffic_lights")
{
if(req.query.road === undefined)
{
res.status(404).send("Not found");
}
else
{
let query = { road: +req.query.road};
traffic_lights_db.collection("traffic_lights").findOne(query, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else if(req.params.database === "vehicles")
{
if(req.query.position === undefined)
{
res.status(404).send("Not found");
}
else
{
rangeStart = +req.query.position;
rangeEnd = +req.query.position + 11;
let query = { position: { $gt: rangeStart, $lte: rangeEnd} };
vehicles_db.collection("vehicles").findOne(query, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else
{
res.status(404).send("Not found");
}
});
app.get("/:database/delete", (req, res) => {
if(req.params.database === "vehicles")
{
if(req.query.id === undefined)
{
res.status(404).send("Not found");
}
else
{
let objectId = new ObjectId(req.query.id);
let query = { _id: objectId };
vehicles_db.collection("vehicles").deleteOne(query, function(err, result) {
if(err) throw err;
res.status(200).send(result);
});
}
}
else
{
res.status(404).send("Not found");
}
});

8
coordinator/Dockerfile

@ -0,0 +1,8 @@
FROM python:3-alpine
WORKDIR /app/src
RUN pip install requests
COPY ["coordinator/src", "./"]

133
coordinator/src/coordinator.py

@ -0,0 +1,133 @@
import requests
import time
import json
class Lane:
def __init__(self, ID):
self.ID = ID
self.weight = 0
self.waitTime = 0
#self.serviceTime = 20
self.serviceTime = 40
self.trafficLightState = 0
self.timer = -1
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:
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]
firstPriority = 0
secondPriority = 0
firstPrioritySet = False
secondPrioritySet = False
for i in range(8):
for j in range(5):
sum[i] += sensorValues[i*5+j]["value"]
#Lanes[i].weight = sum[i] * (1 + Lanes[i].waitTime / 40 + 20 / Lanes[i].serviceTime)
Lanes[i].weight = sum[i] * (1 + Lanes[i].waitTime / 40 + 40 / Lanes[i].serviceTime)
if(Lanes[i].weight != 0):
firstPrioritySet = True
if(Lanes[i].weight > Lanes[firstPriority].weight):
firstPriority = i
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 == 2:
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=" + str(Lanes[i].ID) +"&redLight=0&orangeLight=1&greenLight=0")
Lanes[i].trafficLightState = 1
Lanes[i].timer = 12
elif Lanes[i].trafficLightState == 1 and Lanes[i].timer == 0:
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
Lanes[i].timer = -1
orangeLightIndex = -1
orangeLightCounter = 0
for i in range(8):
if Lanes[i].trafficLightState == 1:
orangeLightIndex = i
orangeLightCounter += 1
valid = False
if orangeLightIndex != -1:
for i in range(3):
if secondSelections[orangeLightIndex][i] == firstPriority:
valid = True
break
if orangeLightCounter < 2 and Lanes[firstPriority].trafficLightState == 0 and (orangeLightIndex == -1 or valid):
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 = 2
Lanes[firstPriority].serviceTime = 1
if secondPrioritySet and orangeLightCounter < 1 and Lanes[secondPriority].trafficLightState == 0:
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 = 2
Lanes[secondPriority].serviceTime = 1
else:
for i in range(8):
if Lanes[i].trafficLightState == 2:
requests.get("http://cloud_computing_project_api:8080/traffic_lights/update?road=" + str(Lanes[i].ID) +"&redLight=0&orangeLight=1&greenLight=0")
Lanes[i].trafficLightState = 1
Lanes[i].timer = 12
elif Lanes[i].trafficLightState == 1 and Lanes[i].timer == 0:
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
Lanes[i].timer = -1
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
Lanes[i].serviceTime = 40
elif Lanes[i].trafficLightState == 2:
Lanes[i].waitTime = 0
Lanes[i].serviceTime += 1
else:
Lanes[i].waitTime = 0
#Lanes[i].serviceTime = 20
Lanes[i].serviceTime = 40
if Lanes[i].timer > 0:
Lanes[i].timer -= 1

236
coordinator/src/new_coordinator.py

@ -0,0 +1,236 @@
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

6
entry_handler/package.json

@ -0,0 +1,6 @@
{
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2"
}
}

39
entry_handler/src/index.js

@ -0,0 +1,39 @@
const app = require("express")();
const cors = require("cors");
const { exec } = require("child_process");
const PORT = 8082;
let vehicleNumber = 0;
app.use(cors());
app.listen(
PORT,
() => console.log("Entry handler up and running")
);
app.get("/insert", (req, res) => {
if(req.query.start === undefined || req.query.destination === undefined)
{
res.status(404).send("Not found");
}
else
{
vehicleNumber++;
let command = "docker run -d --network cloud_computing_project --name vehicle" + vehicleNumber + " cloud_computing_project_vehicle python vehicle.py " + req.query.start + " " + req.query.destination;
exec(command, (err, stdout, stderr) => {
let id = stdout.substring(0, stdout.length - 1);
console.log("New container created with id " + id + ". Vehicle number " + vehicleNumber + " with start " + req.query.start + " and destination " + req.query.destination);
console.log(stderr);
if(err != null)
{
console.log(err);
}
});
res.status(200).send("Ok");
}
});

8
sensors/Dockerfile

@ -0,0 +1,8 @@
FROM python:3-alpine
WORKDIR /app/src
RUN pip install requests
COPY ["sensors/src", "./"]

24
sensors/src/sensor.py

@ -0,0 +1,24 @@
import requests
import time
import json
import sys
position = int(sys.argv[1])
value = 0
res = requests.get("http://cloud_computing_project_api:8080/sensors/subscribe?position=" + str(position))
ID = json.loads(res.text)["insertedId"]
while True:
res = requests.get("http://cloud_computing_project_api:8080/vehicles/discover?position=" + str(position))
if res.text != "" and value == 0:
value = 1
res = requests.get("http://cloud_computing_project_api:8080/sensors/update?id=" + str(ID) + "&value=1")
elif res.text == "" and value == 1:
value = 0
res = requests.get("http://cloud_computing_project_api:8080/sensors/update?id=" + str(ID) + "&value=0")
time.sleep(0.1)

122
start.sh

@ -0,0 +1,122 @@
#!/bin/bash
if ! [ $(docker image ls | grep cloud_computing_project_api | cut -d ' ' -f 1) ]
then
docker build -f api/Dockerfile -t cloud_computing_project_api .
fi
if ! [ $(docker image ls | grep cloud_computing_project_traffic_light | cut -d ' ' -f 1) ]
then
docker build -f traffic_lights/Dockerfile -t cloud_computing_project_traffic_light .
fi
if ! [ $(docker image ls | grep cloud_computing_project_sensor | cut -d ' ' -f 1) ]
then
docker build -f sensors/Dockerfile -t cloud_computing_project_sensor .
fi
if ! [ $(docker image ls | grep cloud_computing_project_vehicle | cut -d ' ' -f 1) ]
then
docker build -f vehicles/Dockerfile -t cloud_computing_project_vehicle .
fi
if ! [ $(docker image ls | grep cloud_computing_project_coordinator | cut -d ' ' -f 1) ]
then
docker build -f coordinator/Dockerfile -t cloud_computing_project_coordinator .
fi
if ! [ $(docker image ls | grep cloud_computing_project_web_server | cut -d ' ' -f 1) ]
then
docker build -f web_server/Dockerfile -t cloud_computing_project_web_server .
fi
if ! [ $(docker network ls | grep cloud_computing_project | cut -d ' ' -f 1) ]
then
docker network create cloud_computing_project
fi
docker run -d --network cloud_computing_project --network-alias cloud_computing_project_db --name cloud_computing_project_db mongo:4.4
docker run -dp 8081:8080 --network cloud_computing_project --network-alias cloud_computing_project_api cloud_computing_project_api
docker run -d --network cloud_computing_project cloud_computing_project_traffic_light python traffic_light.py 11000
docker run -d --network cloud_computing_project cloud_computing_project_traffic_light python traffic_light.py 21000
docker run -d --network cloud_computing_project cloud_computing_project_traffic_light python traffic_light.py 31000
docker run -d --network cloud_computing_project cloud_computing_project_traffic_light python traffic_light.py 41000
docker run -d --network cloud_computing_project cloud_computing_project_traffic_light python traffic_light.py 12000
docker run -d --network cloud_computing_project cloud_computing_project_traffic_light python traffic_light.py 22000
docker run -d --network cloud_computing_project cloud_computing_project_traffic_light python traffic_light.py 32000
docker run -d --network cloud_computing_project cloud_computing_project_traffic_light python traffic_light.py 42000
docker run -d --network cloud_computing_project --name sensor1_1_1 cloud_computing_project_sensor python sensor.py 11008
docker run -d --network cloud_computing_project --name sensor1_1_2 cloud_computing_project_sensor python sensor.py 11017
docker run -d --network cloud_computing_project --name sensor1_1_3 cloud_computing_project_sensor python sensor.py 11026
docker run -d --network cloud_computing_project --name sensor1_1_4 cloud_computing_project_sensor python sensor.py 11035
docker run -d --network cloud_computing_project --name sensor1_1_5 cloud_computing_project_sensor python sensor.py 11044
docker run -d --network cloud_computing_project --name sensor2_1_1 cloud_computing_project_sensor python sensor.py 21008
docker run -d --network cloud_computing_project --name sensor2_1_2 cloud_computing_project_sensor python sensor.py 21017
docker run -d --network cloud_computing_project --name sensor2_1_3 cloud_computing_project_sensor python sensor.py 21026
docker run -d --network cloud_computing_project --name sensor2_1_4 cloud_computing_project_sensor python sensor.py 21035
docker run -d --network cloud_computing_project --name sensor2_1_5 cloud_computing_project_sensor python sensor.py 21044
docker run -d --network cloud_computing_project --name sensor3_1_1 cloud_computing_project_sensor python sensor.py 31008
docker run -d --network cloud_computing_project --name sensor3_1_2 cloud_computing_project_sensor python sensor.py 31017
docker run -d --network cloud_computing_project --name sensor3_1_3 cloud_computing_project_sensor python sensor.py 31026
docker run -d --network cloud_computing_project --name sensor3_1_4 cloud_computing_project_sensor python sensor.py 31035
docker run -d --network cloud_computing_project --name sensor3_1_5 cloud_computing_project_sensor python sensor.py 31044
docker run -d --network cloud_computing_project --name sensor4_1_1 cloud_computing_project_sensor python sensor.py 41008
docker run -d --network cloud_computing_project --name sensor4_1_2 cloud_computing_project_sensor python sensor.py 41017
docker run -d --network cloud_computing_project --name sensor4_1_3 cloud_computing_project_sensor python sensor.py 41026
docker run -d --network cloud_computing_project --name sensor4_1_4 cloud_computing_project_sensor python sensor.py 41035
docker run -d --network cloud_computing_project --name sensor4_1_5 cloud_computing_project_sensor python sensor.py 41044
docker run -d --network cloud_computing_project --name sensor1_2_1 cloud_computing_project_sensor python sensor.py 12008
docker run -d --network cloud_computing_project --name sensor1_2_2 cloud_computing_project_sensor python sensor.py 12017
docker run -d --network cloud_computing_project --name sensor1_2_3 cloud_computing_project_sensor python sensor.py 12026
docker run -d --network cloud_computing_project --name sensor1_2_4 cloud_computing_project_sensor python sensor.py 12035
docker run -d --network cloud_computing_project --name sensor1_2_5 cloud_computing_project_sensor python sensor.py 12044
docker run -d --network cloud_computing_project --name sensor2_2_1 cloud_computing_project_sensor python sensor.py 22008
docker run -d --network cloud_computing_project --name sensor2_2_2 cloud_computing_project_sensor python sensor.py 22017
docker run -d --network cloud_computing_project --name sensor2_2_3 cloud_computing_project_sensor python sensor.py 22026
docker run -d --network cloud_computing_project --name sensor2_2_4 cloud_computing_project_sensor python sensor.py 22035
docker run -d --network cloud_computing_project --name sensor2_2_5 cloud_computing_project_sensor python sensor.py 22044
docker run -d --network cloud_computing_project --name sensor3_2_1 cloud_computing_project_sensor python sensor.py 32008
docker run -d --network cloud_computing_project --name sensor3_2_2 cloud_computing_project_sensor python sensor.py 32017
docker run -d --network cloud_computing_project --name sensor3_2_3 cloud_computing_project_sensor python sensor.py 32026
docker run -d --network cloud_computing_project --name sensor3_2_4 cloud_computing_project_sensor python sensor.py 32035
docker run -d --network cloud_computing_project --name sensor3_2_5 cloud_computing_project_sensor python sensor.py 32044
docker run -d --network cloud_computing_project --name sensor4_2_1 cloud_computing_project_sensor python sensor.py 42008
docker run -d --network cloud_computing_project --name sensor4_2_2 cloud_computing_project_sensor python sensor.py 42017
docker run -d --network cloud_computing_project --name sensor4_2_3 cloud_computing_project_sensor python sensor.py 42026
docker run -d --network cloud_computing_project --name sensor4_2_4 cloud_computing_project_sensor python sensor.py 42035
docker run -d --network cloud_computing_project --name sensor4_2_5 cloud_computing_project_sensor python sensor.py 42044
docker run -d --network cloud_computing_project --name coordinator cloud_computing_project_coordinator python coordinator.py
docker run -dp 8080:80 --network cloud_computing_project --name web_server cloud_computing_project_web_server
cd entry_handler
npm install
cd src
node .

4
stop.sh

@ -0,0 +1,4 @@
#!/bin/bash
docker stop $(docker ps | grep cloud_computing_project | cut -d ' ' -f 1)
docker rm $(docker ps -a | grep cloud_computing_project | cut -d ' ' -f 1)

8
traffic_lights/Dockerfile

@ -0,0 +1,8 @@
FROM python:3-alpine
WORKDIR /app/src
RUN pip install requests
COPY ["traffic_lights/src", "./"]

15
traffic_lights/src/traffic_light.py

@ -0,0 +1,15 @@
import requests
import time
import json
import sys
road = int(sys.argv[1])
res = requests.get("http://cloud_computing_project_api:8080/traffic_lights/subscribe?road=" + str(road))
ID = json.loads(res.text)["insertedId"]
while True:
res = requests.get("http://cloud_computing_project_api:8080/traffic_lights/read?id=" + str(ID))
time.sleep(1)

8
vehicles/Dockerfile

@ -0,0 +1,8 @@
FROM python:3-alpine
WORKDIR /app/src
RUN pip install requests
COPY ["vehicles/src", "./"]

66
vehicles/src/vehicle.py

@ -0,0 +1,66 @@
import requests
import time
import json
import sys
start = int(sys.argv[1])
destination = int(sys.argv[2])
position = start
road = start
route = (start % 10000) / 1000
res = requests.get("http://cloud_computing_project_api:8080/vehicles/subscribe?start=" + str(start) + "&destination=" + str(destination))
ID = json.loads(res.text)["insertedId"]
while position != destination:
res = requests.get("http://cloud_computing_project_api:8080/vehicles/look?position=" + str(position))
if res.text == "":
if route == 1:
if position > (start + 44):
position += 1
requests.get("http://cloud_computing_project_api:8080/vehicles/update?id=" + str(ID) + "&position=" + str(position))
else:
res = requests.get("http://cloud_computing_project_api:8080/traffic_lights/look?road=" + str(road))
greenLight = json.loads(res.text)["greenLight"]
if greenLight == 1 or position < (start + 44):
position += 1
requests.get("http://cloud_computing_project_api:8080/vehicles/update?id=" + str(ID) + "&position=" + str(position))
elif route == 2:
if position > (start + 44):
if position == (start + 85):
position = destination - 71
else:
position += 1
requests.get("http://cloud_computing_project_api:8080/vehicles/update?id=" + str(ID) + "&position=" + str(position))
else:
res = requests.get("http://cloud_computing_project_api:8080/traffic_lights/look?road=" + str(road))
greenLight = json.loads(res.text)["greenLight"]
if greenLight == 1 or position < (start + 44):
position += 1
requests.get("http://cloud_computing_project_api:8080/vehicles/update?id=" + str(ID) + "&position=" + str(position))
elif(route == 3):
if position == (start + 56):
position = destination - 60
else:
position += 1
requests.get("http://cloud_computing_project_api:8080/vehicles/update?id=" + str(ID) + "&position=" + str(position))
time.sleep(0.2)
requests.get("http://cloud_computing_project_api:8080/vehicles/delete?id=" + str(ID))

4
web_server/Dockerfile

@ -0,0 +1,4 @@
FROM nginx:1.25-alpine
COPY ["web_server/src", "/usr/share/nginx/html"]

BIN
web_server/src/images/street.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
web_server/src/images/vehicle1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 560 KiB

BIN
web_server/src/images/vehicle10.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 532 KiB

BIN
web_server/src/images/vehicle11.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 545 KiB

BIN
web_server/src/images/vehicle12.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 537 KiB

BIN
web_server/src/images/vehicle13.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 541 KiB

BIN
web_server/src/images/vehicle14.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 525 KiB

BIN
web_server/src/images/vehicle15.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 523 KiB

BIN
web_server/src/images/vehicle16.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 554 KiB

BIN
web_server/src/images/vehicle17.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 563 KiB

BIN
web_server/src/images/vehicle18.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 556 KiB

BIN
web_server/src/images/vehicle19.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 528 KiB

BIN
web_server/src/images/vehicle2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 491 KiB

BIN
web_server/src/images/vehicle20.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 523 KiB

BIN
web_server/src/images/vehicle21.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 554 KiB

BIN
web_server/src/images/vehicle22.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 563 KiB

BIN
web_server/src/images/vehicle23.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 556 KiB

BIN
web_server/src/images/vehicle24.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 527 KiB

BIN
web_server/src/images/vehicle3.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 KiB

BIN
web_server/src/images/vehicle4.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 KiB

BIN
web_server/src/images/vehicle5.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 532 KiB

BIN
web_server/src/images/vehicle6.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 546 KiB

BIN
web_server/src/images/vehicle7.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 538 KiB

BIN
web_server/src/images/vehicle8.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 541 KiB

BIN
web_server/src/images/vehicle9.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 526 KiB

156
web_server/src/index.css

@ -0,0 +1,156 @@
body {
background-image: url("images/street.png");
background-size: cover;
}
#prompt {
position: fixed;
left: 280px;
top: 50px;
}
#route {
position: fixed;
left: 245px;
top: 120px;
}
div {
border: 5px solid black;
width: 30px;
/*position: absolute;*/
position: fixed;
display: inline-block;
background-color: black;
}
#trafficLight1 {
/*left: 39%;
top: 59%;*/
left: 689px;
top: 692px;
width: 90px;
}
#trafficLight2 {
/*left: 56%;
top: 33%;*/
left: 1130px;
top: 240px;
width: 90px;
}
#trafficLight3 {
/*left: 42%;
top: 28%;*/
left: 707px;
top: 226px;
}
#trafficLight4 {
/*left: 56%;
top: 59%;*/
left: 1172px;
top: 655px;
}
#trafficLight5 {
/*left: 39%;
top: 59%;*/
left: 689px;
top: 655px;
width: 90px;
}
#trafficLight6 {
/*left: 56%;
top: 33%;*/
left: 1130px;
top: 278px;
width: 90px;
}
#trafficLight7 {
/*left: 42%;
top: 28%;*/
left: 749px;
top: 226px;
}
#trafficLight8 {
/*left: 56%;
top: 59%;*/
left: 1130px;
top: 655px;
}
#trafficLight9 {
/*left: 39%;
top: 59%;*/
left: 689px;
top: 730px;
width: 90px;
}
#trafficLight10 {
/*left: 56%;
top: 33%;*/
left: 1130px;
top: 203px;
width: 90px;
}
#trafficLight11 {
/*left: 42%;
top: 28%;*/
left: 665px;
top: 226px;
}
#trafficLight12 {
/*left: 56%;
top: 59%;*/
left: 1214px;
top: 655px;
}
button {
border-radius: 80%;
height: 25px;
width: 25px;
display: inline-block;
position: relative;
/*left: 8%;*/
left: 4%;
}
#trafficLight3 button, #trafficLight4 button, #trafficLight7 button, #trafficLight8 button, #trafficLight11 button {
left: 9%;
}
.trafficLights {
border: 2px solid grey;
background-color: grey;
}
#insert {
position: fixed;
border-radius: 10%;
height: 30px;
width: 160px;
left: 265px;
top: 160px;
}
.arrows {
color: grey;
font-size: 16px;
}
#orangeLight9 {
color: rgb(255, 115, 0);
}

91
web_server/src/index.html

@ -0,0 +1,91 @@
<!DOCTYPE html>
<html>
<head>
<title>Cloud Computing Project</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="index.css">
</head>
<body>
<main>
<h2 id="prompt">Select route</h2>
<select id="route">
<option value="west-east">Start: West, Destination: East</option>
<option value="west-north">Start: West, Destination: North</option>
<option value="west-south">Start: West, Destination: South</option>
<option value="east-west">Start: East, Destination: West</option>
<option value="east-south">Start: East, Destination: South</option>
<option value="east-north">Start: East, Destination: North</option>
<option value="north-south">Start: North, Destination: South</option>
<option value="north-east">Start: North, Destination: East</option>
<option value="north-west">Start: North, Destination: West</option>
<option value="south-north">Start: South, Destination: North</option>
<option value="south-west">Start: South, Destination: West</option>
<option value="south-east">Start: South, Destination: East</option>
</select>
<button id="insert">Insert Vehicle</button>
<div id="trafficLight1">
<button class="trafficLights" id="greenLight1"></button>
<button class="trafficLights" id="orangeLight1"></button>
<button class="trafficLights" id="redLight1"></button>
</div>
<div id="trafficLight2">
<button class="trafficLights" id="redLight2"></button>
<button class="trafficLights" id="orangeLight2"></button>
<button class="trafficLights" id="greenLight2"></button>
</div>
<div id="trafficLight3">
<button class="trafficLights" id="greenLight3"></button>
<button class="trafficLights" id="orangeLight3"></button>
<button class="trafficLights" id="redLight3"></button>
</div>
<div id="trafficLight4">
<button class="trafficLights" id="redLight4"></button>
<button class="trafficLights" id="orangeLight4"></button>
<button class="trafficLights" id="greenLight4"></button>
</div>
<div id="trafficLight5">
<button class="trafficLights"><span class="arrows" id="greenLight5">&uarr;</span></button>
<button class="trafficLights"><span class="arrows" id="orangeLight5">&uarr;</span></button>
<button class="trafficLights"><span class="arrows" id="redLight5">&uarr;</span></button>
</div>
<div id="trafficLight6">
<button class="trafficLights"><span class="arrows" id="redLight6">&darr;</span></button>
<button class="trafficLights"><span class="arrows" id="orangeLight6">&darr;</span></button>
<button class="trafficLights"><span class="arrows" id="greenLight6">&darr;</span></button>
</div>
<div id="trafficLight7">
<button class="trafficLights"><span class="arrows" id="greenLight7">&rarr;</span></button>
<button class="trafficLights"><span class="arrows" id="orangeLight7">&rarr;</span></button>
<button class="trafficLights"><span class="arrows" id="redLight7">&rarr;</span></button>
</div>
<div id="trafficLight8">
<button class="trafficLights"><span class="arrows" id="redLight8">&larr;</span></button>
<button class="trafficLights"><span class="arrows" id="orangeLight8">&larr;</span></button>
<button class="trafficLights"><span class="arrows" id="greenLight8">&larr;</span></button>
</div>
<div id="trafficLight9">
<button class="trafficLights"><span class="arrows" id="greenLight9">&darr;</span></button>
<button class="trafficLights"><span class="arrows" id="orangeLight9">&darr;</span></button>
<button class="trafficLights"><span class="arrows" id="redLight9">&darr;</span></button>
</div>
<div id="trafficLight10">
<button class="trafficLights"><span class="arrows" id="redLight10">&uarr;</span></button>
<button class="trafficLights"><span class="arrows" id="orangeLight10">&uarr;</span></button>
<button class="trafficLights"><span class="arrows" id="greenLight10">&uarr;</span></button>
</div>
<div id="trafficLight11">
<button class="trafficLights"><span class="arrows" id="greenLight11">&larr;</span></button>
<button class="trafficLights"><span class="arrows" id="orangeLight11">&larr;</span></button>
<button class="trafficLights"><span class="arrows" id="redLight11">&larr;</span></button>
</div>
<div id="trafficLight12">
<button class="trafficLights"><span class="arrows" id="redLight12">&rarr;</span></button>
<button class="trafficLights"><span class="arrows" id="orangeLight12">&rarr;</span></button>
<button class="trafficLights"><span class="arrows" id="greenLight12">&rarr;</span></button>
</div>
</main>
</body>
<script src="index.js" type="text/javascript"></script>
</html>

602
web_server/src/index.js

@ -0,0 +1,602 @@
document.getElementById("insert").addEventListener("click", function() {
let value = document.getElementById("route").value;
let start;
let destination;
if(value === "west-east")
{
start = 11000;
destination = 11170;
}
else if(value === "west-north")
{
start = 12000;
destination = 42170;
}
else if(value === "west-south")
{
start = 13000;
destination = 33170;
}
else if(value === "east-west")
{
start = 21000;
destination = 21170;
}
else if(value === "east-south")
{
start = 22000;
destination = 32170;
}
else if(value === "east-north")
{
start = 23000;
destination = 43170;
}
else if(value === "north-south")
{
start = 31000;
destination = 31170;
}
else if(value === "north-east")
{
start = 32000;
destination = 12170;
}
else if(value === "north-west")
{
start = 33000;
destination = 23170;
}
else if(value === "south-north")
{
start = 41000;
destination = 41170;
}
else if(value === "south-west")
{
start = 42000;
destination = 22170;
}
else if(value === "south-east")
{
start = 43000;
destination = 13170;
}
let xhttp = new XMLHttpRequest();
xhttp.onload = function() {
}
xhttp.open("GET", "http://127.0.0.1:8082/insert?start=" + start + "&destination=" + destination, true);
xhttp.send();
});
let response1;
let counter = 0;
setInterval(trafficLightsRequest, 1000);
function trafficLightsRequest()
{
let xhttp = new XMLHttpRequest();
xhttp.onload = function() {
response1 = JSON.parse(this.responseText);
updateTrafficLights();
}
xhttp.open("GET", "http://127.0.0.1:8081/traffic_lights/read", true)
xhttp.send();
}
function updateTrafficLights()
{
for(let i = 0; i < 4; i++)
{
if(response1[i].redLight == 1)
{
document.getElementById("redLight" + (i+1)).style.backgroundColor = "rgb(220, 0, 0)";
document.getElementById("redLight" + (i+1)).style.border = "2px solid rgb(220, 0, 0)";
}
else
{
document.getElementById("redLight" + (i+1)).style.backgroundColor = "gray";
document.getElementById("redLight" + (i+1)).style.border = "2px solid gray";
}
if(response1[i].orangeLight == 1)
{
document.getElementById("orangeLight" + (i+1)).style.backgroundColor = "rgb(255, 115, 0)";
document.getElementById("orangeLight" + (i+1)).style.border = "2px solid rgb(255, 115, 0)";
}
else
{
document.getElementById("orangeLight" + (i+1)).style.backgroundColor = "gray";
document.getElementById("orangeLight" + (i+1)).style.border = "2px solid gray";
}
if(response1[i].greenLight == 1)
{
document.getElementById("greenLight" + (i+1)).style.backgroundColor = "rgb(0, 220, 0)";
document.getElementById("greenLight" + (i+1)).style.border = "2px solid rgb(0, 220, 0)";
}
else
{
document.getElementById("greenLight" + (i+1)).style.backgroundColor = "gray";
document.getElementById("greenLight" + (i+1)).style.border = "2px solid gray";
}
}
for(let i = 4; i < response1.length; i++)
{
if(response1[i].redLight == 1)
{
document.getElementById("redLight" + (i+1)).style.color = "rgb(220, 0, 0)";
}
else
{
document.getElementById("redLight" + (i+1)).style.color = "gray";
}
if(response1[i].orangeLight == 1)
{
document.getElementById("orangeLight" + (i+1)).style.color = "rgb(255, 115, 0)";
}
else
{
document.getElementById("orangeLight" + (i+1)).style.color = "gray";
}
if(response1[i].greenLight == 1)
{
document.getElementById("greenLight" + (i+1)).style.color = "rgb(0, 220, 0)";
}
else
{
document.getElementById("greenLight" + (i+1)).style.color = "gray";
}
}
if(counter === 0)
{
document.getElementById("orangeLight9").style.color = "rgb(255, 115, 0)";
document.getElementById("orangeLight10").style.color = "rgb(255, 115, 0)";
document.getElementById("orangeLight11").style.color = "rgb(255, 115, 0)";
document.getElementById("orangeLight12").style.color = "rgb(255, 115, 0)";
counter++;
}
else
{
document.getElementById("orangeLight9").style.color = "gray";
document.getElementById("orangeLight10").style.color = "gray";
document.getElementById("orangeLight11").style.color = "gray";
document.getElementById("orangeLight12").style.color = "gray";
counter = 0;
}
}
let response2;
let vehicleElements = new Array();
let step = 1 / 170 * 100;
setInterval(vehiclesRequest, 100);
function vehiclesRequest()
{
let xhttp = new XMLHttpRequest();
xhttp.onload = function() {
response2 = JSON.parse(this.responseText);
updateVehiclesPosition();
}
xhttp.open("GET", "http://127.0.0.1:8081/vehicles/read", true)
xhttp.send();
}
function updateVehiclesPosition()
{
for(let i = 0; i < response2.length; i++)
{
let tempObj;
let tempElement;
let exists = false;
for(let j = 0; j < vehicleElements.length; j++)
{
if(vehicleElements[j]._id === response2[i]._id)
{
tempElement = vehicleElements[j].element;
exists = true;
break;
}
}
if(!exists)
{
tempElement = document.createElement("img");
tempElement.style.position = "fixed";
tempElement.style.width = "60px";
document.body.appendChild(tempElement);
tempObj = {_id: response2[i]._id, element: tempElement};
vehicleElements.push(tempObj);
}
if(response2[i].position >= 11000 && response2[i].position <= 11170)
{
tempElement.setAttribute("src", "images/vehicle1.png");
tempElement.style.top = "57%";
tempElement.style.left = (24 + step * (response2[i].position - 11000) * 0.52) + "%";
}
else if(response2[i].position >= 12000 && response2[i].position <= 12170)
{
tempElement.style.left = (24 + step * (response2[i].position - 12000) * 0.52) + "%";
if(response2[i].position < 12055 || response2[i].position > 12085)
{
tempElement.setAttribute("src", "images/vehicle1.png");
tempElement.style.top = "51.7%";
}
else if(response2[i].position < 12064)
{
tempElement.setAttribute("src", "images/vehicle24.png");
tempElement.style.top = (50.8 - (0.2 *(response2[i].position - 12055))) + "%";
}
else if(response2[i].position < 12072)
{
tempElement.setAttribute("src", "images/vehicle23.png");
tempElement.style.top = (47.8 - (0.3 *(response2[i].position - 12064))) + "%";
}
else if(response2[i].position < 12079)
{
tempElement.setAttribute("src", "images/vehicle22.png");
tempElement.style.top = (44.2 - (0.4 *(response2[i].position - 12072))) + "%";
}
else if(response2[i].position < 12083)
{
tempElement.setAttribute("src", "images/vehicle21.png");
tempElement.style.top = (40.5 - (0.5 *(response2[i].position - 12079))) + "%";
}
else if(response2[i].position <= 12085)
{
tempElement.setAttribute("src", "images/vehicle20.png");
tempElement.style.top = (37.7 - (0.6 *(response2[i].position - 12083))) + "%";
}
}
else if(response2[i].position >= 13000 && response2[i].position <= 13170)
{
tempElement.style.left = (24 + step * (response2[i].position - 13000) * 0.52) + "%";
if(response2[i].position < 13052 || response2[i].position > 13056)
{
tempElement.setAttribute("src", "images/vehicle1.png");
tempElement.style.top = "62.5%";
}
else if(response2[i].position === 13052)
{
tempElement.setAttribute("src", "images/vehicle5.png");
tempElement.style.top = "62.8%";
}
else if(response2[i].position === 13053)
{
tempElement.setAttribute("src", "images/vehicle6.png");
tempElement.style.top = "62.9%";
}
else if(response2[i].position === 13054)
{
tempElement.setAttribute("src", "images/vehicle7.png");
tempElement.style.top = "63.1%";
}
else if(response2[i].position === 13055)
{
tempElement.setAttribute("src", "images/vehicle8.png");
tempElement.style.top = "63.5%";
}
else if(response2[i].position === 13056)
{
tempElement.setAttribute("src", "images/vehicle9.png");
tempElement.style.top = "64.2%";
}
}
else if(response2[i].position >= 21000 && response2[i].position <= 21170)
{
tempElement.setAttribute("src", "images/vehicle2.png");
tempElement.style.top = "39%";
tempElement.style.right = (24 + step * (response2[i].position - 21000) * 0.52) + "%";
}
else if(response2[i].position >= 22000 && response2[i].position <= 22170)
{
tempElement.style.left = "";
tempElement.style.right = (24 + step * (response2[i].position - 22000) * 0.52) + "%";
if(response2[i].position < 22055 || response2[i].position > 22085)
{
tempElement.setAttribute("src", "images/vehicle2.png");
tempElement.style.top = "44.6%";
}
else if(response2[i].position < 22064)
{
tempElement.setAttribute("src", "images/vehicle19.png");
tempElement.style.top = (44.9 + (0.2 *(response2[i].position - 22055))) + "%";
}
else if(response2[i].position < 22072)
{
tempElement.setAttribute("src", "images/vehicle18.png");
tempElement.style.top = (46.9 + (0.3 *(response2[i].position - 22064))) + "%";
}
else if(response2[i].position < 22079)
{
tempElement.setAttribute("src", "images/vehicle17.png");
tempElement.style.top = (49.5 + (0.4 *(response2[i].position - 22072))) + "%";
}
else if(response2[i].position < 22083)
{
tempElement.setAttribute("src", "images/vehicle16.png");
tempElement.style.top = (52.8 + (0.5 *(response2[i].position - 22079))) + "%";
}
else if(response2[i].position <= 22085)
{
tempElement.setAttribute("src", "images/vehicle15.png");
tempElement.style.top = (55.2 + (0.6 *(response2[i].position - 22083))) + "%";
}
}
else if(response2[i].position >= 23000 && response2[i].position <= 23170)
{
tempElement.style.left = "";
tempElement.style.right = (24 + step * (response2[i].position - 23000) * 0.52) + "%";
if(response2[i].position < 23052 || response2[i].position > 23056)
{
tempElement.setAttribute("src", "images/vehicle2.png");
tempElement.style.top = "33.7%";
}
else if(response2[i].position === 23052)
{
tempElement.setAttribute("src", "images/vehicle10.png");
tempElement.style.top = "32.8%";
}
else if(response2[i].position === 23053)
{
tempElement.setAttribute("src", "images/vehicle11.png");
tempElement.style.top = "31.6%";
}
else if(response2[i].position === 23054)
{
tempElement.setAttribute("src", "images/vehicle12.png");
tempElement.style.top = "30.5%";
}
else if(response2[i].position === 23055)
{
tempElement.setAttribute("src", "images/vehicle13.png");
tempElement.style.top = "30.1%";
}
else if(response2[i].position === 23056)
{
tempElement.setAttribute("src", "images/vehicle14.png");
tempElement.style.top = "29.5%";
}
}
else if(response2[i].position >= 31000 && response2[i].position <= 31170)
{
tempElement.setAttribute("src", "images/vehicle3.png");
tempElement.style.left = "43.9%";
tempElement.style.top = (step * (response2[i].position - 31000)) +"%";
}
else if(response2[i].position >= 32000 && response2[i].position <= 32170)
{
tempElement.style.top = (step * (response2[i].position - 32000)) +"%";
if(response2[i].position < 32055 || response2[i].position > 32085)
{
tempElement.setAttribute("src", "images/vehicle3.png");
tempElement.style.left = "46.6%";
}
else if(response2[i].position < 32064)
{
tempElement.setAttribute("src", "images/vehicle9.png");
tempElement.style.left = (24 + (44.1 + (0.2 * (response2[i].position - 32055))) * 0.52) + "%";
}
else if(response2[i].position < 32072)
{
tempElement.setAttribute("src", "images/vehicle8.png");
tempElement.style.left = (24 + (46.5 + (0.3 * (response2[i].position - 32064))) * 0.52) + "%";
}
else if(response2[i].position < 32079)
{
tempElement.setAttribute("src", "images/vehicle7.png");
tempElement.style.left = (24 + (49.6 + (0.4 * (response2[i].position - 32072))) * 0.52) + "%";
}
else if(response2[i].position < 32083)
{
tempElement.setAttribute("src", "images/vehicle6.png");
tempElement.style.left = (24 + (52.9 + (0.5 * (response2[i].position - 32079))) * 0.52) + "%";
}
else if(response2[i].position <= 32085)
{
tempElement.setAttribute("src", "images/vehicle5.png");
tempElement.style.left = (24 + (55.5 + (0.6 * (response2[i].position - 32083))) * 0.52) + "%";
}
}
else if(response2[i].position >= 33000 && response2[i].position <= 33170)
{
tempElement.style.top = (step * (response2[i].position - 33000)) +"%";
if(response2[i].position < 33052 || response2[i].position > 33056)
{
tempElement.setAttribute("src", "images/vehicle3.png");
tempElement.style.left = "41.15%";
}
else if(response2[i].position === 33052)
{
tempElement.setAttribute("src", "images/vehicle15.png");
tempElement.style.left = "40.8%";
}
else if(response2[i].position === 33053)
{
tempElement.setAttribute("src", "images/vehicle16.png");
tempElement.style.left = "40.55%";
}
else if(response2[i].position === 33054)
{
tempElement.setAttribute("src", "images/vehicle17.png");
tempElement.style.left = "40.2%";
}
else if(response2[i].position === 33055)
{
tempElement.setAttribute("src", "images/vehicle18.png");
tempElement.style.left = "39.9%";
}
else if(response2[i].position === 33056)
{
tempElement.setAttribute("src", "images/vehicle19.png");
tempElement.style.left = "39.5%";
}
}
else if(response2[i].position >= 41000 && response2[i].position <= 41170)
{
tempElement.setAttribute("src", "images/vehicle4.png");
tempElement.style.left = "53%";
tempElement.style.bottom = (step * (response2[i].position - 41000)) +"%";
}
else if(response2[i].position >= 42000 && response2[i].position <= 42170)
{
tempElement.style.top = "";
tempElement.style.bottom = (step * (response2[i].position - 42000)) +"%";
if(response2[i].position < 42055 || response2[i].position > 42085)
{
tempElement.setAttribute("src", "images/vehicle4.png");
tempElement.style.left = "50.3%";
}
else if(response2[i].position < 42064)
{
tempElement.style.left = "";
tempElement.setAttribute("src", "images/vehicle14.png");
tempElement.style.bottom = (step * (response2[i].position + 1 - 42000)) +"%";
tempElement.style.right = (24 + (44.1 + (0.2 * (response2[i].position - 42055))) * 0.52) + "%";
}
else if(response2[i].position < 42072)
{
tempElement.setAttribute("src", "images/vehicle13.png");
tempElement.style.bottom = (step * (response2[i].position + 1 - 42000)) +"%";
tempElement.style.right = (24 + (46.5 + (0.3 * (response2[i].position - 42064))) * 0.52) + "%";
}
else if(response2[i].position < 42079)
{
tempElement.setAttribute("src", "images/vehicle12.png");
tempElement.style.bottom = (step * (response2[i].position + 1 - 42000)) +"%";
tempElement.style.right = (24 + (49.6 + (0.4 * (response2[i].position - 42072))) * 0.52) + "%";
}
else if(response2[i].position < 42083)
{
tempElement.setAttribute("src", "images/vehicle11.png");
tempElement.style.bottom = (step * (response2[i].position + 1 - 42000)) +"%";
tempElement.style.right = (24 + (52.9 + (0.5 * (response2[i].position - 42079))) * 0.52) + "%";
}
else if(response2[i].position <= 42085)
{
tempElement.setAttribute("src", "images/vehicle10.png");
tempElement.style.bottom = (step * (response2[i].position + 1 - 42000)) +"%";
tempElement.style.right = (24 + (55.5 + (0.6 * (response2[i].position - 42083))) * 0.52) + "%";
}
}
else if(response2[i].position >= 43000 && response2[i].position <= 43170)
{
tempElement.style.top = "";
tempElement.style.bottom = (step * (response2[i].position - 43000)) +"%";
if(response2[i].position < 43052 || response2[i].position > 43056)
{
tempElement.setAttribute("src", "images/vehicle4.png");
tempElement.style.left = "55.8%";
}
else if(response2[i].position === 43052)
{
tempElement.setAttribute("src", "images/vehicle20.png");
tempElement.style.left = "56.1%";
tempElement.style.bottom = (step * (response2[i].position + 1 - 43000)) +"%";
}
else if(response2[i].position === 43053)
{
tempElement.setAttribute("src", "images/vehicle21.png");
tempElement.style.left = "56.35%";
tempElement.style.bottom = (step * (response2[i].position + 1 - 43000)) +"%";
}
else if(response2[i].position === 43054)
{
tempElement.setAttribute("src", "images/vehicle22.png");
tempElement.style.left = "56.55%";
tempElement.style.bottom = (step * (response2[i].position + 1 - 43000)) +"%";
}
else if(response2[i].position === 43055)
{
tempElement.setAttribute("src", "images/vehicle23.png");
tempElement.style.left = "56.7%";
tempElement.style.bottom = (step * (response2[i].position + 1 - 43000)) +"%";
}
else if(response2[i].position === 43056)
{
tempElement.setAttribute("src", "images/vehicle24.png");
tempElement.style.left = "57%";
tempElement.style.bottom = (step * (response2[i].position + 2 - 43000)) +"%";
}
}
}
for(let i = 0; i < vehicleElements.length; i++)
{
let exists = false;
for(let j = 0; j < response2.length; j++)
{
if(response2[j]._id === vehicleElements[i]._id)
{
exists = true;
break;
}
}
if(!exists)
{
vehicleElements[i].element.remove();
if(vehicleElements.length === 1)
{
vehicleElements.pop();
}
else
{
vehicleElements[i] = vehicleElements.pop();
}
}
}
}
Loading…
Cancel
Save