From f9800dab39f516b46cb79d55806c57c1f2b9baf0 Mon Sep 17 00:00:00 2001 From: Andreas Menzel Date: Tue, 21 Mar 2023 09:07:10 +0100 Subject: [PATCH] Update ask infrastructure to new specification --- app/js/informationHolders/Corridor.js | 21 +---- app/js/informationHolders/Intersection.js | 24 ++--- app/js/main.js | 109 ++++++++++++---------- 3 files changed, 72 insertions(+), 82 deletions(-) diff --git a/app/js/informationHolders/Corridor.js b/app/js/informationHolders/Corridor.js index 5233aaa..08da3ec 100644 --- a/app/js/informationHolders/Corridor.js +++ b/app/js/informationHolders/Corridor.js @@ -22,22 +22,11 @@ class Corridor { } - updateValues() { - const payload = '{"corridor_id": "' + this.#id + '","data_type": "corridor_location"}'; - const handleResponse = () => { - const response = JSON.parse(xhttp.responseText); - - if(response['executed']) { - this.#dataValid = true; - - this.#intersectionAId = response['response_data']['intersection_a']; - this.#intersectionBId = response['response_data']['intersection_b']; - } - }; - const xhttp = new XMLHttpRequest(); - xhttp.onload = () => { handleResponse() }; - xhttp.open('GET', trafficControlUrl + 'ask/corridor_location?payload=' + payload + '&rand=' + new Date().getTime(), true); - xhttp.send(); + setValues(intersectionAId, intersectionBId) { + this.#dataValid = true; + + this.#intersectionAId = intersectionAId; + this.#intersectionBId = intersectionBId; } diff --git a/app/js/informationHolders/Intersection.js b/app/js/informationHolders/Intersection.js index 5c587d3..d66bf62 100644 --- a/app/js/informationHolders/Intersection.js +++ b/app/js/informationHolders/Intersection.js @@ -29,23 +29,13 @@ class Intersection { } - updateValues() { - const payload = '{"intersection_id": "' + this.#id + '","data_type": "intersection_location"}'; - const handleResponse = () => { - const response = JSON.parse(xhttp.responseText); - if(response['executed']) { - this.#dataValid = true; - - this.#gpsLat = response['response_data']['gps_lat']; - this.#gpsLon = response['response_data']['gps_lon']; - - this.#altitude = response['response_data']['altitude']; - } - }; - const xhttp = new XMLHttpRequest(); - xhttp.onload = () => { handleResponse() }; - xhttp.open('GET', trafficControlUrl + 'ask/intersection_location?payload=' + payload + '&rand=' + new Date().getTime(), true); - xhttp.send(); + setValues(gpsLat, gpsLon, altitude) { + this.#dataValid = true; + + this.#gpsLat = gpsLat; + this.#gpsLon = gpsLon; + + this.#altitude = altitude; } diff --git a/app/js/main.js b/app/js/main.js index ba088cb..6242885 100644 --- a/app/js/main.js +++ b/app/js/main.js @@ -23,20 +23,20 @@ function setAppletWrapperHeightsWithControlPanel() { function toBoolean(stringValue) { - if(typeof(stringValue) === 'string') { + if (typeof (stringValue) === 'string') { switch (stringValue?.toLowerCase()?.trim()) { case "true": case "yes": case "1": return true; - + case "false": case "no": case "0": case null: case undefined: return false; - + default: return JSON.parse(stringValue); } @@ -99,56 +99,67 @@ function updateDroneList() { function updateIntersectionList() { const payload = '{"intersection_id": "%","data_type": "intersection_list"}'; - const handleResponse = () => { - const response = JSON.parse(xhttp.responseText); - if(response['executed']) { - const intersectionIds = response['response_data']['intersection_ids']; - const myIntersectionIds = Object.keys(intersections); - - const intersectionsToAdd = intersectionIds.filter(x => !myIntersectionIds.includes(x)); - const intersectionsToRemove = myIntersectionIds.filter(x => !intersectionIds.includes(x)); - - for(newInt in intersectionsToAdd) { - const newIntId = intersectionsToAdd[newInt]; - intersections[newIntId] = new Intersection(newIntId); - } - for(remInt in intersectionsToRemove) { - const remIntId = intersectionsToRemove[remInt]; - delete intersections[remIntId]; - } + const handleResponse = () => { + const response = JSON.parse(xhttp.responseText); + if (response['executed']) { + const intersectionIds = Object.keys(response['response_data']); + const myIntersectionIds = Object.keys(intersections); + + const intersectionsToAdd = intersectionIds.filter(x => !myIntersectionIds.includes(x)); + const intersectionsToRemove = myIntersectionIds.filter(x => !intersectionIds.includes(x)); + + for (newIntId of intersectionsToAdd) { + intersections[newIntId] = new Intersection(newIntId); + } + for (remIntId of intersectionsToRemove) { + delete intersections[remIntId]; + } + + for (intId of intersectionIds) { + const gpsLat = response['response_data'][intId]['gps_lat']; + const gpsLon = response['response_data'][intId]['gps_lon']; + const altitude = response['response_data'][intId]['altitude']; + + intersections[intId].setValues(gpsLat, gpsLon, altitude); } - }; - const xhttp = new XMLHttpRequest(); - xhttp.onload = () => { handleResponse() }; - xhttp.open('GET', trafficControlUrl + 'ask/intersection_list?payload=' + payload, true); - xhttp.send(); + } + }; + const xhttp = new XMLHttpRequest(); + xhttp.onload = () => { handleResponse() }; + xhttp.open('GET', trafficControlUrl + 'ask/intersection_list?payload=' + payload, true); + xhttp.send(); } function updateCorridorList() { const payload = '{"corridor_id": "%","data_type": "corridor_list"}'; - const handleResponse = () => { - const response = JSON.parse(xhttp.responseText); - if(response['executed']) { - const corridorIds = response['response_data']['corridor_ids']; - const myCorridorIds = Object.keys(corridors); - - const corridorsToAdd = corridorIds.filter(x => !myCorridorIds.includes(x)); - const corridorsToRemove = myCorridorIds.filter(x => !corridorIds.includes(x)); - - for(newCor in corridorsToAdd) { - const newCorId = corridorsToAdd[newCor]; - corridors[newCorId] = new Corridor(newCorId); - } - for(remCor in corridorsToRemove) { - const remCorId = corridorsToRemove[remCor]; - delete corridors[remCorId]; - } + const handleResponse = () => { + const response = JSON.parse(xhttp.responseText); + if (response['executed']) { + const corridorIds = Object.keys(response['response_data']); + const myCorridorIds = Object.keys(corridors); + + const corridorsToAdd = corridorIds.filter(x => !myCorridorIds.includes(x)); + const corridorsToRemove = myCorridorIds.filter(x => !corridorIds.includes(x)); + + for (newCorId of corridorsToAdd) { + corridors[newCorId] = new Corridor(newCorId); + } + for (remCorId of corridorsToRemove) { + delete corridors[remCorId]; } - }; - const xhttp = new XMLHttpRequest(); - xhttp.onload = () => { handleResponse() }; - xhttp.open('GET', trafficControlUrl + 'ask/corridor_list?payload=' + payload, true); - xhttp.send(); + + for (corId of corridorIds) { + const intersectionAId = response['response_data'][corId]['intersection_a']; + const intersectionBId = response['response_data'][corId]['intersection_b']; + + corridors[corId].setValues(intersectionAId, intersectionBId); + } + } + }; + const xhttp = new XMLHttpRequest(); + xhttp.onload = () => { handleResponse() }; + xhttp.open('GET', trafficControlUrl + 'ask/corridor_list?payload=' + payload, true); + xhttp.send(); } @@ -172,10 +183,10 @@ function updateInfrastructure() { // TODO: Update infrastructure list // TODO: Add / remove intersections / corridors - for(intersectionId in intersections) { + for (intersectionId in intersections) { intersections[intersectionId].updateValues(); } - for(corridorId in corridors) { + for (corridorId in corridors) { corridors[corridorId].updateValues(); } }