Implement train and timetable

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface
2023-06-19 21:34:14 +01:00
parent 199a1760b7
commit a4d82b0aa7
8 changed files with 157 additions and 5 deletions

View File

@@ -58,7 +58,8 @@ async function arrDepBoardStaff(CRS) {
crs: CRS.toUpperCase(),
getNonPassengerServices: true,
time: await getDateTimeString(new Date),
timeWindow: 120
timeWindow: 120,
services: 'PBS'
};
const api = new ldb(ldbsvKey,true);
return await api.call('GetArrivalDepartureBoardByCRS',options,false,false);
@@ -85,6 +86,21 @@ async function getServiceByRID(rid) {
}
}
async function getServicesByOther(id) {
log.out(`ldbService.getServiceByOther: Finding services: ${id}`, 'dbug');
try {
const options = {
serviceID: id,
sdd: getDateString(new Date)
};
const api = new ldb(ldbsvKey,true);
return await api.call('QueryServices', options, false, false);
} catch (err) {
log.out(`ldbService.getServiceByOther: Error: ${err}`, 'EROR');
return false;
}
}
async function getReasonCodeList() {
log.out('ldbService.getReasonCodeList: Fetching reason code list', 'eror');
try {
@@ -119,9 +135,18 @@ async function getDateTimeString(date) {
return format;
}
async function getDateString(date) {
const year = date.getFullYear(),
month = String(date.getMonth() + 1).padStart(2,'0'),
day = String(date.getDate()).padStart(2,'0');
const format = `${year}-${month}-${day}`;
return format;
}
module.exports = {
get,
getServiceByRID,
getServicesByOther,
getReasonCodeList,
getReasonCode
};

View File

@@ -35,8 +35,46 @@ async function findByHeadcodeToday(headcode) {
return preparedData;
}
async function findByHeadcode(date, headcode) {
const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase();
log.out('trainServiceServices.findByHeadcode: Searching for headcode ' +
sanitizedHeadcode, 'dbug');
let searchDate;
if (date === 'now') {
searchDate = new Date();
} else {
searchDate = new Date(date);
}
const dayMap = ['su', 'm', 't', 'w', 'th', 'f', 's'];
const shortDay = dayMap[searchDate.getDay()]; // Fetch short day from map
const query = {
headcode: sanitizedHeadcode,
scheduleStartDate: {$lte: searchDate},
scheduleEndDate: {$gte: searchDate},
daysRun: {$in: [shortDay]}
};
const queryData = await db.query('timetable', query);
let trainData = await parseTrains(queryData);
let preparedData = [];
for (let trainService in trainData) {
// Search for PIS Code for each service
const tiplocList = await getPublicStops(trainData[trainService]['stops']);
//console.log(tiplocList.length); console.log(tiplocList);
if (tiplocList.length) {
const pisDetail = await pis.findByTiplocArray(tiplocList);
trainData[trainService]['pis'] = pisDetail?.[0]?.['code'] ?? 'None';
} else {
trainData[trainService]['pis'] = '0015'; // Not in Service code
// '0015' is a string otherwise it is interpreted as octal 13.
}
preparedData.push(trainData[trainService]);
}
return preparedData;
}
module.exports = {
findByHeadcodeToday,
findByHeadcode
};
/* Internal Functions, not to be exported */