2024-02-06 22:58:49 +01:00
|
|
|
import brToNewline from '@derhuerst/br2nl';
|
|
|
|
import omit from 'lodash/omit.js';
|
2018-06-07 18:46:24 +02:00
|
|
|
|
2018-06-30 18:04:44 +02:00
|
|
|
const typesByIcon = Object.assign(Object.create(null), {
|
2024-02-06 22:58:49 +01:00
|
|
|
HimWarn: 'status',
|
|
|
|
});
|
2018-06-30 18:04:44 +02:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
const parseMsgEdge = (ctx) => (e) => {
|
2019-08-23 16:17:20 +02:00
|
|
|
const res = omit(e, [
|
|
|
|
'icoX',
|
2024-02-06 22:58:49 +01:00
|
|
|
'fLocX',
|
|
|
|
'fromLocation',
|
|
|
|
'tLocX',
|
|
|
|
'toLocation',
|
|
|
|
]);
|
|
|
|
res.icon = e.icon || null;
|
|
|
|
res.fromLocation = Array.isArray(e.fromLocations) && e.fromLocations[0] || e.fromLocation || null;
|
|
|
|
res.toLocation = Array.isArray(e.toLocations) && e.toLocations[0] || e.toLocation || null;
|
|
|
|
return res;
|
|
|
|
};
|
2022-11-18 18:49:51 +01:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const fallbackTime = '000000'; // midnight
|
2020-01-15 18:17:06 +01:00
|
|
|
const parseMsgEvent = (ctx) => (e) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
const {profile} = ctx; // todo: test that covers this
|
2019-02-25 15:04:25 +01:00
|
|
|
return {
|
2021-12-29 17:00:14 +01:00
|
|
|
fromLocation: Array.isArray(e.fromLocations) && e.fromLocations[0] || e.fromLocation || null,
|
|
|
|
toLocation: Array.isArray(e.toLocations) && e.toLocations[0] || e.toLocation || null,
|
2022-11-18 18:49:51 +01:00
|
|
|
start: profile.parseDateTime(ctx, e.fDate, e.fTime || fallbackTime, null),
|
|
|
|
end: profile.parseDateTime(ctx, e.tDate, e.tTime || fallbackTime, null),
|
2024-02-06 22:58:49 +01:00
|
|
|
sections: e.sectionNums || [], // todo: parse
|
|
|
|
};
|
|
|
|
};
|
2019-02-25 15:04:25 +01:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
const parseWarning = (ctx, w) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
const {profile, res: resp, common} = ctx;
|
2019-10-20 00:19:11 +02:00
|
|
|
|
2020-03-09 20:54:59 +01:00
|
|
|
// todo: https://github.com/marudor/BahnhofsAbfahrten/blob/46a74957d68edc15713112df44e1a25150f5a178/src/types/HAFAS/HimSearch.ts#L31-L53
|
|
|
|
// todo: act, pub, lead, tckr
|
|
|
|
// todo: cat (1, 2), pubChL, rRefL/hints, impactL
|
|
|
|
// todo: pubChL
|
2018-06-07 18:46:24 +02:00
|
|
|
// [ { name: 'timetable',
|
|
|
|
// fDate: '20180606',
|
|
|
|
// fTime: '073000',
|
|
|
|
// tDate: '20180713',
|
|
|
|
// tTime: '030000' },
|
|
|
|
// { name: 'export',
|
|
|
|
// fDate: '20180606',
|
|
|
|
// fTime: '073000',
|
|
|
|
// tDate: '20180713',
|
|
|
|
// tTime: '030000' } ]
|
2019-02-25 15:04:25 +01:00
|
|
|
// { name: '1',
|
|
|
|
// fDate: '20190219',
|
|
|
|
// fTime: '000000',
|
|
|
|
// tDate: '20190225',
|
|
|
|
// tTime: '120000' }
|
2020-03-09 20:54:59 +01:00
|
|
|
// todo: w.regionRefL & res.common.himMsgRegionL
|
2018-06-07 18:46:24 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const icon = w.icon || null;
|
|
|
|
const type = icon && icon.type && typesByIcon[icon.type] || 'warning';
|
2018-06-30 18:04:44 +02:00
|
|
|
|
2018-08-24 20:16:23 +02:00
|
|
|
const res = {
|
2019-03-27 18:58:34 +01:00
|
|
|
id: w.hid || null,
|
2018-06-30 18:04:44 +02:00
|
|
|
type,
|
2024-02-06 22:58:49 +01:00
|
|
|
summary: w.head
|
|
|
|
? brToNewline(w.head)
|
|
|
|
: null, // todo: decode HTML entities?
|
|
|
|
text: w.text
|
|
|
|
? brToNewline(w.text)
|
|
|
|
: null, // todo: decode HTML entities?
|
2019-09-03 15:33:05 +02:00
|
|
|
icon, // todo: parse icon
|
2018-06-07 18:46:24 +02:00
|
|
|
priority: w.prio,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
|
|
|
if ('prod' in w) {
|
|
|
|
res.products = profile.parseProductsBitmask(ctx, w.prod);
|
|
|
|
}
|
|
|
|
if ('comp' in w) {
|
|
|
|
res.company = w.comp || null;
|
2018-06-07 18:46:24 +02:00
|
|
|
}
|
2020-03-09 20:54:59 +01:00
|
|
|
|
|
|
|
// todo: parse to sth meaningful
|
2024-02-06 22:58:49 +01:00
|
|
|
if ('cat' in w) {
|
|
|
|
res.category = w.cat;
|
|
|
|
}
|
2020-03-09 20:54:59 +01:00
|
|
|
if (w.catRefL && resp.common && resp.common.himMsgCatL) {
|
|
|
|
res.categories = w.catRefL
|
2024-02-06 22:58:49 +01:00
|
|
|
.map(i => resp.common.himMsgCatL[i])
|
|
|
|
.filter(e => Boolean(e))
|
|
|
|
.map(cat => cat.id);
|
2020-03-09 20:54:59 +01:00
|
|
|
}
|
2019-02-25 15:04:25 +01:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
if (w.edgeRefL && resp.common && resp.common.himMsgEdgeL) {
|
2019-02-25 15:04:25 +01:00
|
|
|
res.edges = w.edgeRefL
|
2024-02-06 22:58:49 +01:00
|
|
|
.map(i => resp.common.himMsgEdgeL[i])
|
|
|
|
.filter(e => Boolean(e))
|
|
|
|
.map(parseMsgEdge(ctx));
|
2019-02-25 15:04:25 +01:00
|
|
|
}
|
2019-10-20 00:19:11 +02:00
|
|
|
if (w.eventRefL && resp.common && resp.common.himMsgEventL) {
|
2019-02-25 15:04:25 +01:00
|
|
|
res.events = w.eventRefL
|
2024-02-06 22:58:49 +01:00
|
|
|
.map(i => resp.common.himMsgEventL[i])
|
|
|
|
.filter(e => Boolean(e))
|
|
|
|
.map(parseMsgEvent(ctx));
|
2019-02-25 15:04:25 +01:00
|
|
|
}
|
2018-08-24 20:16:23 +02:00
|
|
|
|
2020-03-09 20:54:59 +01:00
|
|
|
if (w.affProdRefL) {
|
|
|
|
res.affectedLines = w.affProdRefL
|
2024-02-06 22:58:49 +01:00
|
|
|
.map(i => common.lines[i])
|
|
|
|
.filter(l => Boolean(l));
|
|
|
|
}
|
|
|
|
if (w.fromLocations) {
|
|
|
|
res.fromStops = w.fromLocations;
|
|
|
|
}
|
|
|
|
if (w.toLocations) {
|
|
|
|
res.toStops = w.toLocations;
|
2020-03-09 20:54:59 +01:00
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
if (w.sDate && w.sTime) {
|
|
|
|
res.validFrom = profile.parseDateTime(ctx, w.sDate, w.sTime, null);
|
|
|
|
}
|
|
|
|
if (w.eDate && w.eTime) {
|
|
|
|
res.validUntil = profile.parseDateTime(ctx, w.eDate, w.eTime, null);
|
|
|
|
}
|
|
|
|
if (w.lModDate && w.lModTime) {
|
|
|
|
res.modified = profile.parseDateTime(ctx, w.lModDate, w.lModTime, null);
|
|
|
|
}
|
2018-08-24 20:16:23 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
return res;
|
|
|
|
};
|
2018-06-07 18:46:24 +02:00
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
export {
|
|
|
|
parseWarning,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|