gyrogpsc/static/scripts/refull.js

189 lines
6.7 KiB
JavaScript

/**
* This file contains all the controls for the full replay page.
*
* @authors Timo Volkmann, Frank Herkommer.
*/
// indexes of a list of all collected Serial data where Timestamps matched with the smallest time
//difference so they can be fitted on a chart with a smaller amount of tcp data
let indexes
/**
* function to parse a timestamp to a number
* @param time Timestamp
* @returns composed a number
*/
function composeTimestamp(time){
let composed;
composed = time.slice(11,25).split(':').join("").split('.').join("");
return composed;
}
/**
* function to compare a Timestamp from tcp to all timestamps from serial to find a best match.
* @param num tcp timestamp
* @param arr list of serial timestamps
* @returns {(*|number)[]} array with the index of the closest Serial timestamp match
*/
function findBestTimeMatch(num, arr) {
let mid;
let lo = 0;
let hi = arr.length - 1;
while (hi - lo > 1) {
mid = Math.floor ((lo + hi) / 2);
if (arr[mid] < num) {
lo = mid;
} else {
hi = mid;
}
}
if (num - arr[lo] <= arr[hi] - num) {
return [arr[lo], lo];
}
return [arr[hi], hi];
}
/**
* function that calls find best match function and saves the indexes from a match into a list.
* @param tcpdataList
* @param serialdataList
*/
function findSerialDataIndex(tcpdataList, serialdataList) {
let allSerialTimes = []
serialdataList.forEach(sensordata => {
if (sensordata.Speed !== 0 && sensordata.HAcc !== 0) {
allSpeedsSerial.push(sensordata.Speed)
allAccSerial.push(sensordata.HAcc)
if(sensordata.Position[1] !== 0 && sensordata.Position[0] !== 0){
allSerialCoords.push([sensordata.Position[1], sensordata.Position[0]])
}
let serialTimestamp = composeTimestamp(sensordata.Timestamp)
allSerialTimes.push(serialTimestamp)
}
})
tcpdataList.forEach(sensordata => {
if (sensordata.Speed !== 0 && sensordata.HAcc !== 0) {
let tcpTimestamp = composeTimestamp(sensordata.Timestamp)
let index = findBestTimeMatch(tcpTimestamp, allSerialTimes)[1]
indexes.push(index)
}
})
}
window.addEventListener("load", function(evt) {
//------------------------Buttons------------------------------
let trackings = null;
document.getElementById("messungladen").onclick = function(evt) {
fetch('http://localhost:3011/trackings/', { method: 'GET'}).then(results => {
return results.json()
}).then(r => {
console.log(r)
if (!'data' in r) {
return
}
trackings = r.data
let sel = document.getElementById("meas")
sel.innerHTML = ''
r.data.sort((a,b) => new Date(b.TimeCreated).getTime() - new Date(a.TimeCreated).getTime());
r.data.forEach(tracking => {
console.log(tracking)
let option = document.createElement("option");
option.text = tracking.TimeCreated + " Size: " + tracking.Size
sel.add(option)
})
sel.disabled = false
document.getElementById("replaystarten").disabled = false
})
};
document.getElementById("replaystarten").onclick = function(evt) {
indexes = []
allSpeedsSerial = []
allAccSerial = []
allSerialCoords = []
let sel = document.getElementById("meas")
console.log(trackings[sel.selectedIndex].UUID)
fetch(`http://localhost:3011/trackings/${trackings[sel.selectedIndex].UUID}`, { method: 'GET'}).then(results => {
return results.json()
}).then(r => {
// console.log(r.data.Data)
// console.log(r.data)
if('SOURCE_SERIAL' in r.data.Data && r.data.Data.SOURCE_SERIAL.length > 0 && 'SOURCE_TCP' in r.data.Data && r.data.Data.SOURCE_TCP.length > 0) {
findSerialDataIndex(r.data.Data.SOURCE_TCP, r.data.Data.SOURCE_SERIAL)
}
if ('SOURCE_TCP' in r.data.Data && r.data.Data.SOURCE_TCP.length > 0) {
updateMapTCPbulk(r.data.Data.SOURCE_TCP)
addTCPSpeedData(r.data.Data.SOURCE_TCP)
}
if ('SOURCE_SERIAL' in r.data.Data && r.data.Data.SOURCE_SERIAL.length > 0) {
updateMapSERIALbulk(r.data.Data.SOURCE_SERIAL)
addSerialSpeedData(r.data.Data.SOURCE_SERIAL)
}
addDistances(prepareForDistanceCalc(r.data.Data))
})
}
});
function prepareForDistanceCalc(data) {
if('SOURCE_TCP' in data && data.SOURCE_TCP.length > 0 && 'SOURCE_SERIAL' in data && data.SOURCE_SERIAL.length > 0) {
let sensorPoints = [];
data.SOURCE_TCP.forEach(element => {
if (element.Position[0] !== 0 && element.Position[1] !== 0) {
sensorPoints.push({
differenceMs: Number.MAX_VALUE,
tcp: element,
ser: null,
})
}
})
sensorPoints.forEach((el, index, arr) => {
let tcpTS = Date.parse(el.tcp.Timestamp)
data.SOURCE_SERIAL.forEach(serElement => {
let serTS = Date.parse(serElement.Timestamp)
if (Math.abs(tcpTS - serTS) < el.differenceMs && serElement.Position[0] !== 0 && serElement.Position[1] !== 0) {
el.differenceMs = tcpTS - serTS
arr[index].ser = serElement
}
})
})
// filter differences with more than 50 ms
let newSensorPoints = sensorPoints.filter(el => {
return Math.abs(el.differenceMs) < 50
})
console.log("SENSORPOINTs", newSensorPoints)
return newSensorPoints
} else if ('SOURCE_SERIAL' in data && data.SOURCE_SERIAL.length > 0) {
let sensorPoints = [];
data.SOURCE_SERIAL.forEach(element => {
if (element.Position[0] !== 0 && element.Position[1] !== 0) {
sensorPoints.push({
differenceMs: Number.MAX_VALUE,
tcp: null,
ser: element,
})
}
})
return sensorPoints
} else if ('SOURCE_TCP' in data && data.SOURCE_TCP.length > 0) {
let sensorPoints = [];
data.SOURCE_TCP.forEach(element => {
if (element.Position[0] !== 0 && element.Position[1] !== 0) {
sensorPoints.push({
differenceMs: Number.MAX_VALUE,
tcp: element,
ser: null,
})
}
})
return sensorPoints
}
return null
}