obs-portal/logic/tracks.js

235 lines
5 KiB
JavaScript
Raw Normal View History

2020-11-18 19:53:35 +00:00
const csvParse = require('csv-parse/lib/sync');
2020-11-17 17:24:49 +00:00
function _parseFloat(token) {
2020-11-18 16:24:28 +00:00
let f = parseFloat(token);
2020-11-17 17:24:49 +00:00
if (isNaN(f)) {
f = parseFloat(token.substring(0, 10));
}
if (isNaN(f)) {
f = 0.0;
}
return f;
}
2020-11-18 19:53:35 +00:00
function addPointsToTrack(trackInfo, body, format = null) {
const detectedFormat = format != null ? format : detectFormat(body);
let parser;
switch (detectedFormat) {
case 'invalid':
throw new Error('track format cannot be detected');
case 1:
parser = parseObsver1;
break;
case 2:
parser = parseObsver2;
break;
}
const points = trackInfo.trackData.points;
for (const newPoint of parser(body)) {
points.push(newPoint);
}
}
function detectFormat(body) {
if (!body.length) {
return 'invalid';
}
const firstLinebreakIndex = body.indexOf('\n');
if (firstLinebreakIndex === -1) {
return 1;
}
const firstLine = body.substring(0, firstLinebreakIndex);
const match = firstLine.match(/(^|&)OBSDataFormat=([\d]+)($|&)/);
if (match) {
return Number(match[2]);
}
return 'invalid';
}
function* parseObsver1(body) {
2020-11-18 16:24:28 +00:00
let num = 0;
let start = 0;
let end = 0;
2020-11-17 17:24:49 +00:00
2020-11-18 16:24:28 +00:00
let currentPoint;
2020-11-17 17:24:49 +00:00
while (end < body.length) {
start = end;
2020-11-18 16:24:28 +00:00
while (body[end] !== ';' && body[end] !== '$' && end < body.length) {
end++;
}
2020-11-18 16:24:28 +00:00
if (body[end] === '$') {
2020-11-18 19:53:35 +00:00
if (currentPoint) {
yield currentPoint;
}
2020-11-17 17:07:32 +00:00
// $ is replacing \n as newlines are not allowed in json strings
num = 0;
}
2020-11-17 17:07:32 +00:00
if (end < body.length) {
2020-11-18 16:24:28 +00:00
const token = body.substr(start, end - start);
2020-11-17 17:07:32 +00:00
end++;
2020-11-17 17:24:49 +00:00
if (token.length > 0) {
2020-11-18 16:24:28 +00:00
if (num === 0 && token === 'Date') {
2020-11-17 17:07:32 +00:00
// we have a header line, ignore it for now, TODO parse it
if (end < body.length) {
2020-11-18 16:24:28 +00:00
while (body[end] !== ';' && body[end] !== '$' && end < body.length) {
2020-11-17 17:07:32 +00:00
end++;
}
start = end;
num = 100;
}
}
2020-11-17 17:24:49 +00:00
switch (num) {
case 0:
currentPoint = {
2020-11-18 19:53:35 +00:00
date: token,
2020-11-17 17:24:49 +00:00
time: '',
latitude: '',
longitude: '',
course: '',
speed: '',
d1: '',
d2: '',
flag: '',
private: '',
};
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 17:24:49 +00:00
num++;
}
}
}
2020-11-18 19:53:35 +00:00
if (currentPoint) {
yield currentPoint;
}
}
function* parseObsver2(body) {
for (const record of csvParse(body, {
from_line: 2,
trim: true,
columns: true,
skip_empty_lines: true,
delimiter: ';',
encoding: 'utf8',
relax_column_count: true,
cast(value, context) {
if (value === '') {
return null;
}
let type;
switch (context.column) {
case 'Millis':
case 'Left':
case 'Right':
case 'Confirmed':
case 'Invalid':
case 'InsidePrivacyArea':
case 'Measurements':
case 'Satellites':
type = 'int';
break;
case 'Date':
case 'Time':
case 'Comment':
case 'Marked':
type = 'string';
break;
case 'Latitude':
case 'Longitude':
case 'Altitude':
case 'Course':
case 'Speed':
case 'HDOP':
case 'BatteryLevel':
case 'Factor':
type = 'float';
break;
default:
type = /^(Tms|Lus|Rus)/.test(context.column) ? 'int' : 'string';
}
switch (type) {
case 'int':
return parseInt(value);
case 'float':
return parseFloat(value);
case 'string':
return value;
}
},
})) {
// We convert the new format back to the old format for storage here, until
// we upgrade the storage format as well to include all data. But we'll
// have to upgrade the obsApp first.
yield {
date: record.Date,
time: record.Time,
latitude: record.Latitude,
longitude: record.Longitude,
course: record.Course,
speed: record.Speed,
d1: record.Left,
d2: record.Right,
flag: Boolean(record.Confirmed),
private: Boolean(record.InsidePrivacyArea),
};
}
}
module.exports = { addPointsToTrack, detectFormat, parseObsver1, parseObsver2 };