obs-portal/logic/tracks.js

108 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-11-17 18:24:49 +01:00
function _parseFloat(token) {
2020-11-18 17:24:28 +01:00
let f = parseFloat(token);
2020-11-17 18:24:49 +01:00
if (isNaN(f)) {
f = parseFloat(token.substring(0, 10));
}
if (isNaN(f)) {
f = 0.0;
}
return f;
}
2020-11-17 18:07:32 +01:00
module.exports.addPointsToTrack = function addPointsToTrack(track, body) {
2020-11-18 17:24:28 +01:00
let num = 0;
let start = 0;
let end = 0;
2020-11-17 18:24:49 +01:00
// reference to the array we will mutate
2020-11-18 17:24:28 +01:00
const points = track.trackData.points;
let currentPoint;
2020-11-17 18:24:49 +01:00
while (end < body.length) {
start = end;
2020-11-18 17:24:28 +01:00
while (body[end] !== ';' && body[end] !== '$' && end < body.length) {
end++;
}
2020-11-18 17:24:28 +01:00
if (body[end] === '$') {
2020-11-17 18:07:32 +01:00
// $ is replacing \n as newlines are not allowed in json strings
num = 0;
}
2020-11-17 18:07:32 +01:00
if (end < body.length) {
2020-11-18 17:24:28 +01:00
const token = body.substr(start, end - start);
2020-11-17 18:07:32 +01:00
end++;
2020-11-17 18:24:49 +01:00
if (token.length > 0) {
2020-11-18 17:24:28 +01:00
if (num === 0 && token === 'Date') {
2020-11-17 18:07:32 +01:00
// we have a header line, ignore it for now, TODO parse it
if (end < body.length) {
2020-11-18 17:24:28 +01:00
while (body[end] !== ';' && body[end] !== '$' && end < body.length) {
2020-11-17 18:07:32 +01:00
end++;
}
start = end;
num = 100;
}
}
2020-11-17 18:24:49 +01:00
switch (num) {
case 0:
currentPoint = {
date: 'dummy',
time: '',
latitude: '',
longitude: '',
course: '',
speed: '',
d1: '',
d2: '',
flag: '',
private: '',
};
points.push(currentPoint);
currentPoint.date = token;
break;
case 1:
currentPoint.time = token;
break;
case 2:
currentPoint.latitude = _parseFloat(token);
break;
case 3:
currentPoint.longitude = _parseFloat(token);
break;
case 4:
currentPoint.course = _parseFloat(token);
break;
case 5:
currentPoint.speed = _parseFloat(token);
break;
case 6:
currentPoint.d1 = token;
break;
case 7:
currentPoint.d2 = token;
break;
case 8:
currentPoint.flag = token;
break;
case 9:
currentPoint.private = token;
break;
}
2020-11-17 18:24:49 +01:00
num++;
}
}
}
2020-11-17 18:07:32 +01:00
};