db-vendo-client/parse/location.js

73 lines
1.3 KiB
JavaScript
Raw Normal View History

import {parse} from 'qs';
2019-02-05 19:07:19 +01:00
2024-12-07 16:16:31 +00:00
const POI = 'POI';
const STATION = 'ST';
const ADDRESS = 'ADR';
2017-11-11 22:35:41 +01:00
const leadingZeros = /^0+/;
const parseLocation = (ctx, l) => {
2024-12-18 00:08:52 +00:00
const {profile, common} = ctx;
2024-12-07 22:46:04 +00:00
if (!l) {
return null;
}
2024-12-07 16:16:31 +00:00
const lid = parse(l.id, {delimiter: '@'});
const res = {
type: 'location',
2024-12-10 17:51:20 +00:00
id: (l.extId || lid.L || l.evaNumber || l.evaNo || '').replace(leadingZeros, '') || null,
};
2024-12-17 19:41:00 +00:00
const name = l.name || lid.O;
2024-12-07 16:16:31 +00:00
if (l.lat && l.lon) {
res.latitude = l.lat;
res.longitude = l.lon;
} else if ('X' in lid && 'Y' in lid) {
res.latitude = lid.Y / 1000000;
res.longitude = lid.X / 1000000;
2017-11-11 22:35:41 +01:00
}
2024-12-17 19:41:00 +00:00
if (l.type === STATION || l.extId || l.evaNumber || l.evaNo || lid.A == 1) {
2024-12-18 00:08:52 +00:00
let stop = {
type: 'station',
id: res.id,
2024-12-17 19:41:00 +00:00
name: name,
location: 'number' === typeof res.latitude
? res
: null, // todo: remove `.id`
};
2024-12-07 16:16:31 +00:00
// TODO subStops
2024-12-07 16:16:31 +00:00
if ('products' in l) {
stop.products = profile.parseProductsBitmask(ctx, l.products);
}
2024-12-18 00:08:52 +00:00
if (common && common.locations && common.locations[stop.id]) {
delete stop.type;
stop = {
...common.locations[stop.id],
...stop,
};
}
2024-12-08 21:42:57 +00:00
// TODO isMeta
2024-12-18 00:08:52 +00:00
// TODO entrances, lines
return stop;
}
2024-12-17 19:41:00 +00:00
res.name = name;
if (l.type === ADDRESS || lid.A == 2) {
res.address = name;
}
2024-12-17 19:41:00 +00:00
if (l.type === POI || lid.A == 4) {
res.poi = true;
}
return res;
};
2017-11-11 22:35:41 +01:00
2022-05-07 16:17:37 +02:00
export {
2024-12-07 16:16:31 +00:00
parseLocation,
};